Este tema aún no ha recibido ninguna valoración - Valorar este tema

Attribute.GetCustomAttributes (Método) (Module, Type)

Recupera una matriz de los atributos personalizados aplicados a un módulo. Los parámetros especifican el módulo y el tipo del atributo personalizado que se va a buscar.

Espacio de nombres: System
Ensamblado: mscorlib (en mscorlib.dll)

public static Attribute[] GetCustomAttributes (
	Module element,
	Type attributeType
)
public static Attribute[] GetCustomAttributes (
	Module element, 
	Type attributeType
)
public static function GetCustomAttributes (
	element : Module, 
	attributeType : Type
) : Attribute[]

Parámetros

element

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

attributeType

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

Valor devuelto

Una matriz de Attribute que contiene los atributos personalizados de tipo attributeType aplicados a element o una matriz vacía si no existen dichos atributos personalizados.
Tipo de excepciónCondición

ArgumentNullException

El valor de element o de attributeType es referencia de objeto null (Nothing en Visual Basic).

ArgumentException

attributeType no se deriva de Attribute.

En el siguiente código de ejemplo se muestra el uso de GetCustomAttributes, que toma Module como parámetro.

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

package CustAttrs2JSL; 
import System.*;
import System.Reflection.*;
import 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)
 */
class DemoClass
{
    public static void main(String[] args)
    {
        Type clsType = DemoClass.class.ToType();

        // Get the Module type to access its metadata.
        Module module = clsType.get_Module();

        // Iterate through all the attributes for the module.
        for (int iCtr = 0; iCtr < Attribute.GetCustomAttributes(module).
            get_Length(); iCtr++) {
            Attribute attr = (Attribute)Attribute.GetCustomAttributes(module).
                get_Item(iCtr);
            // Check for the Description attribute.
            if (attr.GetType().Equals(DescriptionAttribute.class.ToType())) {
                Console.WriteLine("Module {0} has the description " + 
                    "\"{1}\".", module.get_Name(),
                    ((DescriptionAttribute)attr).get_Description());
            }
            // Check for the CLSCompliant attribute.
            else {
                if (attr.GetType().Equals(CLSCompliantAttribute.class.
                    ToType())) {
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.get_Name(), (((CLSCompliantAttribute)attr).
                        get_IsCompliant()) ? "is" : "is not");
                }
            }
        }
    } //main
} //DemoClass   

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

.NET Framework

Compatible con: 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0
¿Te ha resultado útil?
(Caracteres restantes: 1500)

Adiciones de comunidad

AGREGAR
© 2013 Microsoft. Reservados todos los derechos.