Information Technology Reference
In-Depth Information
To c r e a t e a C L S - c o m p l i a n t a s s e m b l y, y o u m u s t f o l l o w t w o r u l e s . F i r s t , t h e
type of all parameters and return values from public and protected mem-
bers must be CLS compliant. Second, any non-CLS-compliant public or
protected member must have a CLS-compliant synonym.
The first rule is simple to follow: You can have it enforced by the compiler.
Add the CLSCompliant attribute to your assembly:
[assembly: System.CLSCompliant(true)]
The compiler enforces CLS compliance for the entire assembly. If you write
a public method or property that uses a construct that is not compliant
with CLS, it's an error. That's good because it makes CLS compliance an
easy goal. After turning on CLS compliance, these two definitions won't
compile because unsigned integers are not compliant with CLS:
// Not CLS Compliant, returns unsigned int:
public UInt32 Foo()
{
return foo;
}
// Not CLS compliant, parameter is an unsigned int.
public void Foo2( UInt32 parm)
{
}
Remember that creating a CLS-compliant assembly affects only items that
can be seen outside the current assembly. Foo and Foo2 generate CLS com-
pliance errors when declared either public or protected. However, if Foo and
Foo2 were internal, or private, they could be included in a CLS-compliant
assembly; CLS-compliant interfaces are required only for items that are
exposed outside the assembly.
What about this property? Is it CLS compliant?
public MyClass TheProperty { get ; set ; }
It depends. If MyClass is CLS compliant and indicates that it is CLS com-
pliant, this property is CLS compliant. On the other hand, if MyClass is not
marked as CLS compliant, this property is not CLS compliant. That means
that the earlier TheProperty is CLS compliant only if MyClass resides in a
CLS-compliant assembly.
 
Search WWH ::




Custom Search