Attributes to interoperate with COM components in C#

 

Attributes in C# are used to provide information at design time and as well as at run-time. There are different types of attributes available in C# to interoperate with COM components.

COMImport attribute: COMImport attribute marks the class as an externally implemented COM class and 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() { }

    }

COMVisibility attribute: This attribute is used to specify whether or not a class or interface is visible in COM.

 public class COMVisibilityAttribute: System.Attribute

{

          public COMVisibilityAttribute(System.Interop.ComVisibility value) {}

          public ComVisibilityAttribute Value { get {} }

}

There are many other attributes. Those are

  • The DispId attribute
  • The DllImport attribute
  • The GlobalObject attribute
  • The Guid attribute
  • The HasDefaultInterface attribute
  • The ImportedFromCOM attribute
  • The In and Out attributes
  • The InterfaceType attribute
  • The IsCOMRegisterFunction attribute
  • The Marshal attribute
  • The Name attribute
  • The NoIDispatch attribute
  • The NonSerialized attribute
  • The Predeclared attribute
  • The ReturnsHResult attribute
  • The Serializable attribute
  • The StructLayout attribute
  • The StructOffset attribute
  • The TypeLibFunc attribute
  • The TypeLibType attribute
  • The TypeLibVar attribute

 

All these above attributes are used to interoperate with COM components from C#.