COMImport
attributeWhen placed on a class, the COMImport
attribute marks the class as an externally implemented COM class. Such a class declaration enables the use of a C# name to refer to a COM class.
[AttributeUsage(AttributeTargets.Class)] public class COMImportAttribute: System.Attribute { public COMImportAttribute() {…} }
A class that is decorated with the COMImport
attribute is subject to the following restrictions:
Guid
attribute, which specifies the CLSID for the COM class being imported. A compile-time error occurs if a class declaration includes the COMImport
attribute but fails to include the Guid
attribute.object
.The example
[COMImport, Guid("00020810-0000-0000-C000-000000000046")] class Worksheet {} class Test { static void Main() { Worksheet w = new Worksheet(); // Creates an Excel worksheet } }
declares a class Worksheet as a class imported from COM that has a CLSID of "00020810-0000-0000-C000-000000000046
". Instantiating a Worksheet
instance causes a corresponding COM instantiation.