문제

아래 코드는 경고 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);
    }
}
도움이 되었습니까?

해결책

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는 두 인터페이스가 동일한 이름 또는 서명으로 충돌 방법을 정의 할 수 있다고 정의하기 때문에 컴파일러는 차이를 알리는 방법을 알아야하지만, 충돌이 두 인터페이스 사이에있을 때만 다시 한 번.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top