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

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

When we need to apply the ClassInterface attribute to a class, how is this done here?

+4
source share
1 answer

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

ClassInterfaceAttribute on MSDN

ClassInterfaceType on MSDN

The following shows the use of 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 { } 
+3
source

Source: https://habr.com/ru/post/1434504/


All Articles