Export (0) Print
Expand All

ComVisibleAttribute Class

Controls accessibility of an individual managed type or member, or of all types within an assembly, to COM.

For a list of all members of this type, see ComVisibleAttribute Members.

System.Object
   System.Attribute
      System.Runtime.InteropServices.ComVisibleAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Assembly Or AttributeTargets.Class _
   Or AttributeTargets.Struct Or AttributeTargets.Enum Or _
   AttributeTargets.Method Or AttributeTargets.Property Or _
   AttributeTargets.Field Or AttributeTargets.Interface Or _
   AttributeTargets.Delegate)>
NotInheritable Public Class ComVisibleAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
   | AttributeTargets.Struct | AttributeTargets.Enum |
   AttributeTargets.Method | AttributeTargets.Property |
   AttributeTargets.Field | AttributeTargets.Interface |
   AttributeTargets.Delegate)]
public sealed class ComVisibleAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Assembly |
   AttributeTargets::Class | AttributeTargets::Struct |
   AttributeTargets::Enum | AttributeTargets::Method |
   AttributeTargets::Property | AttributeTargets::Field |
   AttributeTargets::Interface | AttributeTargets::Delegate)]
public __gc __sealed class ComVisibleAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class |
   AttributeTargets.Struct | AttributeTargets.Enum |
   AttributeTargets.Method | AttributeTargets.Property |
   AttributeTargets.Field | AttributeTargets.Interface |
   AttributeTargets.Delegate)
class ComVisibleAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You can apply this attribute to assemblies, interfaces, classes, structures, delegates, enumerations, fields, methods, or properties.

The default is true, which indicates that the managed type is visible to COM. This attribute is not needed to make public managed assemblies and types visible; they are visible to COM by default. Only public types can be made visible. The attribute cannot be used to make an otherwise internal or protected type visible to COM or to make members of a nonvisible type visible.

Setting the attribute to false on the assembly hides all public types within the assembly. You can selectively make types within the assembly visible by setting the individual types to true. Setting the attribute to false on a specific type hides that type and its members. However, you cannot make members of a type visible if the type is invisible. Setting the attribute to false on a type prevents that type from being exported to a type library; classes are not registered; interfaces are never responsive to unmanaged QueryInterface calls.

Unless you explicitly set a class and its members to false, inherited classes can expose to COM base class members that are invisible in the original class. For example, if you set ClassA to false and do not apply the attribute to its members, the class and its members are invisible to COM. However, if you derive ClassB from ClassA and export ClassB to COM, ClassA members become visible base class members of ClassB.

For a detailed description of the export process, see Assembly to Type Library Conversion Summary.

Example

The following example shows how you can control the visibility to COM of a class so that its members are invisible. By setting ComVisibleAttribute to false on MyClass, and false on MyMethod and MyProperty, you can avoid inadvertently exposing the members to COM through inheritance.

[Visual Basic] 
Imports System.Runtime.InteropServices

<ComVisible(False)> _
Class SampleClass
    
    Public Sub New()
        'Insert code here.
    End Sub
    
    <ComVisible(False)> _
    Public Function MyMethod(param As String) As Integer
        Return 0
    End Function    
    
    Public Function MyOtherMethod() As Boolean
        Return True
    End Function
    
    <ComVisible(False)> _
    Public ReadOnly Property MyProperty() As Integer
        Get
            Return MyProperty
        End Get
    End Property
    
End Class

[C#] 
using System.Runtime.InteropServices;

[ComVisible(false)]
class MyClass
{
   public MyClass()
   {
      //Insert code here.
   }
   
   [ComVisible(false)]
   public int MyMethod(string param) 
   {
      return 0;
   }

   public bool MyOtherMethod() 
   {
      return true;
   }

   [ComVisible(false)]
   public int MyProperty
   {
      get
      {
         return MyProperty;
      }
   }
}

[C++] 
using namespace System::Runtime::InteropServices;

[ComVisible(false)]
__gc class MyClass
{
private:
   int myProperty;
public:
   MyClass()
   {
      //Insert code here.
   }
   
   [ComVisible(false)]
   int MyMethod(String* param) 
   {
      return 0;
   }

   bool MyOtherMethod() 
   {
      return true;
   }

   [ComVisible(false)]
   __property int get_MyProperty() {
      return myProperty;
   }
};

[JScript] 
import System.Runtime.InteropServices;

ComVisible(false) class MyClass
{
   public function MyClass()
   {
      //Insert code here.
   }
   
   ComVisible(false) public function MyMethod(param : String) : int 
   {
      return 0;
   }

   public function MyOtherMethod() : boolean
   {
      return true;
   }

   ComVisible(false) public function get MyProperty() : int 
   {
       return MyProperty;
   }
}

Requirements

Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

ComVisibleAttribute Members | System.Runtime.InteropServices Namespace | Type Library Exporter (Tlbexp.exe) | Assembly to Type Library Conversion Summary

Show:
© 2014 Microsoft