The CLSCompliant Attribute

The System.CLSCompliantAttribute attribute is applied to program elements to indicate CLS compliance. An assembly that is CLS-compliant must specify the following global attribute:

[assembly:CLSCompliant(true)]

An assembly that doesn’t explicitly declare CLS compliance is assumed to be noncompliant. For multifile assemblies, CLS compliance needs to be declared only once. If the CLSCompliantAttribute attribute is specified multiple times with different values, a compile-time error is generated.

There is no need to specify the CLSCompliant attribute on every program element. A program element is assumed to have the same CLS compliance as its containing element. For example, a class defined within a compliant assembly is assumed to be compliant, as is a member of a compliant class. An element cannot be marked as CLS-compliant if its enclosing element isn’t compliant.

Noncompliant elements contained within compliant elements must be marked as such using the CLSCompliant attribute with a false argument—for example:

[CLSCompliant(false)]

A compiler error is raised if the compiler determines that a program element marked as CLS-compliant is actually not compliant.

The following example demonstrates the use of the CLSCompliant attribute to identify various CLS-compliant program elements:

using System;

[assembly:CLSCompliant(true)]

public class MyClass {

    [CLSCompliant(true)]
    protected void MethodA(int a, params int[] b) {
        // method implementation code
    }

    [CLSCompliant(false)]
    protected void MethodB(uint a) {
        // method implementation code
    }

    [CLSCompliant(false)]
    protected void methodC(byte a) {
        // method implementation code
    }

    [CLSCompliant(false)]
    protected void MethodC(byte a) {
        // method implementation code
    }

    private ushort MethodD(sbyte a) {
        // method implementation code
        return 0;
    }

    public static int Main(String[] args) {
        // Main method implementation code
        return 0;
    }
}

The assembly resulting from the compilation of this code is identified as CLS-compliant using the global attribute assembly:CLSCompliant(true). The MyClass type doesn’t have a CLSCompliant attribute specified; it’s assumed to be compliant because it’s a member of a compliant assembly. MethodA is CLS-compliant, while MethodB is not; it takes a uint argument that is non-CLS-compliant.

At least one version of MethodC must be marked as noncompliant, or a compiler error will occur. However, we have marked both versions noncompliant because the CLS specifies that case must not be used to differentiate between program elements. MethodD takes a non-CLS-compliant argument; however, it’s private and so is not bound by the CLS rules. Finally, the Main method has no CLSCompliant attribute but is assumed to be compliant because it’s contained within the compliant MyClass type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset