Export (0) Print
Expand All

ClassInterfaceAttribute Class

Indicates the type of class interface to be generated for a class exposed to COM, if an interface is generated at all.

For a list of all members of this type, see ClassInterfaceAttribute Members.

System.Object
   System.Attribute
      System.Runtime.InteropServices.ClassInterfaceAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Assembly Or _
   AttributeTargets.Class)>
NotInheritable Public Class ClassInterfaceAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Assembly |
   AttributeTargets.Class)]
public sealed class ClassInterfaceAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Assembly |
   AttributeTargets::Class)]
public __gc __sealed class ClassInterfaceAttribute : public
   Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class)
class ClassInterfaceAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You can apply this attribute to assemblies or classes.

This attribute controls whether the Type Library Exporter (Tlbexp.exe) automatically generates a class interface for the attributed class. A class interface carries the same name as the class itself, but the name is prefixed with an underscore. When exposed, the class interface contains all the public, non- static (Shared in Visual Basic) members of the managed class, including members inherited from its base class. Managed classes cannot access a class interface and have no need to as they can access the class members directly. Tlbexp.exe generates a unique interface identifier (IID) for the class interface.

Class interfaces can be dual or dispatch-only interfaces. Optionally, you can suppress the generation of the class interface and provide a custom interface instead. You expose or suppress a class interface by specifying a System.Runtime.InteropServices.ClassInterfaceType enumeration member. When you apply ClassInterfaceAttribute to an assembly, the attribute pertains to all classes in the assembly unless the individual classes override the setting with their own attribute.

Although class interfaces eliminate the task of explicitly defining interfaces for each class, their use in production applications is strongly discouraged. Dual class interfaces allow clients to bind to a specific interface layout that is subject to change as the class evolves. For example, consider a managed class that exposes a class interface to COM clients. The first version of the class contains methods North and South. An unmanaged client can bind to the class interface, which provides North as the first method in the class interface and method South as the second method. Now consider the next version of the class, which has a new method, East, inserted between methods North and South. Unmanaged clients that try to bind to the new class through the old class interface end up calling method East when they intend to call method South, because the positioning of methods within the interface has changed. Moreover, any change to the layout of a base class also affects the layout of the class interface for all derived classes. Managed clients, which bind directly to classes, do not exhibit the same versioning problems. For specific guidelines on using a class interface, see Introducing the Class Interface.

The Type Library Importer (Tlbimp.exe) always applies to imported classes the ClassInterfaceType.None enumeration member to indicate that existing COM classes never expose managed interfaces.

Example

[Visual Basic, C#, C++] The following example shows how to apply the ClassInterfaceAttribute with the ClassInterfaceType value AutoDispatch, which generates an IDispatch interface for MyClass.

[Visual Basic] 
Imports System.Runtime.InteropServices

<ClassInterface(ClassInterfaceType.AutoDispatch)> _
Public Class SampleClass    
    ' Insert class members here.
End Class

[C#] 
using System.Runtime.InteropServices;

[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class MyClass
{
   public MyClass() {}
}

[C++] 
using namespace System::Runtime::InteropServices;

[ClassInterface(ClassInterfaceType::AutoDispatch)]
public __gc class MyClass
{
public:
   MyClass() {}
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

ClassInterfaceAttribute Members | System.Runtime.InteropServices Namespace | ClassInterfaceType | Type Library Exporter (Tlbexp.exe) | Type Library Importer (Tlbimp.exe)

Show:
© 2014 Microsoft