Auf Englisch lesen

Teilen über


Attribute.GetCustomAttributes Methode

Definition

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly, ein Modul, ein Typmemm oder einen Methodenparameter angewendet werden.

Überlädt

GetCustomAttributes(ParameterInfo, Boolean)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf einen Methodenparameter angewendet werden. Parameter geben den Methodenparameter an und geben an, ob Vorgänger des Methodenparameters durchsucht werden sollen.

GetCustomAttributes(MemberInfo, Type, Boolean)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Element eines Typs angewendet werden. Parameter geben das Element, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und ob Vorgänger des Elements durchsucht werden sollen.

GetCustomAttributes(ParameterInfo, Type, Boolean)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf einen Methodenparameter angewendet werden. Parameter geben den Methodenparameter, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und ob Vorgänger des Methodenparameters durchsucht werden sollen.

GetCustomAttributes(Module, Type, Boolean)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Modul angewendet werden. Parameter geben das Modul, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und eine ignorierte Suchoption.

GetCustomAttributes(MemberInfo, Type)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Element eines Typs angewendet werden. Parameter geben das Element und den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll.

GetCustomAttributes(Assembly, Type, Boolean)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly angewendet werden. Parameter geben die Assembly, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und eine ignorierte Suchoption.

GetCustomAttributes(Module, Type)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Modul angewendet werden. Parameter geben das Modul und den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll.

GetCustomAttributes(ParameterInfo, Type)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf einen Methodenparameter angewendet werden. Parameter geben den Methodenparameter und den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll.

GetCustomAttributes(MemberInfo, Boolean)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Element eines Typs angewendet werden. Parameter geben das Element, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und ob Vorgänger des Elements durchsucht werden sollen.

GetCustomAttributes(Assembly, Type)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly angewendet werden. Parameter geben die Assembly und den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll.

GetCustomAttributes(Assembly, Boolean)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly angewendet werden. Parameter geben die Assembly und eine ignorierte Suchoption an.

GetCustomAttributes(ParameterInfo)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf einen Methodenparameter angewendet werden. Ein Parameter gibt den Methodenparameter an.

GetCustomAttributes(Module)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Modul angewendet werden. Ein Parameter gibt das Modul an.

GetCustomAttributes(MemberInfo)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Element eines Typs angewendet werden. Ein Parameter gibt das Element an.

GetCustomAttributes(Assembly)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly angewendet werden. Ein Parameter gibt die Assembly an.

GetCustomAttributes(Module, Boolean)

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Modul angewendet werden. Parameter geben das Modul und eine ignorierte Suchoption an.

GetCustomAttributes(ParameterInfo, Boolean)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf einen Methodenparameter angewendet werden. Parameter geben den Methodenparameter an und geben an, ob Vorgänger des Methodenparameters durchsucht werden sollen.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element, bool inherit);

Parameter

element
ParameterInfo

Ein von der ParameterInfo Klasse abgeleitetes Objekt, das einen Parameter eines Elements einer Klasse beschreibt.

inherit
Boolean

Wenn true, wird angegeben, dass auch die Vorgänger von element nach benutzerdefinierten Attributen durchsucht werden sollen.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute enthält, die auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

Die Member Eigenschaft von element ist null.

element ist null.

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein ParameterInfo als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() ==
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 */

Hinweise

Wenn element einen Parameter in einer Methode eines abgeleiteten Typs darstellt, enthält der Rückgabewert die vererbbaren benutzerdefinierten Attribute, die auf denselben Parameter in den überschriebenen Basismethoden angewendet werden.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(MemberInfo, Type, Boolean)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Element eines Typs angewendet werden. Parameter geben das Element, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und ob Vorgänger des Elements durchsucht werden sollen.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, Type type, bool inherit);
C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, Type attributeType, bool inherit);

Parameter

element
MemberInfo

Ein von der MemberInfo Klasse abgeleitetes Objekt, das einen Konstruktor, ein Ereignis, ein Feld, eine Methode oder ein Eigenschaftsmememm einer Klasse beschreibt.

typeattributeType
Type

Der Typ oder ein Basistyp des benutzerdefinierten Attributs, nach dem gesucht werden soll.

inherit
Boolean

Wenn true, wird angegeben, dass auch die Vorgänger von element nach benutzerdefinierten Attributen durchsucht werden sollen.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute des Typs enthält, type auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element oder type ist null.

type wird nicht von Attributeabgeleitet.

element ist kein Konstruktor, keine Methode, Eigenschaft, Ereignis, Typ oder Feld.

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein MemberInfo als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // Define the Unmanaged attribute.
    public class UnmanagedAttribute : Attribute
    {
        // Storage for the UnmanagedType value.
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor.
        public UnmanagedAttribute(UnmanagedType type)
        {
            thisType = type;
        }

        // Define a property to get and set the UnmanagedType value.
        public UnmanagedType Win32Type
        {
            get { return thisType; }
            set { thisType = Win32Type; }
        }
    }

    // Create a class for an imported Win32 unmanaged function.
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);
    }

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        [Unmanaged(UnmanagedType.User)]
        public void Win32CallMethod()
        {
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
        }
    }

    class DemoClass {
        static void Main(string[] args)
            {
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
            {
                // Iterate through all the attributes of the method.
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute.
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                    {
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

                    // Check for the Unmanaged attribute.
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                    {
                        Console.WriteLine(
                            "This method calls unmanaged code.");
                        Console.WriteLine(
                            String.Format("The Unmanaged attribute type is {0}.",
                                          ((UnmanagedAttribute)attr).Win32Type));
                        AClass myCls = new AClass();
                        myCls.Win32CallMethod();
                    }
                }
            }
        }
    }
}

/*

This code example produces the following results.

First, the compilation yields the warning, "... This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.

*/

Hinweise

Der Rückgabewert enthält die benutzerdefinierten Attribute für Vorgänger von element, wenn inherittrueist.

Hinweis

Ab .NET Framework, Version 2.0, gibt diese Methode Sicherheitsattribute für Methoden, Konstruktoren und Typen zurück, wenn die Attribute im neuen Metadatenformat gespeichert sind. Assemblys, die mit Version 2.0 oder höher kompiliert wurden, verwenden das neue Format. Dynamische Assemblys und Assemblys, die mit früheren Versionen von .NET Framework kompiliert wurden, verwenden das alte XML-Format. Siehe Deklarative Sicherheitsattribute.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(ParameterInfo, Type, Boolean)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf einen Methodenparameter angewendet werden. Parameter geben den Methodenparameter, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und ob Vorgänger des Methodenparameters durchsucht werden sollen.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element, Type attributeType, bool inherit);

Parameter

element
ParameterInfo

Ein von der ParameterInfo Klasse abgeleitetes Objekt, das einen Parameter eines Elements einer Klasse beschreibt.

attributeType
Type

Der Typ oder ein Basistyp des benutzerdefinierten Attributs, nach dem gesucht werden soll.

inherit
Boolean

Wenn true, wird angegeben, dass auch die Vorgänger von element nach benutzerdefinierten Attributen durchsucht werden sollen.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute des Typs enthält, attributeType auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element oder attributeType ist null.

attributeType wird nicht von Attributeabgeleitet.

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein ParameterInfo als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() ==
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 */

Hinweise

Wenn element einen Parameter in einer Methode eines abgeleiteten Typs darstellt, enthält der Rückgabewert die vererbbaren benutzerdefinierten Attribute, die auf denselben Parameter in den überschriebenen Basismethoden angewendet werden.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(Module, Type, Boolean)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Modul angewendet werden. Parameter geben das Modul, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und eine ignorierte Suchoption.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Module element, Type attributeType, bool inherit);

Parameter

element
Module

Ein Von der Module Klasse abgeleitetes Objekt, das eine portable ausführbare Datei beschreibt.

attributeType
Type

Der Typ oder ein Basistyp des benutzerdefinierten Attributs, nach dem gesucht werden soll.

inherit
Boolean

Dieser Parameter wird ignoriert und wirkt sich nicht auf den Vorgang dieser Methode aus.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute des Typs enthält, attributeType auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element oder attributeType ist null.

attributeType wird nicht von Attributeabgeleitet.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein Module als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

namespace CustAttrs2CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Module type to access its metadata.
            Module module = clsType.Module;

            // Iterate through all the attributes for the module.
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute.
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name,
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute.
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ?
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

Hinweise

Der Rückgabewert enthält die benutzerdefinierten Attribute für Vorgänger von element, wenn inherittrueist.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(MemberInfo, Type)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Element eines Typs angewendet werden. Parameter geben das Element und den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, Type type);
C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, Type attributeType);

Parameter

element
MemberInfo

Ein von der MemberInfo Klasse abgeleitetes Objekt, das einen Konstruktor, ein Ereignis, ein Feld, eine Methode oder ein Eigenschaftsmememm einer Klasse beschreibt.

typeattributeType
Type

Der Typ oder ein Basistyp des benutzerdefinierten Attributs, nach dem gesucht werden soll.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute des Typs enthält, type auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element oder type ist null.

type wird nicht von Attributeabgeleitet.

element ist kein Konstruktor, keine Methode, Eigenschaft, Ereignis, Typ oder Feld.

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributeveranschaulicht, wobei ein MemberInfo als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // Define the Unmanaged attribute.
    public class UnmanagedAttribute : Attribute
    {
        // Storage for the UnmanagedType value.
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor.
        public UnmanagedAttribute(UnmanagedType type)
        {
            thisType = type;
        }

        // Define a property to get and set the UnmanagedType value.
        public UnmanagedType Win32Type
        {
            get { return thisType; }
            set { thisType = Win32Type; }
        }
    }

    // Create a class for an imported Win32 unmanaged function.
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);
    }

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        [Unmanaged(UnmanagedType.User)]
        public void Win32CallMethod()
        {
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
        }
    }

    class DemoClass {
        static void Main(string[] args)
            {
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
            {
                // Iterate through all the attributes of the method.
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute.
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                    {
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

                    // Check for the Unmanaged attribute.
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                    {
                        Console.WriteLine(
                            "This method calls unmanaged code.");
                        Console.WriteLine(
                            String.Format("The Unmanaged attribute type is {0}.",
                                          ((UnmanagedAttribute)attr).Win32Type));
                        AClass myCls = new AClass();
                        myCls.Win32CallMethod();
                    }
                }
            }
        }
    }
}

/*

This code example produces the following results.

First, the compilation yields the warning, "... This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.

*/

Hinweise

Der Rückgabewert enthält die benutzerdefinierten Attribute für Vorgänger von element.

Hinweis

Ab .NET Framework, Version 2.0, gibt diese Methode Sicherheitsattribute für Methoden, Konstruktoren und Typen zurück, wenn die Attribute im neuen Metadatenformat gespeichert sind. Assemblys, die mit Version 2.0 oder höher kompiliert wurden, verwenden das neue Format. Dynamische Assemblys und Assemblys, die mit früheren Versionen von .NET Framework kompiliert wurden, verwenden das alte XML-Format. Siehe Deklarative Sicherheitsattribute.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(Assembly, Type, Boolean)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly angewendet werden. Parameter geben die Assembly, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und eine ignorierte Suchoption.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element, Type attributeType, bool inherit);

Parameter

element
Assembly

Ein von der Assembly Klasse abgeleitetes Objekt, das eine wiederverwendbare Auflistung von Modulen beschreibt.

attributeType
Type

Der Typ oder ein Basistyp des benutzerdefinierten Attributs, nach dem gesucht werden soll.

inherit
Boolean

Dieser Parameter wird ignoriert und wirkt sich nicht auf den Vorgang dieser Methode aus.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute des Typs enthält, attributeType auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element oder attributeType ist null.

attributeType wird nicht von Attributeabgeleitet.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein Assembly als Parameter verwendet wird.

C#
using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

class Example {
    static void Main() {
        // Get the Assembly object to access its metadata.
        Assembly assy = typeof(Example).Assembly;

        // Iterate through the attributes for the assembly.
        foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
            // Check for the AssemblyTitle attribute.
            if (attr.GetType() == typeof(AssemblyTitleAttribute))
                Console.WriteLine("Assembly title is \"{0}\".",
                    ((AssemblyTitleAttribute)attr).Title);

            // Check for the AssemblyDescription attribute.
            else if (attr.GetType() ==
                typeof(AssemblyDescriptionAttribute))
                Console.WriteLine("Assembly description is \"{0}\".",
                    ((AssemblyDescriptionAttribute)attr).Description);

            // Check for the AssemblyCompany attribute.
            else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                Console.WriteLine("Assembly company is {0}.",
                    ((AssemblyCompanyAttribute)attr).Company);
        }
   }
}
// The example displays the following output:
//     Assembly title is "CustAttrs1CS".
//     Assembly description is "GetCustomAttributes() Demo".
//     Assembly company is Microsoft.

Hinweise

Hinweis

Ab .NET Framework, Version 2.0, gibt diese Methode Sicherheitsattribute zurück, wenn die Attribute im neuen Metadatenformat gespeichert werden. Assemblys, die mit Version 2.0 oder höher kompiliert wurden, verwenden das neue Format. Dynamische Assemblys und Assemblys, die mit früheren Versionen von .NET Framework kompiliert wurden, verwenden das alte XML-Format. Siehe Deklarative Sicherheitsattribute.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(Module, Type)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Modul angewendet werden. Parameter geben das Modul und den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Module element, Type attributeType);

Parameter

element
Module

Ein Von der Module Klasse abgeleitetes Objekt, das eine portable ausführbare Datei beschreibt.

attributeType
Type

Der Typ oder ein Basistyp des benutzerdefinierten Attributs, nach dem gesucht werden soll.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute des Typs enthält, attributeType auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element oder attributeType ist null.

attributeType wird nicht von Attributeabgeleitet.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein Module als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

namespace CustAttrs2CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Module type to access its metadata.
            Module module = clsType.Module;

            // Iterate through all the attributes for the module.
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute.
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name,
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute.
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ?
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(ParameterInfo, Type)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf einen Methodenparameter angewendet werden. Parameter geben den Methodenparameter und den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element, Type attributeType);

Parameter

element
ParameterInfo

Ein von der ParameterInfo Klasse abgeleitetes Objekt, das einen Parameter eines Elements einer Klasse beschreibt.

attributeType
Type

Der Typ oder ein Basistyp des benutzerdefinierten Attributs, nach dem gesucht werden soll.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute des Typs enthält, attributeType auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element oder attributeType ist null.

attributeType wird nicht von Attributeabgeleitet.

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein ParameterInfo als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() ==
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 */

Hinweise

Wenn element einen Parameter in einer Methode eines abgeleiteten Typs darstellt, enthält der Rückgabewert die vererbbaren benutzerdefinierten Attribute, die auf denselben Parameter in den überschriebenen Basismethoden angewendet werden.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(MemberInfo, Boolean)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Element eines Typs angewendet werden. Parameter geben das Element, den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll, und ob Vorgänger des Elements durchsucht werden sollen.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element, bool inherit);

Parameter

element
MemberInfo

Ein von der MemberInfo Klasse abgeleitetes Objekt, das einen Konstruktor, ein Ereignis, ein Feld, eine Methode oder ein Eigenschaftsmememm einer Klasse beschreibt.

inherit
Boolean

Wenn true, wird angegeben, dass auch die Vorgänger von element nach benutzerdefinierten Attributen durchsucht werden sollen.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute enthält, die auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element ist null.

element ist kein Konstruktor, keine Methode, Eigenschaft, Ereignis, Typ oder Feld.

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein MemberInfo als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // Define the Unmanaged attribute.
    public class UnmanagedAttribute : Attribute
    {
        // Storage for the UnmanagedType value.
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor.
        public UnmanagedAttribute(UnmanagedType type)
        {
            thisType = type;
        }

        // Define a property to get and set the UnmanagedType value.
        public UnmanagedType Win32Type
        {
            get { return thisType; }
            set { thisType = Win32Type; }
        }
    }

    // Create a class for an imported Win32 unmanaged function.
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);
    }

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        [Unmanaged(UnmanagedType.User)]
        public void Win32CallMethod()
        {
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
        }
    }

    class DemoClass {
        static void Main(string[] args)
            {
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
            {
                // Iterate through all the attributes of the method.
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute.
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                    {
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

                    // Check for the Unmanaged attribute.
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                    {
                        Console.WriteLine(
                            "This method calls unmanaged code.");
                        Console.WriteLine(
                            String.Format("The Unmanaged attribute type is {0}.",
                                          ((UnmanagedAttribute)attr).Win32Type));
                        AClass myCls = new AClass();
                        myCls.Win32CallMethod();
                    }
                }
            }
        }
    }
}

/*

This code example produces the following results.

First, the compilation yields the warning, "... This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.

*/

Hinweise

Der Rückgabewert enthält die benutzerdefinierten Attribute für Vorgänger von element, wenn inherittrueist.

Hinweis

Ab .NET Framework, Version 2.0, gibt diese Methode Sicherheitsattribute für Methoden, Konstruktoren und Typen zurück, wenn die Attribute im neuen Metadatenformat gespeichert sind. Assemblys, die mit Version 2.0 oder höher kompiliert wurden, verwenden das neue Format. Dynamische Assemblys und Assemblys, die mit früheren Versionen von .NET Framework kompiliert wurden, verwenden das alte XML-Format. Siehe Deklarative Sicherheitsattribute.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(Assembly, Type)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly angewendet werden. Parameter geben die Assembly und den Typ des benutzerdefinierten Attributs an, nach dem gesucht werden soll.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element, Type attributeType);

Parameter

element
Assembly

Ein von der Assembly Klasse abgeleitetes Objekt, das eine wiederverwendbare Auflistung von Modulen beschreibt.

attributeType
Type

Der Typ oder ein Basistyp des benutzerdefinierten Attributs, nach dem gesucht werden soll.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute des Typs enthält, attributeType auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element oder attributeType ist null.

attributeType wird nicht von Attributeabgeleitet.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein Assembly als Parameter verwendet wird.

C#
using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

class Example {
    static void Main() {
        // Get the Assembly object to access its metadata.
        Assembly assy = typeof(Example).Assembly;

        // Iterate through the attributes for the assembly.
        foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
            // Check for the AssemblyTitle attribute.
            if (attr.GetType() == typeof(AssemblyTitleAttribute))
                Console.WriteLine("Assembly title is \"{0}\".",
                    ((AssemblyTitleAttribute)attr).Title);

            // Check for the AssemblyDescription attribute.
            else if (attr.GetType() ==
                typeof(AssemblyDescriptionAttribute))
                Console.WriteLine("Assembly description is \"{0}\".",
                    ((AssemblyDescriptionAttribute)attr).Description);

            // Check for the AssemblyCompany attribute.
            else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                Console.WriteLine("Assembly company is {0}.",
                    ((AssemblyCompanyAttribute)attr).Company);
        }
   }
}
// The example displays the following output:
//     Assembly title is "CustAttrs1CS".
//     Assembly description is "GetCustomAttributes() Demo".
//     Assembly company is Microsoft.

Hinweise

Hinweis

Ab .NET Framework, Version 2.0, gibt diese Methode Sicherheitsattribute zurück, wenn die Attribute im neuen Metadatenformat gespeichert werden. Assemblys, die mit Version 2.0 oder höher kompiliert wurden, verwenden das neue Format. Dynamische Assemblys und Assemblys, die mit früheren Versionen von .NET Framework kompiliert wurden, verwenden das alte XML-Format. Siehe Deklarative Sicherheitsattribute.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(Assembly, Boolean)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly angewendet werden. Parameter geben die Assembly und eine ignorierte Suchoption an.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element, bool inherit);

Parameter

element
Assembly

Ein von der Assembly Klasse abgeleitetes Objekt, das eine wiederverwendbare Auflistung von Modulen beschreibt.

inherit
Boolean

Dieser Parameter wird ignoriert und wirkt sich nicht auf den Vorgang dieser Methode aus.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute enthält, die auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element ist null.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein Assembly als Parameter verwendet wird.

C#
using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

class Example {
    static void Main() {
        // Get the Assembly object to access its metadata.
        Assembly assy = typeof(Example).Assembly;

        // Iterate through the attributes for the assembly.
        foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
            // Check for the AssemblyTitle attribute.
            if (attr.GetType() == typeof(AssemblyTitleAttribute))
                Console.WriteLine("Assembly title is \"{0}\".",
                    ((AssemblyTitleAttribute)attr).Title);

            // Check for the AssemblyDescription attribute.
            else if (attr.GetType() ==
                typeof(AssemblyDescriptionAttribute))
                Console.WriteLine("Assembly description is \"{0}\".",
                    ((AssemblyDescriptionAttribute)attr).Description);

            // Check for the AssemblyCompany attribute.
            else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                Console.WriteLine("Assembly company is {0}.",
                    ((AssemblyCompanyAttribute)attr).Company);
        }
   }
}
// The example displays the following output:
//     Assembly title is "CustAttrs1CS".
//     Assembly description is "GetCustomAttributes() Demo".
//     Assembly company is Microsoft.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(ParameterInfo)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf einen Methodenparameter angewendet werden. Ein Parameter gibt den Methodenparameter an.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.ParameterInfo element);

Parameter

element
ParameterInfo

Ein von der ParameterInfo Klasse abgeleitetes Objekt, das einen Parameter eines Elements einer Klasse beschreibt.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute enthält, die auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element ist null.

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein ParameterInfo als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() ==
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 */

Hinweise

Wenn element einen Parameter in einer Methode eines abgeleiteten Typs darstellt, enthält der Rückgabewert die vererbbaren benutzerdefinierten Attribute, die auf denselben Parameter in den überschriebenen Basismethoden angewendet werden.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(Module)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Modul angewendet werden. Ein Parameter gibt das Modul an.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Module element);

Parameter

element
Module

Ein Von der Module Klasse abgeleitetes Objekt, das eine portable ausführbare Datei beschreibt.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute enthält, die auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element ist null.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein Module als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

namespace CustAttrs2CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Module type to access its metadata.
            Module module = clsType.Module;

            // Iterate through all the attributes for the module.
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute.
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name,
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute.
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ?
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(MemberInfo)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Element eines Typs angewendet werden. Ein Parameter gibt das Element an.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.MemberInfo element);

Parameter

element
MemberInfo

Ein von der MemberInfo Klasse abgeleitetes Objekt, das einen Konstruktor, ein Ereignis, ein Feld, eine Methode oder ein Eigenschaftsmememm einer Klasse beschreibt.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute enthält, die auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element ist null.

element ist kein Konstruktor, keine Methode, Eigenschaft, Ereignis, Typ oder Feld.

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributeveranschaulicht, wobei ein MemberInfo als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // Define the Unmanaged attribute.
    public class UnmanagedAttribute : Attribute
    {
        // Storage for the UnmanagedType value.
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor.
        public UnmanagedAttribute(UnmanagedType type)
        {
            thisType = type;
        }

        // Define a property to get and set the UnmanagedType value.
        public UnmanagedType Win32Type
        {
            get { return thisType; }
            set { thisType = Win32Type; }
        }
    }

    // Create a class for an imported Win32 unmanaged function.
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);
    }

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        [Unmanaged(UnmanagedType.User)]
        public void Win32CallMethod()
        {
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
        }
    }

    class DemoClass {
        static void Main(string[] args)
            {
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
            {
                // Iterate through all the attributes of the method.
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute.
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                    {
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

                    // Check for the Unmanaged attribute.
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                    {
                        Console.WriteLine(
                            "This method calls unmanaged code.");
                        Console.WriteLine(
                            String.Format("The Unmanaged attribute type is {0}.",
                                          ((UnmanagedAttribute)attr).Win32Type));
                        AClass myCls = new AClass();
                        myCls.Win32CallMethod();
                    }
                }
            }
        }
    }
}

/*

This code example produces the following results.

First, the compilation yields the warning, "... This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.

*/

Hinweise

Der Rückgabewert enthält die benutzerdefinierten Attribute für Vorgänger von element.

Hinweis

Ab .NET Framework, Version 2.0, gibt diese Methode Sicherheitsattribute für Methoden, Konstruktoren und Typen zurück, wenn die Attribute im neuen Metadatenformat gespeichert sind. Assemblys, die mit Version 2.0 oder höher kompiliert wurden, verwenden das neue Format. Dynamische Assemblys und Assemblys, die mit früheren Versionen von .NET Framework kompiliert wurden, verwenden das alte XML-Format. Siehe Deklarative Sicherheitsattribute.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(Assembly)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf eine Assembly angewendet werden. Ein Parameter gibt die Assembly an.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Assembly element);

Parameter

element
Assembly

Ein von der Assembly Klasse abgeleitetes Objekt, das eine wiederverwendbare Auflistung von Modulen beschreibt.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute enthält, die auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element ist null.

Beispiele

Im folgenden Beispiel werden die benutzerdefinierten Attribute abgerufen, die in der aktuellen Assembly gefunden wurden.

C#
using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

class Example {
    static void Main() {
        // Get the Assembly object to access its metadata.
        Assembly assy = typeof(Example).Assembly;

        // Iterate through the attributes for the assembly.
        foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
            // Check for the AssemblyTitle attribute.
            if (attr.GetType() == typeof(AssemblyTitleAttribute))
                Console.WriteLine("Assembly title is \"{0}\".",
                    ((AssemblyTitleAttribute)attr).Title);

            // Check for the AssemblyDescription attribute.
            else if (attr.GetType() ==
                typeof(AssemblyDescriptionAttribute))
                Console.WriteLine("Assembly description is \"{0}\".",
                    ((AssemblyDescriptionAttribute)attr).Description);

            // Check for the AssemblyCompany attribute.
            else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                Console.WriteLine("Assembly company is {0}.",
                    ((AssemblyCompanyAttribute)attr).Company);
        }
   }
}
// The example displays the following output:
//     Assembly title is "CustAttrs1CS".
//     Assembly description is "GetCustomAttributes() Demo".
//     Assembly company is Microsoft.

Hinweise

Hinweis

Ab .NET Framework, Version 2.0, gibt diese Methode Sicherheitsattribute zurück, wenn die Attribute im neuen Metadatenformat gespeichert werden. Assemblys, die mit Version 2.0 oder höher kompiliert wurden, verwenden das neue Format. Dynamische Assemblys und Assemblys, die mit früheren Versionen von .NET Framework kompiliert wurden, verwenden das alte XML-Format. Siehe Deklarative Sicherheitsattribute.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1

GetCustomAttributes(Module, Boolean)

Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs
Quelle:
Attribute.CoreCLR.cs

Ruft ein Array der benutzerdefinierten Attribute ab, die auf ein Modul angewendet werden. Parameter geben das Modul und eine ignorierte Suchoption an.

C#
public static Attribute[] GetCustomAttributes (System.Reflection.Module element, bool inherit);

Parameter

element
Module

Ein Von der Module Klasse abgeleitetes Objekt, das eine portable ausführbare Datei beschreibt.

inherit
Boolean

Dieser Parameter wird ignoriert und wirkt sich nicht auf den Vorgang dieser Methode aus.

Gibt zurück

Ein Attribute Array, das die benutzerdefinierten Attribute enthält, die auf elementangewendet werden, oder ein leeres Array, wenn keine solchen benutzerdefinierten Attribute vorhanden sind.

Ausnahmen

element ist null.

Beispiele

Im folgenden Codebeispiel wird die Verwendung von GetCustomAttributesveranschaulicht, wobei ein Module als Parameter verwendet wird.

C#
using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

namespace CustAttrs2CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Module type to access its metadata.
            Module module = clsType.Module;

            // Iterate through all the attributes for the module.
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute.
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name,
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute.
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ?
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

Hinweise

Der Rückgabewert enthält die benutzerdefinierten Attribute für Vorgänger von element, wenn inherittrueist.

Gilt für:

.NET 9 und andere Versionen
Produkt Versionen
.NET 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 2.0, 2.1