Per Mausklick bewerten und Feedback geben
MSDN
MSDN Library
.NET Entwicklung
.NET Framework 3.5
.NET Framework
System-Namespace
Attribute-Klasse
Attribute-Methoden
 GetCustomAttributes-Methode (Parame...
Alle reduzieren/Alle erweitern Alle reduzieren
Diese Seite ist spezifisch für
Microsoft Visual Studio 2008/.NET Framework 3.5

Andere Versionen stehen ebenfalls zur Verfügung für:
.NET Framework-Klassenbibliothek
Attribute..::.GetCustomAttributes-Methode (ParameterInfo)

Aktualisiert: November 2007

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Deklaration)
Public Shared Function GetCustomAttributes ( _
    element As ParameterInfo _
) As Attribute()
Visual Basic (Verwendung)
Dim element As ParameterInfo
Dim returnValue As Attribute()

returnValue = Attribute.GetCustomAttributes(element)
C#
public static Attribute[] GetCustomAttributes(
    ParameterInfo element
)
VisualC++
public:
static array<Attribute^>^ GetCustomAttributes(
    ParameterInfo^ element
)
J#
public static Attribute[] GetCustomAttributes(
    ParameterInfo element
)
Jscript
public static function GetCustomAttributes(
    element : ParameterInfo
) : Attribute[]

Parameter

element
Typ: System.Reflection..::.ParameterInfo
Ein Objekt, das von der ParameterInfo-Klasse abgeleitet wurde und einen Parameter eines Klassenmembers beschreibt.

Rückgabewert

Typ: array<System..::.Attribute>[]()[]
Ein Attribute-Array mit den benutzerdefinierten Attributen, die für element übernommen werden, oder ein leeres Array, wenn keine benutzerdefinierten Attribute vorhanden sind.
AusnahmeBedingung
ArgumentNullException

element ist nullNothingnullptrNULL-Verweis (Nothing in Visual Basic).

TypeLoadException

Ein benutzerdefinierter Attributtyp kann nicht geladen werden.

Wenn element einen Parameter in einer Methode eines abgeleiteten Typs darstellt, enthält der zurückgegebene Wert die vererbbaren benutzerdefinierten Attribute, die für denselben Parameter in den überschriebenen Basismethoden übernommen wurden.

Im folgenden Codebeispiel wird die Verwendung der GetCustomAttributes-Methode veranschaulicht, die ein ParameterInfo als Parameter annimmt.

Visual Basic
Imports System
Imports System.Reflection
Imports System.ComponentModel

Module DemoModule
    Public Class AClass
        ' Add Description and ParamArray (with the keyword) attributes.
        Public Sub ParamArrayAndDesc( _
            <Description("This argument is a ParamArray")> _
            ByVal ParamArray args() As Integer)
        End Sub
    End Class

    Sub Main()
        ' Get the Class type to access its metadata.
        Dim clsType As Type = GetType(AClass)
        ' Get the type information for the method.
        Dim mInfo As MethodInfo = clsType.GetMethod("ParamArrayAndDesc")
        ' Get the Parameter information for the method.
        Dim pInfo() As ParameterInfo = mInfo.GetParameters()
        Dim attr As Attribute
        ' Iterate through each attribute of the parameter.
        For Each attr In Attribute.GetCustomAttributes(pInfo(0))
            ' Check for the ParamArray attribute.
            If TypeOf attr Is ParamArrayAttribute Then
                ' Convert the attribute to access its data.
                Dim paAttr As ParamArrayAttribute = _
                    CType(attr, ParamArrayAttribute)
                Console.WriteLine("Parameter {0} has the " + _
                    "ParamArray attribute.", pInfo(0).Name)
            ' Check for the Description attribute.
            ElseIf TypeOf attr Is DescriptionAttribute Then
                ' Convert the attribute to access its data.
                Dim descAttr As DescriptionAttribute = _
                    CType(attr, DescriptionAttribute)
                Console.WriteLine("Parameter {0} has a description " + _
                    "attribute. The description is:", pInfo(0).Name)
                Console.WriteLine(descAttr.Description)
            End If
        Next
    End Sub
End Module

' Output:
' Parameter args has a description attribute. The description is:
' This argument is a ParamArray
' Parameter args has the ParamArray attribute.
C#
using System;
using System.Reflection;
using System.ComponentModel;

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

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

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 */
J#
package CustAttrs5JSL; 
import System.*;
import System.Reflection.*;
import System.ComponentModel.*;

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

class DemoClass
{
    public static void main(String[] args)
    {
        // Get the Class type to access its metadata.
        Type clsType = AClass.class.ToType();

        // 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.
                for (int iCtr = 0; iCtr < Attribute.GetCustomAttributes(
                    pInfo[0]).get_Length(); iCtr++) {
                    Attribute attr = (Attribute)Attribute.
                        GetCustomAttributes(pInfo[0]).get_Item(iCtr);
                    // Check for the ParamArray attribute.
                    if (attr.GetType().Equals(ParamArrayAttribute.class.
                        ToType())) {
                        Console.WriteLine("Parameter {0} for method {1} " 
                            + "has the ParamArray attribute.",
                            pInfo[0].get_Name(), mInfo.get_Name());
                    }
                    // Check for the Description attribute.
                    else {
                        if (attr.GetType().Equals(DescriptionAttribute.class.
                            ToType())) {
                            Console.WriteLine("Parameter {0} for method {1} " 
                                + "has a description attribute.", 
                                pInfo[0].get_Name(),mInfo.get_Name());
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)(attr)).
                                get_Description());
                        }
                    }
                }
            }
        }
    } //main
} //DemoClass   

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

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile für Smartphone, Windows Mobile für Pocket PC, Xbox 360

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 3.5, 2.0, 1.0

XNA Framework

Unterstützt in: 2.0, 1.0
Communityinhalt   Was ist Community Content?
Neuen Inhalt hinzufügen RSS  Anmerkungen
Processing
© 2009 Microsoft Corporation. Alle Rechte vorbehalten. Nutzungsbedingungen | Markenzeichen | Informationen zur Datensicherheit
Page view tracker