Ler en inglés

Compartir por


Attribute.IsDefined Método

Definición

Determina si alguno de los atributos personalizados de un tipo especificado se aplica a un ensamblado, módulo, miembro de tipo o parámetro de método.

Sobrecargas

IsDefined(ParameterInfo, Type, Boolean)

Determina si alguno de los atributos personalizados se aplica a un parámetro de método. Los parámetros especifican el parámetro de método, el tipo del atributo personalizado que se va a buscar y si se deben buscar los antecesores del parámetro de método.

IsDefined(Module, Type, Boolean)

Determina si alguno de los atributos personalizados se aplica a un módulo. Los parámetros especifican el módulo, el tipo de atributo personalizado que se va a buscar y una opción de búsqueda omitida.

IsDefined(MemberInfo, Type, Boolean)

Determina si alguno de los atributos personalizados se aplica a un miembro de un tipo. Los parámetros especifican el miembro, el tipo del atributo personalizado que se va a buscar y si se deben buscar los antecesores del miembro.

IsDefined(Assembly, Type, Boolean)

Determina si alguno de los atributos personalizados se aplica a un ensamblado. Los parámetros especifican el ensamblado, el tipo de atributo personalizado que se va a buscar y una opción de búsqueda omitida.

IsDefined(MemberInfo, Type)

Determina si alguno de los atributos personalizados se aplica a un miembro de un tipo. Los parámetros especifican el miembro y el tipo del atributo personalizado que se va a buscar.

IsDefined(Module, Type)

Determina si se deben aplicar atributos personalizados de un tipo especificado a un módulo. Los parámetros especifican el módulo y el tipo del atributo personalizado que se va a buscar.

IsDefined(Assembly, Type)

Determina si alguno de los atributos personalizados se aplica a un ensamblado. Los parámetros especifican el ensamblado y el tipo del atributo personalizado que se va a buscar.

IsDefined(ParameterInfo, Type)

Determina si alguno de los atributos personalizados se aplica a un parámetro de método. Los parámetros especifican el parámetro de método y el tipo del atributo personalizado que se va a buscar.

IsDefined(ParameterInfo, Type, Boolean)

Determina si alguno de los atributos personalizados se aplica a un parámetro de método. Los parámetros especifican el parámetro de método, el tipo del atributo personalizado que se va a buscar y si se deben buscar los antecesores del parámetro de método.

C#
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType, bool inherit);

Parámetros

element
ParameterInfo

Objeto derivado de la clase ParameterInfo que describe un parámetro de un miembro de una clase.

attributeType
Type

Tipo, o tipo base, del atributo personalizado que se va a buscar.

inherit
Boolean

Si es true, especifica que se busquen también los atributos personalizados de los antecesores de element.

Devoluciones

Boolean

Es true si se aplica a attributeType un atributo personalizado de tipo element; en caso contrario, es false.

Excepciones

element o attributeType es null.

attributeType no se deriva de Attribute.

element no es un método, constructor o tipo.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de IsDefined, tomando como ParameterInfo parámetro .

C#
using System;
using System.Reflection;

namespace IsDef5CS
{
    public class TestClass
    {
        // Assign a ParamArray attribute to the parameter using the keyword.
        public void Method1(params String[] args)
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the ParameterInfo array for the method parameters.
            ParameterInfo[] pInfo = mInfo.GetParameters();
            if (pInfo != null)
            {
                // See if the ParamArray attribute is defined.
                bool isDef = Attribute.IsDefined(pInfo[0],
                                                 typeof(ParamArrayAttribute));
                // Display the result.
                Console.WriteLine("The ParamArray attribute {0} defined for " +
                                  "parameter {1} of method {2}.",
                                  isDef ? "is" : "is not",
                                  pInfo[0].Name,
                                  mInfo.Name);
            }
            else
                Console.WriteLine("The parameters information could " +
                            "not be retrieved for method {0}.", mInfo.Name);
        }
    }
}

/*
 * Output:
 * The ParamArray attribute is defined for parameter args of method Method1.
 */

Se aplica a

.NET 7 e outras versións
Produto Versións
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

IsDefined(Module, Type, Boolean)

Determina si alguno de los atributos personalizados se aplica a un módulo. Los parámetros especifican el módulo, el tipo de atributo personalizado que se va a buscar y una opción de búsqueda omitida.

C#
public static bool IsDefined (System.Reflection.Module element, Type attributeType, bool inherit);

Parámetros

element
Module

Objeto derivado de la clase Module que describe un archivo ejecutable portable.

attributeType
Type

Tipo, o tipo base, del atributo personalizado que se va a buscar.

inherit
Boolean

Este parámetro se omite y no afecta al funcionamiento de este método.

Devoluciones

Boolean

Es true si se aplica a attributeType un atributo personalizado de tipo element; en caso contrario, es false.

Excepciones

element o attributeType es null.

attributeType no se deriva de Attribute.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de IsDefined, tomando como Module parámetro .

C#
using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module,
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module,
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */

Comentarios

Este método omite el inherit parámetro y no busca en los atributos personalizados los antecesores de element .

Se aplica a

.NET 7 e outras versións
Produto Versións
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

IsDefined(MemberInfo, Type, Boolean)

Determina si alguno de los atributos personalizados se aplica a un miembro de un tipo. Los parámetros especifican el miembro, el tipo del atributo personalizado que se va a buscar y si se deben buscar los antecesores del miembro.

C#
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType, bool inherit);

Parámetros

element
MemberInfo

Objeto derivado de la clase MemberInfo que describe un constructor, evento, campo, método, tipo o miembro de propiedad de una clase.

attributeType
Type

Tipo, o tipo base, del atributo personalizado que se va a buscar.

inherit
Boolean

Si es true, especifica que se busquen también los atributos personalizados de los antecesores de element.

Devoluciones

Boolean

Es true si se aplica a attributeType un atributo personalizado de tipo element; en caso contrario, es false.

Excepciones

element o attributeType es null.

attributeType no se deriva de Attribute.

element no es un constructor, método, propiedad, evento, tipo o campo.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de IsDefined, tomando como MemberInfo parámetro .

C#
using System;
using System.Reflection;

namespace IsDef4CS
{
    public class TestClass
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef)
            {
                ObsoleteAttribute obsAttr =
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute(
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */

Comentarios

Nota

A partir de la versión 2.0 de .NET Framework, este método devuelve true si un tipo, método o constructor tiene atributos de seguridad almacenados en el nuevo formato de metadatos. Los ensamblados compilados con la versión 2.0 o posterior usan el nuevo formato. Los ensamblados dinámicos y los ensamblados compilados con versiones anteriores del .NET Framework usan el formato XML antiguo. Consulte Emisión de atributos de seguridad declarativos.

Se aplica a

.NET 7 e outras versións
Produto Versións
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

IsDefined(Assembly, Type, Boolean)

Determina si alguno de los atributos personalizados se aplica a un ensamblado. Los parámetros especifican el ensamblado, el tipo de atributo personalizado que se va a buscar y una opción de búsqueda omitida.

C#
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType, bool inherit);

Parámetros

element
Assembly

Objeto derivado de la clase Assembly que describe una colección reutilizable de módulos.

attributeType
Type

Tipo, o tipo base, del atributo personalizado que se va a buscar.

inherit
Boolean

Este parámetro se omite y no afecta al funcionamiento de este método.

Devoluciones

Boolean

Es true si se aplica a attributeType un atributo personalizado de tipo element; en caso contrario, es false.

Excepciones

element o attributeType es null.

attributeType no se deriva de Attribute.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de IsDefined, tomando como Assembly parámetro .

C#
using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy,
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr =
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".",
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */

Comentarios

Nota

A partir de la versión 2.0 de .NET Framework, este método devuelve true si el ensamblado tiene atributos de seguridad almacenados en el nuevo formato de metadatos. Los ensamblados compilados con la versión 2.0 o posterior usan el nuevo formato. Los ensamblados dinámicos y los ensamblados compilados con versiones anteriores del .NET Framework usan el formato XML antiguo. Consulte Emisión de atributos de seguridad declarativos.

Se aplica a

.NET 7 e outras versións
Produto Versións
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

IsDefined(MemberInfo, Type)

Determina si alguno de los atributos personalizados se aplica a un miembro de un tipo. Los parámetros especifican el miembro y el tipo del atributo personalizado que se va a buscar.

C#
public static bool IsDefined (System.Reflection.MemberInfo element, Type attributeType);

Parámetros

element
MemberInfo

Objeto derivado de la clase MemberInfo que describe un constructor, evento, campo, método, tipo o miembro de propiedad de una clase.

attributeType
Type

Tipo, o tipo base, del atributo personalizado que se va a buscar.

Devoluciones

Boolean

Es true si se aplica a attributeType un atributo personalizado de tipo element; en caso contrario, es false.

Excepciones

element o attributeType es null.

attributeType no se deriva de Attribute.

element no es un constructor, método, propiedad, evento, tipo o campo.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de IsDefined, tomando como MemberInfo parámetro .

C#
using System;
using System.Reflection;

namespace IsDef4CS
{
    public class TestClass
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef)
            {
                ObsoleteAttribute obsAttr =
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute(
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */

Comentarios

Se buscan atributos personalizados en los antecesores de element .

Nota

A partir de la versión 2.0 de .NET Framework, este método devuelve true si un tipo, método o constructor tiene atributos de seguridad almacenados en el nuevo formato de metadatos. Los ensamblados compilados con la versión 2.0 o posterior usan el nuevo formato. Los ensamblados dinámicos y los ensamblados compilados con versiones anteriores del .NET Framework usan el formato XML antiguo. Consulte Emisión de atributos de seguridad declarativos.

Se aplica a

.NET 7 e outras versións
Produto Versións
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

IsDefined(Module, Type)

Determina si se deben aplicar atributos personalizados de un tipo especificado a un módulo. Los parámetros especifican el módulo y el tipo del atributo personalizado que se va a buscar.

C#
public static bool IsDefined (System.Reflection.Module element, Type attributeType);

Parámetros

element
Module

Objeto derivado de la clase Module que describe un archivo ejecutable portable.

attributeType
Type

Tipo, o tipo base, del atributo personalizado que se va a buscar.

Devoluciones

Boolean

Es true si se aplica a attributeType un atributo personalizado de tipo element; en caso contrario, es false.

Excepciones

element o attributeType es null.

attributeType no se deriva de Attribute.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de IsDefined, tomando como Module parámetro .

C#
using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module,
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module,
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */

Comentarios

Los antecesores de element no se buscan atributos personalizados.

Se aplica a

.NET 7 e outras versións
Produto Versións
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

IsDefined(Assembly, Type)

Determina si alguno de los atributos personalizados se aplica a un ensamblado. Los parámetros especifican el ensamblado y el tipo del atributo personalizado que se va a buscar.

C#
public static bool IsDefined (System.Reflection.Assembly element, Type attributeType);

Parámetros

element
Assembly

Objeto derivado de la clase Assembly que describe una colección reutilizable de módulos.

attributeType
Type

Tipo, o tipo base, del atributo personalizado que se va a buscar.

Devoluciones

Boolean

Es true si se aplica a attributeType un atributo personalizado de tipo element; en caso contrario, es false.

Excepciones

element o attributeType es null.

attributeType no se deriva de Attribute.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de IsDefined, tomando como Assembly parámetro .

C#
using System;
using System.Reflection;

// Add an AssemblyDescription attribute
[assembly: AssemblyDescription("A sample description")]
namespace IsDef1CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // Get the assembly object.
            Assembly assy = clsType.Assembly;
            // Store the assembly's name.
            String assyName = assy.GetName().Name;
            // See if the Assembly Description is defined.
            bool isdef = Attribute.IsDefined(assy,
                typeof(AssemblyDescriptionAttribute));
            if (isdef)
            {
                // Affirm that the attribute is defined.
                Console.WriteLine("The AssemblyDescription attribute " +
                    "is defined for assembly {0}.", assyName);
                // Get the description attribute itself.
                AssemblyDescriptionAttribute adAttr =
                    (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(
                    assy, typeof(AssemblyDescriptionAttribute));
                // Display the description.
                if (adAttr != null)
                    Console.WriteLine("The description is \"{0}\".",
                        adAttr.Description);
                else
                    Console.WriteLine("The description could not " +
                        "be retrieved.");
            }
            else
                Console.WriteLine("The AssemblyDescription attribute is not " +
                    "defined for assembly {0}.", assyName);
        }
    }
}

/*
 * Output:
 * The AssemblyDescription attribute is defined for assembly IsDef1CS.
 * The description is "A sample description".
 */

Comentarios

Nota

A partir de la versión 2.0 de .NET Framework, este método devuelve true si el ensamblado tiene atributos de seguridad almacenados en el nuevo formato de metadatos. Los ensamblados compilados con la versión 2.0 o posterior usan el nuevo formato. Los ensamblados dinámicos y los ensamblados compilados con versiones anteriores del .NET Framework usan el formato XML antiguo. Consulte Emisión de atributos de seguridad declarativos.

Se aplica a

.NET 7 e outras versións
Produto Versións
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

IsDefined(ParameterInfo, Type)

Determina si alguno de los atributos personalizados se aplica a un parámetro de método. Los parámetros especifican el parámetro de método y el tipo del atributo personalizado que se va a buscar.

C#
public static bool IsDefined (System.Reflection.ParameterInfo element, Type attributeType);

Parámetros

element
ParameterInfo

Objeto derivado de la clase ParameterInfo que describe un parámetro de un miembro de una clase.

attributeType
Type

Tipo, o tipo base, del atributo personalizado que se va a buscar.

Devoluciones

Boolean

Es true si se aplica a attributeType un atributo personalizado de tipo element; en caso contrario, es false.

Excepciones

element o attributeType es null.

attributeType no se deriva de Attribute.

Ejemplos

En el ejemplo de código siguiente se muestra el uso de IsDefined, tomando como ParameterInfo parámetro .

C#
using System;
using System.Reflection;

namespace IsDef5CS
{
    public class TestClass
    {
        // Assign a ParamArray attribute to the parameter using the keyword.
        public void Method1(params String[] args)
        {}
    }

    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // Get the ParameterInfo array for the method parameters.
            ParameterInfo[] pInfo = mInfo.GetParameters();
            if (pInfo != null)
            {
                // See if the ParamArray attribute is defined.
                bool isDef = Attribute.IsDefined(pInfo[0],
                                                 typeof(ParamArrayAttribute));
                // Display the result.
                Console.WriteLine("The ParamArray attribute {0} defined for " +
                                  "parameter {1} of method {2}.",
                                  isDef ? "is" : "is not",
                                  pInfo[0].Name,
                                  mInfo.Name);
            }
            else
                Console.WriteLine("The parameters information could " +
                            "not be retrieved for method {0}.", mInfo.Name);
        }
    }
}

/*
 * Output:
 * The ParamArray attribute is defined for parameter args of method Method1.
 */

Comentarios

Se buscan atributos personalizados en los antecesores de element .

Se aplica a

.NET 7 e outras versións
Produto Versións
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1