警告CS3006在这种情况下,有效吗?

编程入门 行业动态 更新时间:2024-10-09 07:27:45
本文介绍了警告CS3006在这种情况下,有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在code以下产生一个警告CS3006重载方法MyNamespace.Sample.MyMethod(INT [])'只是在不同的文献或缩小,或在阵列级别,是不符合CLS。

这是警告无效,即是这种真正的不符合CLS?我还以为显式接口实现将不算为过载。

[总成:CLSCompliant(真) 命名空间了myNameSpace {     公共类样品:ISample     {         公共无效的MyMethod(INT []数组)         {             返回;         }         无效ISample.MyMethod(REF INT []数组)         {             this.MyMethod(阵列);         }     }     公共接口ISample     {         无效的MyMethod([IN]参考INT []数组);     } }

解决方案

CLS规范仅适用于您的类的可见部分。因此,你会认为 REF INT [] 不是公开,因此不相关。但它是可见的,通过接口

你的code的用户都知道,样品为无效的MyMethod(INT [])。他们也知道,它实现 ISample 提供无效的MyMethod(REF INT [])。因此,我认为它实际上是不符合CLS。


编辑:埃里克利珀评论了原来的问题,他认为这是其实一个编译器错误,原来的code是符合CLS的。


不过,这是有效的:

[总成:CLSCompliant(真) 命名空间了myNameSpace {     公共类样品:ISample,ISample2     {         无效ISample.MyMethod(REF INT []数组)         {         }         无效ISample2.MyMethod(INT []数组)         {         }     }     公共接口ISample     {         无效的MyMethod(REF INT []数组);     }     公共接口ISample2     {         无效的MyMethod(INT []数组);     } }

这是因为CLS定义了两种接口可以定义矛盾的方法具有相同的名称或签名和编译器必须知道如何分辨出来 - 但同样,只有当冲突是两个接口之间

The code below generates a warning CS3006 "Overloaded method MyNamespace.Sample.MyMethod(int[])' differing only in ref or out, or in array rank, is not CLS-compliant".

Is this warning valid, i.e. is this genuinely not CLS-compliant? I'd have thought an explicit interface implementation would not count as an overload.

[assembly: CLSCompliant(true)] namespace MyNamespace { public class Sample : ISample { public void MyMethod(int[] array) { return; } void ISample.MyMethod(ref int[] array) { this.MyMethod(array); } } public interface ISample { void MyMethod([In] ref int[] array); } }

解决方案

CLS compliance only applies to the visible part of your class. Therefore, you'd think that the ref int[] is not public and therefore not relevant. But it is visible, through the interface.

The users of your code know that Sample provides void MyMethod(int[]). They also know that it implements ISample which provides void MyMethod(ref int[]). Therefore, I believe it is in fact not CLS-Compliant.


EDIT: Eric Lippert has commented on the original question that he believes this is in fact a compiler bug and that the original code is CLS-Compliant.


This, however, is valid:

[assembly: CLSCompliant(true)] namespace MyNamespace { public class Sample : ISample, ISample2 { void ISample.MyMethod(ref int[] array) { } void ISample2.MyMethod(int[] array) { } } public interface ISample { void MyMethod(ref int[] array); } public interface ISample2 { void MyMethod(int[] array); } }

That is because CLS defines that two interface may define conflicting methods with the same name or signature and the compiler must know how to tell the difference - but again, only when the conflict is between two interfaces.

更多推荐

警告CS3006在这种情况下,有效吗?

本文发布于:2023-11-13 06:14:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在这种情况下

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!