ComVisibleAttribute Classe

Definizione

Controlla l'accessibilità di un singolo tipo gestito o un membro, oppure di tutti i tipi all'interno di un assembly, per COM.

public ref class ComVisibleAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)]
public sealed class ComVisibleAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ComVisibleAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)>]
type ComVisibleAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Field | System.AttributeTargets.Interface | System.AttributeTargets.Method | System.AttributeTargets.Property | System.AttributeTargets.Struct, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ComVisibleAttribute = class
    inherit Attribute
Public NotInheritable Class ComVisibleAttribute
Inherits Attribute
Ereditarietà
ComVisibleAttribute
Attributi

Esempio

Nell'esempio seguente viene illustrato come controllare la visibilità su COM di una classe in modo che i relativi membri siano invisibili. Impostando ComVisibleAttributefalse su su MyClass(SampleClass nell'esempio di Visual Basic) e su MyMethod e falseMyProperty, è possibile evitare inavvertitamente di esporre i membri a COM tramite ereditarietà.

using namespace System::Runtime::InteropServices;

[ComVisible(false)]
ref class MyClass
{
private:
   int myProperty;

public:
   MyClass()
   {
      
      //Insert code here.
   }


   [ComVisible(false)]
   int MyMethod( String^ param )
   {
      return 0;
   }

   bool MyOtherMethod()
   {
      return true;
   }


   property int MyProperty 
   {

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

   }

};
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;
      }
   }
}
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

Commenti

È possibile applicare questo attributo ad assembly, interfacce, classi, strutture, delegati, enumerazioni, campi, metodi, funzioni di accesso agli eventi o proprietà.

Il valore predefinito è true, che indica che il tipo gestito è visibile a COM. Questo attributo non è necessario per rendere visibili gli assembly e i tipi gestiti pubblici; sono visibili a COM per impostazione predefinita. Solo public i tipi possono essere resi visibili. L'attributo non può essere utilizzato per rendere visibile un tipo in caso contrario internal a COM o protected per rendere visibili i membri di un tipo non visibile.

L'impostazione dell'attributo su false nell'assembly nasconde tutti i public tipi all'interno dell'assembly. È possibile rendere visibili in modo selettivo i tipi all'interno dell'assembly impostando i singoli tipi su true. L'impostazione dell'attributo su false su un tipo specifico nasconde tale tipo e i relativi membri. Tuttavia, non è possibile rendere visibili i membri di un tipo se il tipo è invisibile. L'impostazione dell'attributo su false su su un tipo impedisce l'esportazione del tipo in una libreria dei tipi; le classi non sono registrate; le interfacce non rispondono mai alle chiamate non gestite QueryInterface .

A meno che non si imposti in modo esplicito una classe e i relativi membri su false, le classi ereditate possono esporre ai membri della classe di base COM invisibili nella classe originale. Ad esempio, se si imposta ClassA su false e non si applica l'attributo ai relativi membri, la classe e i relativi membri sono invisibili a COM. Tuttavia, se si deriva ClassB da ClassA ed esporta ClassB in COM, i membri ClassA diventano membri della classe base visibili di ClassB.

Per una descrizione dettagliata del processo di esportazione, vedere Riepilogo conversione da assembly a libreria dei tipi.

Costruttori

ComVisibleAttribute(Boolean)

Inizializza una nuova istanza della classe ComVisibleAttribute.

Proprietà

TypeId

Quando è implementata in una classe derivata, ottiene un identificatore univoco della classe Attribute.

(Ereditato da Attribute)
Value

Ottiene un valore che indica se il tipo COM è visibile.

Metodi

Equals(Object)

Restituisce un valore che indica se questa istanza è uguale a un oggetto specificato.

(Ereditato da Attribute)
GetHashCode()

Restituisce il codice hash per l'istanza.

(Ereditato da Attribute)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
IsDefaultAttribute()

In caso di override in una classe derivata, indica se il valore di questa istanza è il valore predefinito per la classe derivata.

(Ereditato da Attribute)
Match(Object)

Quando è sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza equivale a un oggetto specificato.

(Ereditato da Attribute)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch.

(Ereditato da Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Recupera le informazioni sul tipo relative a un oggetto, che possono essere usate per ottenere informazioni sul tipo relative a un'interfaccia.

(Ereditato da Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1).

(Ereditato da Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fornisce l'accesso a proprietà e metodi esposti da un oggetto.

(Ereditato da Attribute)

Si applica a

Vedi anche