在这种情况下警告 CS3006 有效吗?
下面的代码生成警告 CS3006“重载方法 MyNamespace.Sample.MyMethod(int[])' 仅在 ref 或 out 或数组等级上不同,不符合 CLS”。
此警告是否有效,即这是否真的不符合 CLS? 我原以为显式接口实现不会算作重载。
[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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CLS 合规性仅适用于您班级的可见部分。 因此,您可能会认为
ref int[]
不是public
,因此不相关。 但它是通过界面可见的。代码的用户知道
Sample
提供void MyMethod(int[])
。 他们还知道它实现了提供void MyMethod(ref int[])
的ISample
。 因此,我认为它实际上不符合 CLS。编辑: Eric Lippert 对最初的问题发表了评论,他认为这实际上是一个编译器错误,并且原始代码符合 CLS。
然而,这是有效的:
这是因为 CLS 定义两个接口可能定义具有相同名称或签名的冲突方法,并且编译器必须知道如何区分差异 - 但同样,只有当冲突发生在两个接口之间时。
CLS compliance only applies to the visible part of your class. Therefore, you'd think that the
ref int[]
is notpublic
and therefore not relevant. But it is visible, through the interface.The users of your code know that
Sample
providesvoid MyMethod(int[])
. They also know that it implementsISample
which providesvoid 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:
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.