.NET Framework Class Library
ClassInterfaceAttribute Class

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

Inheritance Hierarchy
SystemObject
   SystemAttribute
    System.Runtime.InteropServicesClassInterfaceAttribute

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_0_0_0%]([%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_0_0_1%].Assembly Or [%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_0_0_2%].Class, Inherited := False)> _
<[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_0_0_3%](True)> _
Public NotInheritable Class ClassInterfaceAttribute _
	Inherits [%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_0_0_4%]
[[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_1_0_0%]([%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_1_0_1%].Assembly|[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_1_0_2%].Class, Inherited = false)]
[[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_1_0_3%](true)]
public sealed class ClassInterfaceAttribute : [%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_1_0_4%]
[[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_2_0_0%]([%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_2_0_1%]::Assembly|[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_2_0_2%]::Class, Inherited = false)]
[[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_2_0_3%](true)]
public ref class ClassInterfaceAttribute sealed : public [%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_2_0_4%]
[<[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_3_0_0%]>]
[<[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_3_0_1%]([%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_3_0_2%].Assembly|[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_3_0_3%].Class, Inherited = false)>]
[<[%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_3_0_4%](true)>]
type ClassInterfaceAttribute =  
    class 
        inherit [%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_3_0_5%] 
    end
public final class ClassInterfaceAttribute extends [%$TOPIC/2b5k2e6k_en-us_VS_110_2_0_4_0_0%]

The ClassInterfaceAttribute type exposes the following members.

Constructors
  NameDescription
Public method Supported by Portable Class Library ClassInterfaceAttribute(ClassInterfaceType)Initializes a new instance of the ClassInterfaceAttribute class with the specified ClassInterfaceType enumeration member.
Public method Supported by Portable Class Library ClassInterfaceAttribute(Int16)Initializes a new instance of the ClassInterfaceAttribute class with the specified ClassInterfaceType enumeration value.
Top
Properties
  NameDescription
Public property TypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Public property Supported by Portable Class Library ValueGets the ClassInterfaceType value that describes which type of interface should be generated for the class.
Top
Methods
  NameDescription
Public method Supported by Portable Class Library EqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public method Supported by Portable Class Library GetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public method Supported by Portable Class Library GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public method MatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public method Supported by Portable Class Library ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method _AttributeGetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeInvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top
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 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.InteropServicesClassInterfaceType 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 Tlbimp.exe (Type Library Importer) always applies to imported classes the ClassInterfaceTypeNone enumeration member to indicate that existing COM classes never expose managed interfaces.

Examples

The following example shows how to apply the ClassInterfaceAttribute with the ClassInterfaceType value AutoDispatch, which generates an IDispatch interface for MyClass.

Imports System.Runtime.InteropServices

<ClassInterface(ClassInterfaceType.AutoDispatch)> _
Public Class SampleClass    
    ' Insert class members here. 
End Class
using System.Runtime.InteropServices;

[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class MyClass
{
   public MyClass() {}
}
using namespace System::Runtime::InteropServices;

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

};
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
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.