题
下面的代码生成警告CS3006“重载方法MyNamespace.Sample.MyMethod(INT [])”仅在参考文献或不同,或在阵列等级,是不符合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);
}
}
解决方案
CLS遵守仅适用于您的类的可见部分。因此,你会觉得ref int[]
不public
,因此不相关。但它是可见的,通过该接口。
你的代码的用户都知道,Sample
提供void MyMethod(int[])
。他们也知道,它实现ISample
提供void MyMethod(ref int[])
。因此,相信它是实际上不符合CLS。
编辑:埃里克利珀已经评论了原来的问题,他认为这是事实上编译器故障和原始代码是符合CLS的。
此,但是,是有效的:
[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);
}
}
这是因为CLS定义了两种接口可以定义矛盾的方法具有相同的名称或签名,编译器必须知道如何分辨出来 - 但同样,只有当冲突是两个接口之间
不隶属于 StackOverflow