Microsoft Learn

ComVisibleAttribute Class

Namespace: System.Runtime.InteropServices

Assembly: System.Runtime.InteropServices.dll

Syntax
[System.AttributeUsageAttribute((System.AttributeTargets)(32 | 3072 | 4096 | 262144), Inherited=true)]
public sealed class ComVisibleAttribute : Attribute

Description

Indicates whether COM types are visible to unmanaged clients. This attribute can be applied to assemblies, modules, interfaces, classes, and structures.

By default, types defined in managed assemblies are not visible to COM clients. To make a managed type visible to COM, you must apply the ComVisibleAttribute with its value set to true.

Members

Constructors

  • ComVisibleAttribute(bool visible): Initializes a new instance of the ComVisibleAttribute class with the specified visibility setting.

Properties

  • Visible: Gets a value indicating whether the COM type is visible to unmanaged clients.

Example

// Make the class visible to COM
[ComVisible(true)]
public class MyComVisibleClass
{
    public void DoSomething()
    {
        // COM-callable method implementation
    }
}

// By default, this class is not visible to COM
public class MyInvisibleClass
{
    public void DoSomethingElse()
    {
        // Not callable from COM
    }
}

Remarks

When you set ComVisibleAttribute(true) on an assembly, all types within that assembly are marked as visible to COM by default. You can then selectively hide specific types by applying ComVisibleAttribute(false) to them.

Conversely, if an assembly is not marked as COM-visible (either by default or by explicitly setting ComVisibleAttribute(false)), you must explicitly mark each type that you want to expose to COM with ComVisibleAttribute(true).

The Inherited property of the AttributeUsageAttribute specifies whether the attribute is inherited by derived classes. In this case, it is set to true, meaning that if a base class is marked as COM-visible, its derived classes will also be considered COM-visible unless explicitly marked otherwise.

The AttributeTargets enumeration specifies the types of program elements to which an attribute can be applied. The values 32 (Assembly), 3072 (Interface), 4096 (Class), and 262144 (Struct) indicate that this attribute can be applied to assemblies, interfaces, classes, and structures.

Requirements

Namespace
System.Runtime.InteropServices
Assembly
System.Runtime.InteropServices.dll

See Also