Common Language Specification Compliance Checking
The Common Language Specification (CLS) is a set of rules intended to promote language interoperability. For a framework to be usable in multiple languages, it is essential that it conforms to these rules. In order for a given type to be used by other languages, it must be compliant with the CLS. Compliance with the CLS means avoiding language constructs that are not shared by all languages. Lack of compliance with the CLS means that a type is not guaranteed to be usable from all languages.
In Visual J# 2005, the compiler provides support for checking types for compliance with the Common Language Specification (CLS). For more information on the CLS, see What is the Common Language Specification?.
A type that is intended to be used by another language should be marked with the CLSCompliant attribute. Also, an assembly that contains CLS compliant types should be marked with the assembly-level CLSCompliant attribute. The compiler will check the CLS compliance of any public type marked with the CLSCompliant attribute in an assembly that is marked CLS compliant.
// MyClass.jsl
// compile with: /target:library
// Assembly-level CLS Compliant attribute:
/** @assembly System.CLSCompliant(true) */
// Class-level CLS Compliant attribute:
/** @attribute System.CLSCompliant(true) */
public class MyClass
{
public void method1()
{
}
public void method2()
{
}
}
The CLSCompliant attribute should not be used on classes without the assembly-level CLSCompliant attribute, since they will not be checked for CLS compliance unless the assembly-level attribute is present.
The CLSCompliant attribute may be used with the false argument. This should be used for a class that is not CLS compliant in an assembly that is marked as CLS compliant. Marking a class with CLSComplaint(false)
indicates that the class is not intended to be used in other languages and should not be checked for CLS compliance.
If the assembly-level attribute is set to 'false', CLS compliance checking will only be done for those types in the assembly that have been specifically marked with the CLSCompliant attribute set to 'true'.
Only public types should be marked as CLSCompliant(true)
. Internal types may not be used by other languages, so CLS rules do not apply to such types.
For a listing of CLS Rules enforced by the Visual J# compiler, see What is the Common Language Specification?.