The CLS specifies how to mark externally visible parts of an assembly to indicate whether or not they comply with the CLS requirements. (Implementers are discouraged from marking extensions to this standard as CLS-compliant.) This is done using the custom attribute mechanism (see §I.9.7 and Partition II). The class System.CLSCompliantAttribute
(see Partition IV) indicates which types and type members are CLS-compliant. It also can be attached to an assembly, to specify the default level of compliance for all top-level types that assembly contains.
The constructor for System.CLSCompliantAttribute
takes a Boolean argument indicating whether the item with which it is associated is CLS-compliant. This allows any item (assembly, type, or type member) to be explicitly marked as CLS-compliant or not.
The rules for determining CLS compliance are:
-
When an assembly does not carry an explicit
System.CLSCompliantAttribute
, it shall be assumed to carrySystem.CLSCompliantAttribute(false)
. -
By default, a type inherits the CLS-compliance attribute of its enclosing type (for nested types) or acquires the level of compliance attached to its assembly (for top-level types). A type can be marked as either CLS-compliant or not CLS-compliant by attaching the
System.CLSCompliantAttribute
attribute. -
By default, other members (methods, fields, properties, and events) inherit the CLS-compliance of their type. They can be marked as not CLS-compliant by attaching the attribute
System.CLSCompliantAttribute(false)
.
Members of non-CLS compliant types shall not be marked CLS-compliant.
[Note:
CLS (consumer): Can ignore any member that is not CLS-compliant using the above rules.
CLS (extender): Should encourage correct labeling of newly authored assemblies and publicly exported types and members. Compile-time enforcement of the CLS rules is strongly encouraged.
CLS (framework): Shall correctly label all publicly exported members as to their CLS compliance. The rules specified here can be used to minimize the number of markers required (for example, label the entire assembly if all types and members are compliant or if there are only a few exceptions that need to be marked). end note]