警告CS3006在这种情况下,有效吗? [英] Is warning CS3006 valid in this case?

查看:309
本文介绍了警告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在这种情况下,有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆