Leggi in inglese

Condividi tramite


MemberInfo Classe

Definizione

Ottiene informazioni sugli attributi di un membro e fornisce l'accesso ai metadati dei membri.

C#
public abstract class MemberInfo
C#
public abstract class MemberInfo : System.Reflection.ICustomAttributeProvider
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
public abstract class MemberInfo : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._MemberInfo
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class MemberInfo : System.Reflection.ICustomAttributeProvider, System.Runtime.InteropServices._MemberInfo
Ereditarietà
MemberInfo
Derivato
Attributi
Implementazioni

Esempio

In questo esempio viene illustrato come usare varie classi di reflection per analizzare i metadati contenuti in un assembly.

C#
using System;
using System.Reflection;

class Module1
{
    public static void Main()
    {
        // This variable holds the amount of indenting that
        // should be used when displaying each line of information.
        Int32 indent = 0;
        // Display information about the EXE assembly.
        Assembly a = typeof(Module1).Assembly;
        Display(indent, "Assembly identity={0}", a.FullName);
        Display(indent+1, "Codebase={0}", a.CodeBase);

        // Display the set of assemblies our assemblies reference.

        Display(indent, "Referenced assemblies:");
        foreach (AssemblyName an in a.GetReferencedAssemblies() )
        {
             Display(indent + 1, "Name={0}, Version={1}, Culture={2}, PublicKey token={3}", an.Name, an.Version, an.CultureInfo.Name, (BitConverter.ToString (an.GetPublicKeyToken())));
        }
        Display(indent, "");

        // Display information about each assembly loading into this AppDomain.
        foreach (Assembly b in AppDomain.CurrentDomain.GetAssemblies())
        {
            Display(indent, "Assembly: {0}", b);

            // Display information about each module of this assembly.
            foreach ( Module m in b.GetModules(true) )
            {
                Display(indent+1, "Module: {0}", m.Name);
            }

            // Display information about each type exported from this assembly.

            indent += 1;
            foreach ( Type t in b.GetExportedTypes() )
            {
                Display(0, "");
                Display(indent, "Type: {0}", t);

                // For each type, show its members & their custom attributes.

                indent += 1;
                foreach (MemberInfo mi in t.GetMembers() )
                {
                    Display(indent, "Member: {0}", mi.Name);
                    DisplayAttributes(indent, mi);

                    // If the member is a method, display information about its parameters.

                    if (mi.MemberType==MemberTypes.Method)
                    {
                        foreach ( ParameterInfo pi in ((MethodInfo) mi).GetParameters() )
                        {
                            Display(indent+1, "Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name);
                        }
                    }

                    // If the member is a property, display information about the property's accessor methods.
                    if (mi.MemberType==MemberTypes.Property)
                    {
                        foreach ( MethodInfo am in ((PropertyInfo) mi).GetAccessors() )
                        {
                            Display(indent+1, "Accessor method: {0}", am);
                        }
                    }
                }
                indent -= 1;
            }
            indent -= 1;
        }
    }

    // Displays the custom attributes applied to the specified member.
    public static void DisplayAttributes(Int32 indent, MemberInfo mi)
    {
        // Get the set of custom attributes; if none exist, just return.
        object[] attrs = mi.GetCustomAttributes(false);
        if (attrs.Length==0) {return;}

        // Display the custom attributes applied to this member.
        Display(indent+1, "Attributes:");
        foreach ( object o in attrs )
        {
            Display(indent+2, "{0}", o.ToString());
        }
    }

    // Display a formatted string indented by the specified amount.
    public static void Display(Int32 indent, string format, params object[] param)

    {
        Console.Write(new string(' ', indent*2));
        Console.WriteLine(format, param);
    }
}

//The output shown below is abbreviated.
//
//Assembly identity=ReflectionCS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
//  Codebase=file:///C:/Documents and Settings/test/My Documents/Visual Studio 2005/Projects/Reflection/Reflection/obj/Debug/Reflection.exe
//Referenced assemblies:
//  Name=mscorlib, Version=2.0.0.0, Culture=, PublicKey token=B7-7A-5C-56-19-34-E0-89
//
//Assembly: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//  Module: mscorlib.dll
//
//  Type: System.Object
//    Member: GetType
//    Member: ToString
//    Member: Equals
//      Parameter: Type=System.Object, Name=obj
//    Member: Equals
//      Parameter: Type=System.Object, Name=objA
//      Parameter: Type=System.Object, Name=objB
//    Member: ReferenceEquals
//      Attributes:
//        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
//      Parameter: Type=System.Object, Name=objA
//      Parameter: Type=System.Object, Name=objB
//    Member: GetHashCode
//    Member: .ctor
//      Attributes:
//        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
//
//  Type: System.ICloneable
//    Member: Clone
//
//  Type: System.Collections.IEnumerable
//    Member: GetEnumerator
//      Attributes:
//        System.Runtime.InteropServices.DispIdAttribute
//
//  Type: System.Collections.ICollection
//    Member: CopyTo
//      Parameter: Type=System.Array, Name=array
//      Parameter: Type=System.Int32, Name=index
//    Member: get_Count
//    Member: get_SyncRoot
//    Member: get_IsSynchronized
//    Member: Count
//      Accessor method: Int32 get_Count()
//    Member: SyncRoot
//      Accessor method: System.Object get_SyncRoot()
//    Member: IsSynchronized
//      Accessor method: Boolean get_IsSynchronized()
//
//  Type: System.Collections.IList
//    Member: get_Item
//      Parameter: Type=System.Int32, Name=index
//    Member: set_Item
//      Parameter: Type=System.Int32, Name=index
//      Parameter: Type=System.Object, Name=value
//    Member: Add
//      Parameter: Type=System.Object, Name=value
//    Member: Contains
//      Parameter: Type=System.Object, Name=value
//    Member: Clear
//    Member: get_IsReadOnly
//    Member: get_IsFixedSize
//    Member: IndexOf
//      Parameter: Type=System.Object, Name=value
//    Member: Insert
//      Parameter: Type=System.Int32, Name=index
//      Parameter: Type=System.Object, Name=value
//    Member: Remove
//      Parameter: Type=System.Object, Name=value
//    Member: RemoveAt
//      Parameter: Type=System.Int32, Name=index
//    Member: Item
//      Accessor method: System.Object get_Item(Int32)
//      Accessor method: Void set_Item(Int32, System.Object)
//    Member: IsReadOnly
//      Accessor method: Boolean get_IsReadOnly()
//    Member: IsFixedSize
//      Accessor method: Boolean get_IsFixedSize()
//
//  Type: System.Array
//    Member: IndexOf
//      Parameter: Type=T[], Name=array
//      Parameter: Type=T, Name=value
//    Member: AsReadOnly
//      Parameter: Type=T[], Name=array
//    Member: Resize
//      Attributes:
//        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
//      Parameter: Type=T[]&, Name=array
//      Parameter: Type=System.Int32, Name=newSize
//    Member: BinarySearch
//      Attributes:
//        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
//      Parameter: Type=T[], Name=array
//      Parameter: Type=T, Name=value
//    Member: BinarySearch
//      Attributes:
//        System.Runtime.ConstrainedExecution.ReliabilityContractAttribute
//      Parameter: Type=T[], Name=array
//      Parameter: Type=T, Name=value
//      Parameter: Type=System.Collections.Generic.IComparer`1[T], Name=comparer

Commenti

La classe MemberInfo è la classe base astratta per le classi usate per ottenere informazioni su tutti i membri di una classe (costruttori, eventi, campi, metodi e proprietà).

Questa classe introduce le funzionalità di base fornite da tutti i membri.

Note per gli implementatori

Quando si eredita da MemberInfo è necessario eseguire l'override dei membri seguenti: GetCustomAttributes(Boolean) e IsDefined(Type, Boolean).

Costruttori

MemberInfo()

Inizializza una nuova istanza della classe MemberInfo.

Proprietà

CustomAttributes

Ottiene una raccolta contenente gli attributi personalizzati di questo membro.

DeclaringType

Ottiene la classe che dichiara questo membro.

IsCollectible

Ottiene un valore che indica se l'oggetto MemberInfo fa parte di un assembly contenuto in un AssemblyLoadContextraccoglibile.

MemberType

In caso di override in una classe derivata, ottiene un valore MemberTypes che indica il tipo del membro, metodo, costruttore, evento e così via.

MetadataToken

Ottiene un valore che identifica un elemento di metadati.

Module

Ottiene il modulo in cui viene definito il tipo che dichiara il membro rappresentato dal MemberInfo corrente.

Name

Ottiene il nome del membro corrente.

ReflectedType

Ottiene l'oggetto classe utilizzato per ottenere questa istanza di MemberInfo.

Metodi

Equals(Object)

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

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetCustomAttributes(Boolean)

Quando sottoposto a override in una classe derivata, restituisce una matrice di tutti gli attributi personalizzati applicati a questo membro.

GetCustomAttributes(Type, Boolean)

In caso di override in una classe derivata, restituisce una matrice di attributi personalizzati applicati a questo membro e identificata da Type.

GetCustomAttributesData()

Restituisce un elenco di oggetti CustomAttributeData che rappresentano i dati sugli attributi applicati al membro di destinazione.

GetHashCode()

Restituisce il codice hash per questa istanza.

GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Individua gli attributi di un membro e fornisce l'accesso ai metadati dei membri.

GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
HasSameMetadataDefinitionAs(MemberInfo)

Ottiene informazioni sugli attributi di un membro e fornisce l'accesso ai metadati dei membri.

IsDefined(Type, Boolean)

In caso di override in una classe derivata, indica se uno o più attributi del tipo specificato o dei relativi tipi derivati vengono applicati a questo membro.

MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Operatori

Equality(MemberInfo, MemberInfo)

Indica se due oggetti MemberInfo sono uguali.

Inequality(MemberInfo, MemberInfo)

Indica se due oggetti MemberInfo non sono uguali.

Implementazioni dell'interfaccia esplicita

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

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

_MemberInfo.GetType()

Ottiene un oggetto Type che rappresenta la classe MemberInfo.

_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

Recupera le informazioni sul tipo per un oggetto, che può quindi essere utilizzato per ottenere le informazioni sul tipo per un'interfaccia.

_MemberInfo.GetTypeInfoCount(UInt32)

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

_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

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

ICustomAttributeProvider.GetCustomAttributes(Boolean)

Restituisce una matrice di tutti gli attributi personalizzati definiti in questo membro, esclusi gli attributi denominati o una matrice vuota se non sono presenti attributi personalizzati.

ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)

Restituisce una matrice di attributi personalizzati definiti in questo membro, identificato dal tipo o una matrice vuota se non sono presenti attributi personalizzati di tale tipo.

ICustomAttributeProvider.IsDefined(Type, Boolean)

Indica se in questo membro è definita una o più istanze di attributeType.

Metodi di estensione

GetCustomAttribute(MemberInfo, Type, Boolean)

Recupera un attributo personalizzato di un tipo specificato applicato a un membro specificato e, facoltativamente, controlla i predecessori di tale membro.

GetCustomAttribute(MemberInfo, Type)

Recupera un attributo personalizzato di un tipo specificato applicato a un membro specificato.

GetCustomAttribute<T>(MemberInfo, Boolean)

Recupera un attributo personalizzato di un tipo specificato applicato a un membro specificato e, facoltativamente, controlla i predecessori di tale membro.

GetCustomAttribute<T>(MemberInfo)

Recupera un attributo personalizzato di un tipo specificato applicato a un membro specificato.

GetCustomAttributes(MemberInfo, Boolean)

Recupera una raccolta di attributi personalizzati applicati a un membro specificato e, facoltativamente, controlla i predecessori di tale membro.

GetCustomAttributes(MemberInfo, Type, Boolean)

Recupera una raccolta di attributi personalizzati di un tipo specificato applicato a un membro specificato e, facoltativamente, controlla i predecessori di tale membro.

GetCustomAttributes(MemberInfo, Type)

Recupera una raccolta di attributi personalizzati di un tipo specificato applicato a un membro specificato.

GetCustomAttributes(MemberInfo)

Recupera una raccolta di attributi personalizzati applicati a un membro specificato.

GetCustomAttributes<T>(MemberInfo, Boolean)

Recupera una raccolta di attributi personalizzati di un tipo specificato applicato a un membro specificato e, facoltativamente, controlla i predecessori di tale membro.

GetCustomAttributes<T>(MemberInfo)

Recupera una raccolta di attributi personalizzati di un tipo specificato applicato a un membro specificato.

IsDefined(MemberInfo, Type, Boolean)

Indica se gli attributi personalizzati di un tipo specificato vengono applicati a un membro specificato e, facoltativamente, applicati ai relativi predecessori.

IsDefined(MemberInfo, Type)

Indica se gli attributi personalizzati di un tipo specificato vengono applicati a un membro specificato.

GetMetadataToken(MemberInfo)

Ottiene un token di metadati per il membro specificato, se disponibile.

HasMetadataToken(MemberInfo)

Restituisce un valore che indica se un token di metadati è disponibile per il membro specificato.

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Thread safety

Questo tipo è thread-safe.