Attribute.GetCustomAttributes Metoda

Definice

Načte pole vlastních atributů použitých u sestavení, modulu, člena typu nebo parametru metody.

Přetížení

GetCustomAttributes(ParameterInfo, Boolean)

Načte pole vlastních atributů použitých u parametru metody. Parametry určují parametr metody a zda se mají hledat předky parametru metody.

GetCustomAttributes(MemberInfo, Type, Boolean)

Načte pole vlastních atributů použitých na člena typu. Parametry určují člena, typ vlastního atributu, který se má vyhledat, a zda se mají hledat nadřazené položky člena.

GetCustomAttributes(ParameterInfo, Type, Boolean)

Načte pole vlastních atributů použitých u parametru metody. Parametry určují parametr metody, typ vlastního atributu, který se má vyhledat, a zda se mají hledat nadřazené položky parametru metody.

GetCustomAttributes(Module, Type, Boolean)

Načte pole vlastních atributů použitých v modulu. Parametry určují modul, typ vlastního atributu, který se má vyhledat, a ignorovanou možnost hledání.

GetCustomAttributes(MemberInfo, Type)

Načte pole vlastních atributů použitých na člena typu. Parametry určují člena a typ vlastního atributu, který se má vyhledat.

GetCustomAttributes(Assembly, Type, Boolean)

Načte pole vlastních atributů použitých na sestavení. Parametry určují sestavení, typ vlastního atributu, který se má vyhledat, a ignorovanou možnost hledání.

GetCustomAttributes(Module, Type)

Načte pole vlastních atributů použitých v modulu. Parametry určují modul a typ vlastního atributu, který se má vyhledat.

GetCustomAttributes(ParameterInfo, Type)

Načte pole vlastních atributů použitých u parametru metody. Parametry určují parametr metody a typ vlastního atributu, který se má vyhledat.

GetCustomAttributes(MemberInfo, Boolean)

Načte pole vlastních atributů použitých na člena typu. Parametry určují člena, typ vlastního atributu, který se má vyhledat, a zda se mají hledat nadřazené položky člena.

GetCustomAttributes(Assembly, Type)

Načte pole vlastních atributů použitých na sestavení. Parametry určují sestavení a typ vlastního atributu, který se má vyhledat.

GetCustomAttributes(Assembly, Boolean)

Načte pole vlastních atributů použitých na sestavení. Parametry určují sestavení a ignorovanou možnost hledání.

GetCustomAttributes(ParameterInfo)

Načte pole vlastních atributů použitých u parametru metody. Parametr určuje parametr metody.

GetCustomAttributes(Module)

Načte pole vlastních atributů použitých v modulu. Parametr určuje modul.

GetCustomAttributes(MemberInfo)

Načte pole vlastních atributů použitých na člena typu. Parametr určuje člena.

GetCustomAttributes(Assembly)

Načte pole vlastních atributů použitých na sestavení. Parametr určuje sestavení.

GetCustomAttributes(Module, Boolean)

Načte pole vlastních atributů použitých v modulu. Parametry určují modul a ignorovanou možnost hledání.

GetCustomAttributes(ParameterInfo, Boolean)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých u parametru metody. Parametry určují parametr metody a zda se mají hledat předky parametru metody.

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

Parametry

element
ParameterInfo

Objekt odvozený z třídy ParameterInfo, který popisuje parametr člena třídy.

inherit
Boolean

Pokud true, určuje, že má hledat také nadřazené položky element pro vlastní atributy.

Návraty

Pole Attribute obsahující vlastní atributy použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

Member vlastnost element je null.

element je null.

Vlastní typ atributu nelze načíst.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít ParameterInfo jako parametr.

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"
 */

Poznámky

Pokud element představuje parametr v metodě odvozeného typu, návratová hodnota zahrnuje zděděné vlastní atributy použité na stejný parametr v přepisovaných základních metodách.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých na člena typu. Parametry určují člena, typ vlastního atributu, který se má vyhledat, a zda se mají hledat nadřazené položky člena.

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

Parametry

element
MemberInfo

Objekt odvozený z MemberInfo třídy, která popisuje konstruktor, událost, pole, metodu nebo vlastnost člen třídy.

typeattributeType
Type

Typ nebo základní typ vlastního atributu, který se má vyhledat.

inherit
Boolean

Pokud true, určuje, že má hledat také nadřazené položky element pro vlastní atributy.

Návraty

Pole Attribute obsahující vlastní atributy typu type použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element nebo type je null.

type není odvozen z Attribute.

element není konstruktor, metoda, vlastnost, událost, typ nebo pole.

Vlastní typ atributu nelze načíst.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít MemberInfo jako parametr.

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.

*/

Poznámky

Návratová hodnota obsahuje vlastní atributy pro předky element, pokud je inherittrue.

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí atributy zabezpečení pro metody, konstruktory a typy, pokud jsou atributy uloženy v novém formátu metadat. Sestavení zkompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná se staršími verzemi rozhraní .NET Framework používají starý formát XML. Viz generování deklarativních atributů zabezpečení.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých u parametru metody. Parametry určují parametr metody, typ vlastního atributu, který se má vyhledat, a zda se mají hledat nadřazené položky parametru metody.

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

Parametry

element
ParameterInfo

Objekt odvozený z třídy ParameterInfo, který popisuje parametr člena třídy.

attributeType
Type

Typ nebo základní typ vlastního atributu, který se má vyhledat.

inherit
Boolean

Pokud true, určuje, že má hledat také nadřazené položky element pro vlastní atributy.

Návraty

Pole Attribute obsahující vlastní atributy typu attributeType použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element nebo attributeType je null.

attributeType není odvozen z Attribute.

Vlastní typ atributu nelze načíst.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít ParameterInfo jako parametr.

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"
 */

Poznámky

Pokud element představuje parametr v metodě odvozeného typu, návratová hodnota zahrnuje zděděné vlastní atributy použité na stejný parametr v přepisovaných základních metodách.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých v modulu. Parametry určují modul, typ vlastního atributu, který se má vyhledat, a ignorovanou možnost hledání.

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

Parametry

element
Module

Objekt odvozený z Module třídy, která popisuje přenosný spustitelný soubor.

attributeType
Type

Typ nebo základní typ vlastního atributu, který se má vyhledat.

inherit
Boolean

Tento parametr je ignorován a nemá vliv na operaci této metody.

Návraty

Pole Attribute obsahující vlastní atributy typu attributeType použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element nebo attributeType je null.

attributeType není odvozen z Attribute.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít Module jako parametr.

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".
 */

Poznámky

Návratová hodnota obsahuje vlastní atributy pro předky element, pokud je inherittrue.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých na člena typu. Parametry určují člena a typ vlastního atributu, který se má vyhledat.

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

Parametry

element
MemberInfo

Objekt odvozený z MemberInfo třídy, která popisuje konstruktor, událost, pole, metodu nebo vlastnost člen třídy.

typeattributeType
Type

Typ nebo základní typ vlastního atributu, který se má vyhledat.

Návraty

Pole Attribute obsahující vlastní atributy typu type použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element nebo type je null.

type není odvozen z Attribute.

element není konstruktor, metoda, vlastnost, událost, typ nebo pole.

Vlastní typ atributu nelze načíst.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttribute, vzít MemberInfo jako parametr.

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.

*/

Poznámky

Návratová hodnota obsahuje vlastní atributy pro předky element.

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí atributy zabezpečení pro metody, konstruktory a typy, pokud jsou atributy uloženy v novém formátu metadat. Sestavení zkompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná se staršími verzemi rozhraní .NET Framework používají starý formát XML. Viz generování deklarativních atributů zabezpečení.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých na sestavení. Parametry určují sestavení, typ vlastního atributu, který se má vyhledat, a ignorovanou možnost hledání.

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

Parametry

element
Assembly

Objekt odvozený z Assembly třídy, která popisuje opakovaně použitelnou kolekci modulů.

attributeType
Type

Typ nebo základní typ vlastního atributu, který se má vyhledat.

inherit
Boolean

Tento parametr je ignorován a nemá vliv na operaci této metody.

Návraty

Pole Attribute obsahující vlastní atributy typu attributeType použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element nebo attributeType je null.

attributeType není odvozen z Attribute.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít Assembly jako parametr.

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.

Poznámky

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí atributy zabezpečení, pokud jsou atributy uloženy v novém formátu metadat. Sestavení zkompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná se staršími verzemi rozhraní .NET Framework používají starý formát XML. Viz generování deklarativních atributů zabezpečení.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých v modulu. Parametry určují modul a typ vlastního atributu, který se má vyhledat.

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

Parametry

element
Module

Objekt odvozený z Module třídy, která popisuje přenosný spustitelný soubor.

attributeType
Type

Typ nebo základní typ vlastního atributu, který se má vyhledat.

Návraty

Pole Attribute obsahující vlastní atributy typu attributeType použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element nebo attributeType je null.

attributeType není odvozen z Attribute.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít Module jako parametr.

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".
 */

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých u parametru metody. Parametry určují parametr metody a typ vlastního atributu, který se má vyhledat.

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

Parametry

element
ParameterInfo

Objekt odvozený z třídy ParameterInfo, který popisuje parametr člena třídy.

attributeType
Type

Typ nebo základní typ vlastního atributu, který se má vyhledat.

Návraty

Pole Attribute obsahující vlastní atributy typu attributeType použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element nebo attributeType je null.

attributeType není odvozen z Attribute.

Vlastní typ atributu nelze načíst.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít ParameterInfo jako parametr.

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"
 */

Poznámky

Pokud element představuje parametr v metodě odvozeného typu, návratová hodnota zahrnuje zděděné vlastní atributy použité na stejný parametr v přepisovaných základních metodách.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých na člena typu. Parametry určují člena, typ vlastního atributu, který se má vyhledat, a zda se mají hledat nadřazené položky člena.

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

Parametry

element
MemberInfo

Objekt odvozený z MemberInfo třídy, která popisuje konstruktor, událost, pole, metodu nebo vlastnost člen třídy.

inherit
Boolean

Pokud true, určuje, že má hledat také nadřazené položky element pro vlastní atributy.

Návraty

Pole Attribute obsahující vlastní atributy použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element je null.

element není konstruktor, metoda, vlastnost, událost, typ nebo pole.

Vlastní typ atributu nelze načíst.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít MemberInfo jako parametr.

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.

*/

Poznámky

Návratová hodnota obsahuje vlastní atributy pro předky element, pokud je inherittrue.

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí atributy zabezpečení pro metody, konstruktory a typy, pokud jsou atributy uloženy v novém formátu metadat. Sestavení zkompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná se staršími verzemi rozhraní .NET Framework používají starý formát XML. Viz generování deklarativních atributů zabezpečení.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých na sestavení. Parametry určují sestavení a typ vlastního atributu, který se má vyhledat.

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

Parametry

element
Assembly

Objekt odvozený z Assembly třídy, která popisuje opakovaně použitelnou kolekci modulů.

attributeType
Type

Typ nebo základní typ vlastního atributu, který se má vyhledat.

Návraty

Pole Attribute obsahující vlastní atributy typu attributeType použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element nebo attributeType je null.

attributeType není odvozen z Attribute.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít Assembly jako parametr.

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.

Poznámky

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí atributy zabezpečení, pokud jsou atributy uloženy v novém formátu metadat. Sestavení zkompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná se staršími verzemi rozhraní .NET Framework používají starý formát XML. Viz generování deklarativních atributů zabezpečení.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých na sestavení. Parametry určují sestavení a ignorovanou možnost hledání.

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

Parametry

element
Assembly

Objekt odvozený z Assembly třídy, která popisuje opakovaně použitelnou kolekci modulů.

inherit
Boolean

Tento parametr je ignorován a nemá vliv na operaci této metody.

Návraty

Pole Attribute obsahující vlastní atributy použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element je null.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít Assembly jako parametr.

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.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých u parametru metody. Parametr určuje parametr metody.

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

Parametry

element
ParameterInfo

Objekt odvozený z třídy ParameterInfo, který popisuje parametr člena třídy.

Návraty

Pole Attribute obsahující vlastní atributy použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element je null.

Vlastní typ atributu nelze načíst.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít ParameterInfo jako parametr.

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"
 */

Poznámky

Pokud element představuje parametr v metodě odvozeného typu, návratová hodnota zahrnuje zděděné vlastní atributy použité na stejný parametr v přepisovaných základních metodách.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých v modulu. Parametr určuje modul.

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

Parametry

element
Module

Objekt odvozený z Module třídy, která popisuje přenosný spustitelný soubor.

Návraty

Pole Attribute obsahující vlastní atributy použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element je null.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít Module jako parametr.

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".
 */

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých na člena typu. Parametr určuje člena.

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

Parametry

element
MemberInfo

Objekt odvozený z MemberInfo třídy, která popisuje konstruktor, událost, pole, metodu nebo vlastnost člen třídy.

Návraty

Pole Attribute obsahující vlastní atributy použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element je null.

element není konstruktor, metoda, vlastnost, událost, typ nebo pole.

Vlastní typ atributu nelze načíst.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttribute, vzít MemberInfo jako parametr.

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.

*/

Poznámky

Návratová hodnota obsahuje vlastní atributy pro předky element.

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí atributy zabezpečení pro metody, konstruktory a typy, pokud jsou atributy uloženy v novém formátu metadat. Sestavení zkompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná se staršími verzemi rozhraní .NET Framework používají starý formát XML. Viz generování deklarativních atributů zabezpečení.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých na sestavení. Parametr určuje sestavení.

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

Parametry

element
Assembly

Objekt odvozený z Assembly třídy, která popisuje opakovaně použitelnou kolekci modulů.

Návraty

Pole Attribute obsahující vlastní atributy použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element je null.

Příklady

Následující příklad načte vlastní atributy nalezené v aktuálním sestavení.

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.

Poznámky

Poznámka

Počínaje rozhraním .NET Framework verze 2.0 tato metoda vrátí atributy zabezpečení, pokud jsou atributy uloženy v novém formátu metadat. Sestavení zkompilovaná s verzí 2.0 nebo novější používají nový formát. Dynamická sestavení a sestavení zkompilovaná se staršími verzemi rozhraní .NET Framework používají starý formát XML. Viz generování deklarativních atributů zabezpečení.

Platí pro

.NET 9 a další verze
Produkt Verze
.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)

Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs
Zdroj:
Attribute.CoreCLR.cs

Načte pole vlastních atributů použitých v modulu. Parametry určují modul a ignorovanou možnost hledání.

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

Parametry

element
Module

Objekt odvozený z Module třídy, která popisuje přenosný spustitelný soubor.

inherit
Boolean

Tento parametr je ignorován a nemá vliv na operaci této metody.

Návraty

Pole Attribute obsahující vlastní atributy použité na elementnebo prázdné pole, pokud takové vlastní atributy neexistují.

Výjimky

element je null.

Příklady

Následující příklad kódu ukazuje použití GetCustomAttributes, vzít Module jako parametr.

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".
 */

Poznámky

Návratová hodnota obsahuje vlastní atributy pro předky element, pokud je inherittrue.

Platí pro

.NET 9 a další verze
Produkt Verze
.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