Information Technology Reference
In-Depth Information
MyClass has a CLS-compliant public interface. Clients expecting the IFoo2
interface must access it through the non-CLS-compliant IFoo2 pointer.
Complicated? No, not really. Creating a CLS-compliant type mandates that
your public interfaces contain only CLS-compliant types. It means that
your base class must be CLS compliant. All interfaces that you implement
publicly must be CLS compliant. If you implement a non-CLS compliant
interface, you must hide it from your public interface using explicit inter-
face implementation.
CLS compliance does not force you to adopt a least common denomina-
tor approach to your designs and implementations. It means carefully
watching the publicly accessible interfaces of your assembly. For any pub-
lic or protected class, any type mentioned in these constructs must be CLS
compliant:
Base classes
■
Return values for public and protected methods and properties
■
Parameters for public and protected methods and indexers
■
Runtime event arguments
■
Public interfaces, declared or implemented
■
The compiler tries to enforce a compliant assembly. That makes it easy for
you to provide some minimum level of CLS support. With a bit of extra
care, you can create an assembly that anyone using any language can use.
The CLS specification tries to ensure that language interoperability is pos-
sible without sacrificing the constructs in your favorite language. You just
need to provide alternatives in the interface.
CLS compliance requires you to spend a little time thinking about the pub-
lic interfaces from the standpoint of other languages. You don't need to
restrict all your code to CLS-compliant constructs; just avoid the non-
compliant constructs in the interface. The payback of interlanguage oper-
ability is worth the extra time.
This item should really be titled “Build Assemblies That Are the Right Size
and Contain a Small Number of Public Types.” But that's too wordy, so I
titled it based on the most common mistake I see: developers putting
everything but the kitchen sink in one assembly. That makes it hard to