1

好的,我很困惑:当一个类的唯一构造函数有一个System.TimeSpan参数并且类继承自时,为什么类不符合 CLS System.Attribute

System.Attribute这是一个 min-repro,以及两者System.TimeSpan 符合 CLS 的证明:

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

(repros with any /langversion:1..5)

4

0 回答 0