构造函数不符合CLS,与TimeSpan参数兼容并继承Attribute

时间:2015-07-13 19:41:44

标签: c# inheritance attributes timespan cls-compliant

好的,我很难过:为什么一个类不符合CLS,当它的唯一构造函数有一个urls.py参数该类继承自System.TimeSpan

这是一个min-repro,另外证明System.AttributeSystem.Attribute 符合 CLS:

System.TimeSpan

编译运行:

using System;

[assembly:CLSCompliant(true)]

namespace Test
{
    public class CLSCompliantClass1
    {
        public TimeSpan Foo; // CLS-compliant
        public CLSCompliantClass1(TimeSpan bar) { } // CLS-compliant
    }

    public class CLSCompliantClass2 : Attribute
    {
        public TimeSpan Foo; // CLS-compliant
        public CLSCompliantClass2(double foo) { } // CLS-compliant
    }

    public class NonCLSCompliantClass : Attribute
    {
        // 'Test.NonCLSCompliantClass' has no accessible constructors which use only CLS-compliant types
        public NonCLSCompliantClass(TimeSpan bar) { } // not CLS-compliant??
    }
}

(任何>csc /t:library cls.cs /warnaserror /nologo cls.cs(19,15): error CS3015: Warning as Error: 'Test.NonCLSCompliantClass' has no accessible constructors which use only CLS-compliant types 的重印)

0 个答案:

没有答案