Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would we want to apply the ClassInterface attribute to a class?

Tags:

c#

.net

[ClassInterface(ClassInterfaceType.None)]
Class Program()
{
}

When would we need to apply the ClassInterface attribute to a class, as is done here?

like image 639
Adam Lee Avatar asked Sep 16 '12 23:09

Adam Lee


1 Answers

ClassInterfaceAttribute is used for declaring how visible your class is to COM callers, that is, if your class will play nice with something from the COM world trying to use your class. There are three options, contained in the ClassInterfaceType enumeration, which you can specify as a parameter of the overload of the ClassInterfaceAttribute. Here it is from MSDN, and below is an example of three class declarations, each with a different choice of InterfaceType:

ClassInterfaceAttribute on MSDN

ClassInterfaceType on MSDN

The following illustrates use of the ClassInterfaceAttribute:

// This one will not be visible to COM clients.
[ClassInterface(ClassInterfaceType.None)]
public class MyClass1
{

}

// This one will provide an interface for COM clients, 
// but only when/if one is requested of it.
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class MyClass2
{

}

// This one will, immediately upon instantiation, 
// automatically include an interface for COM clients.
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyClass3
{

}
like image 56
dylansweb Avatar answered Nov 11 '22 05:11

dylansweb