Share via


Applying Interop Attributes

The System.Runtime.InteropServices namespace provides three categories of interop-specific attributes: those applied by you at design time, those applied by COM interop tools and APIs during the conversion process, and those applied either by you or COM interop.

If you are unfamiliar with the task of applying attributes to managed code, see Extending Metadata Using Attributes. Like other custom attributes, you can apply interop-specific attributes to types, methods, properties, parameters, fields, and other members.

Design-Time Attributes

You can adjust the outcome of the conversion process performed by COM interop tools and APIs by using design-time attributes. The following table describes the attributes that you can apply to your managed source code. COM interop tools, on occasion, might also apply the attributes described in this table.

Attribute

Description

AutomationProxyAttribute

Specifies whether the type should be marshaled using the Automation marshaler or a custom proxy and stub.

ClassInterfaceAttribute

Controls the type of interface generated for a class.

CoClassAttribute

Identifies the CLSID of the original coclass imported from a type library.

COM interop tools typically apply this attribute.

ComImportAttribute

Indicates that a coclass or interface definition was imported from a COM type library. The runtime uses this flag to know how to activate and marshal the type. This attribute prohibits the type from being exported back to a type library.

COM interop tools typically apply this attribute.

ComRegisterFunctionAttribute

Indicates that a method should be called when the assembly is registered for use from COM, so that user-written code can be executed during the registration process.

ComSourceInterfacesAttribute

Identifies interfaces that are sources of events for the class.

COM interop tools can apply this attribute.

ComUnregisterFunctionAttribute

Indicates that a method should be called when the assembly is unregistered from COM, so that user-written code can execute during the process.

ComVisibleAttribute

Renders types invisible to COM when the attribute value equals false. This attribute can be applied to an individual type or to an entire assembly to control COM visibility. By default, all managed, public types are visible; the attribute is not needed to make them visible.

DispIdAttribute

Specifies the COM dispatch identifier (DISPID) of a method or field. This attribute contains the DISPID for the method, field, or property it describes.

COM interop tools can apply this attribute.

FieldOffsetAttribute

Indicates the physical position of each field within a class when used with the StructLayoutAttribute, and the LayoutKind is set to Explicit.

GuidAttribute

Specifies the globally unique identifier (GUID) of a class, interface, or an entire type library. The string passed to the attribute must be a format that is an acceptable constructor argument for the type System.Guid.

COM interop tools can apply this attribute.

IDispatchImpAttribute

Indicates which IDispatch interface implementation the common language runtime uses when exposing dual interfaces and dispinterfaces to COM.

InAttribute

Indicates that data should be marshaled in to the caller. Can be used to attribute parameters.

InterfaceTypeAttribute

Controls how a managed interface is exposed to COM clients (Dual, IUnknown-derived, or IDispatch only).

COM interop tools can apply this attribute.

LCIDConversionAttribute

Indicates that an unmanaged method signature expects an LCID parameter.

COM interop tools can apply this attribute.

MarshalAsAttribute

Indicates how the data in fields or parameters should be marshaled between managed and unmanaged code. The attribute is always optional because each data type has default marshaling behavior.

COM interop tools can apply this attribute.

OptionalAttribute

Indicates that a parameter is optional.

COM interop tools can apply this attribute.

OutAttribute

Indicates that the data in a field or parameter must be marshaled from a called object back to its caller.

PreserveSigAttribute

Suppresses the HRESULT or retval signature transformation that normally takes place during interoperation calls. The attribute affects marshaling as well as type library exporting.

COM interop tools can apply this attribute.

ProgIdAttribute

Specifies the ProgID of a .NET Framework class. Can be used to attribute classes.

StructLayoutAttribute

Controls the physical layout of the fields of a class.

COM interop tools can apply this attribute.

Conversion-Tool Attributes

The following table describes attributes that COM interop tools apply during the conversion process. You do not apply these attributes at design time.

Attribute

Description

ComAliasNameAttribute

Indicates the COM alias for a parameter or field type. Can be used to attribute parameters, fields, or return values.

ComConversionLossAttribute

Indicates that information about a class or interface was lost when it was imported from a type library to an assembly.

ComEventInterfaceAttribute

Identifies the source interface and the class that implements the methods of the event interface.

ImportedFromTypeLibAttribute

Indicates that the assembly was originally imported from a COM type library. This attribute contains the type library definition of the original type library.

TypeLibFuncAttribute

Contains the FUNCFLAGS that were originally imported for this function from the COM type library.

TypeLibTypeAttribute

Contains the TYPEFLAGS that were originally imported for this type from the COM type library.

TypeLibVarAttribute

Contains the VARFLAGS that were originally imported for this variable from the COM type library.

See Also

Reference

System.Runtime.InteropServices

Concepts

Exposing .NET Framework Components to COM

Qualifying .NET Types for Interoperation

Packaging an Assembly for COM

Other Resources

Extending Metadata Using Attributes