Marking Assemblies and Types as CLS-Compliant

Chapter 2, “Getting Started with the Visual Studio 2010 IDE,” and Chapter 3, “The Anatomy of a Visual Basic Project,” offer an overview of assemblies. When you produce a reusable component such as a class library or a user control, such as a .dll assembly, you need to ensure that it is CLS-compliant. You can add the following attribute to the assembly definition:

<Assembly: CLSCompliant(True)>

This attribute tells the compiler to check if a type used in your code is CLS-compliant. If the compiler finds a CLS-incompliant type, it throws a warning message. Assembly members should also be marked as CLS-compliant, if you plan that they will be. For example, a class is defined CLS-compliant as follows:

<CLSCompliant(True)> Public Class Person

You might wonder why you should add this attribute at the class level if you specified one at the assembly level. The reason is that you might implement CLS-incompliant classes (therefore assigning False to the CLSCompliant attribute), and this is useful for communicating both the compiler and code analysis tools that a CLS-incompliant class should not be checked.

Code Analysis

There are different code analysis tools for checking if code is CLS-compliant. The first one is the well-known Microsoft FxCop, which can be installed by double-clicking the same-name shortcut in the Windows SDK subfolder of Windows’s Start Menu. The second one is the code analysis instrumentation available in Visual Studio Team System, which is covered in Chapter 58, “Advanced Analysis Tools.” Both tools are important for finding errors about CLS compliance of your code.

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

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