CustomAttributeData.GetCustomAttributes 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个 CustomAttributeData 对象列表,这些对象表示有关已应用于目标的特性的数据。
重载
GetCustomAttributes(Assembly) |
返回 CustomAttributeData 对象列表,这些对象表示已应用到目标程序集的特性相关数据。 |
GetCustomAttributes(MemberInfo) |
返回 CustomAttributeData 对象列表,这些对象表示已应用到目标成员的特性相关数据。 |
GetCustomAttributes(Module) |
返回 CustomAttributeData 对象列表,这些对象表示已应用到目标模块的特性相关数据。 |
GetCustomAttributes(ParameterInfo) |
返回 CustomAttributeData 对象列表,这些对象表示已应用到目标参数的特性相关数据。 |
GetCustomAttributes(Assembly)
- Source:
- CustomAttributeData.cs
- Source:
- CustomAttributeData.cs
- Source:
- CustomAttributeData.cs
返回 CustomAttributeData 对象列表,这些对象表示已应用到目标程序集的特性相关数据。
public:
static System::Collections::Generic::IList<System::Reflection::CustomAttributeData ^> ^ GetCustomAttributes(System::Reflection::Assembly ^ target);
public static System.Collections.Generic.IList<System.Reflection.CustomAttributeData> GetCustomAttributes (System.Reflection.Assembly target);
static member GetCustomAttributes : System.Reflection.Assembly -> System.Collections.Generic.IList<System.Reflection.CustomAttributeData>
Public Shared Function GetCustomAttributes (target As Assembly) As IList(Of CustomAttributeData)
参数
- target
- Assembly
要检索其自定义特性数据的程序集。
返回
对象的列表,表示已应用到目标程序集的特性相关数据。
例外
target
为 null
。
示例
以下示例定义了一个具有四个构造函数和四个属性的自定义属性。 其中两个属性是只读的,使用构造函数的位置参数进行设置。 其他两个属性是可读/写的,只能使用命名参数进行设置。 一个位置属性是字符串数组,一个命名属性是整数数组。
该特性可应用于程序集、在该程序集中声明的类型、该类型的方法以及该方法的参数。 这些情况使用不同的构造函数。 执行时,程序集将自身加载到仅反射上下文中,并显示自定义属性。
应用于 类型的属性演示具有位置参数和命名参数的数组属性。
方法 GetCustomAttributes 用于 Main()
获取应用于程序集的自定义属性。 方法的返回值将 ShowAttributeData
传递给 方法。
using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
// An enumeration used by the ExampleAttribute class.
public enum class ExampleKind
{
FirstKind, SecondKind, ThirdKind, FourthKind
};
// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets::All)]
public ref class ExampleAttribute: public Attribute
{
private:
// Data for properties.
ExampleKind kindValue;
String^ noteValue;
array<String^>^ arrayStrings;
array<int>^ arrayNumbers;
// Constructors.
void ExampleAttributeInitialize( ExampleKind initKind, array<String^>^ initStrings )
{
kindValue = initKind;
arrayStrings = initStrings;
}
public:
ExampleAttribute()
{
ExampleAttributeInitialize( ExampleKind::FirstKind, nullptr );
}
ExampleAttribute( ExampleKind initKind )
{
ExampleAttributeInitialize( initKind, nullptr );
}
ExampleAttribute( ExampleKind initKind, array<String^>^ initStrings )
{
ExampleAttributeInitialize( initKind, initStrings );
}
// Properties. The Note and Numbers properties must be read/write, so they
// can be used as named parameters.
//
property ExampleKind Kind
{
ExampleKind get()
{
return kindValue;
}
}
property array<String^>^ Strings
{
array<String^>^ get()
{
return arrayStrings;
}
}
property String^ Note
{
String^ get()
{
return noteValue;
}
void set( String^ value )
{
noteValue = value;
}
}
property array<int>^ Numbers
{
array<int>^ get()
{
return arrayNumbers;
}
void set( array<int>^ value )
{
arrayNumbers = value;
}
}
};
// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind::ThirdKind,Note="This is a note on the assembly.")];
// The example attribute is applied to the test class.
//
[Example(ExampleKind::SecondKind,
gcnew array<String^> { "String array argument, line 1",
"String array argument, line 2",
"String array argument, line 3" },
Note="This is a note on the class.",
Numbers = gcnew array<int> { 53, 57, 59 })]
public ref class Test
{
public:
// The example attribute is applied to a method, using the
// parameterless constructor and supplying a named argument.
// The attribute is also applied to the method parameter.
//
[Example(Note="This is a note on a method.")]
void TestMethod( [Example] Object^ arg ){}
// Main() gets objects representing the assembly, the test
// type, the test method, and the method parameter. Custom
// attribute data is displayed for each of these.
//
static void Main()
{
Assembly^ assembly = Assembly::ReflectionOnlyLoad( "Source" );
Type^ t = assembly->GetType( "Test" );
MethodInfo^ m = t->GetMethod( "TestMethod" );
array<ParameterInfo^>^p = m->GetParameters();
Console::WriteLine( "\r\nAttributes for assembly: '{0}'", assembly );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( assembly ) );
Console::WriteLine( "\r\nAttributes for type: '{0}'", t );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( t ) );
Console::WriteLine( "\r\nAttributes for member: '{0}'", m );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( m ) );
Console::WriteLine( "\r\nAttributes for parameter: '{0}'", p );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( p[ 0 ] ) );
}
private:
static void ShowValueOrArray(CustomAttributeTypedArgument^ cata)
{
if (cata->Value->GetType() == ReadOnlyCollection<CustomAttributeTypedArgument>::typeid)
{
Console::WriteLine(" Array of '{0}':", cata->ArgumentType);
for each (CustomAttributeTypedArgument^ cataElement in
(ReadOnlyCollection<CustomAttributeTypedArgument>^) cata->Value)
{
Console::WriteLine(" Type: '{0}' Value: '{1}'",
cataElement->ArgumentType, cataElement->Value);
}
}
else
{
Console::WriteLine( " Type: '{0}' Value: '{1}'",
cata->ArgumentType, cata->Value );
}
}
static void ShowAttributeData( IList< CustomAttributeData^ >^ attributes )
{
for each ( CustomAttributeData^ cad in attributes )
{
Console::WriteLine( " {0}", cad );
Console::WriteLine( " Constructor: '{0}'", cad->Constructor );
Console::WriteLine( " Constructor arguments:" );
for each ( CustomAttributeTypedArgument^ cata in cad->ConstructorArguments )
{
ShowValueOrArray(cata);
}
Console::WriteLine( " Named arguments:" );
for each ( CustomAttributeNamedArgument cana in cad->NamedArguments )
{
Console::WriteLine( " MemberInfo: '{0}'", cana.MemberInfo );
ShowValueOrArray(cana.TypedValue);
}
}
}
};
int main()
{
Test::Main();
}
/* This code example produces output similar to the following:
Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
Constructor: 'Void .ctor(Int32)'
Constructor arguments:
Type: 'System.Int32' Value: '8'
Named arguments:
[System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'Boolean WrapNonExceptionThrows'
Type: 'System.Boolean' Value: 'True'
[ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
Constructor: 'Void .ctor(ExampleKind)'
Constructor arguments:
Type: 'ExampleKind' Value: '2'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the assembly.'
Attributes for type: 'Test'
[ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
Constructor: 'Void .ctor(ExampleKind, System.String[])'
Constructor arguments:
Type: 'ExampleKind' Value: '1'
Array of 'System.String[]':
Type: 'System.String' Value: 'String array argument, line 1'
Type: 'System.String' Value: 'String array argument, line 2'
Type: 'System.String' Value: 'String array argument, line 3'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the class.'
MemberInfo: 'Int32[] Numbers'
Array of 'System.Int32[]':
Type: 'System.Int32' Value: '53'
Type: 'System.Int32' Value: '57'
Type: 'System.Int32' Value: '59'
Attributes for member: 'Void TestMethod(System.Object)'
[ExampleAttribute(Note = "This is a note on a method.")]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on a method.'
Attributes for parameter: 'System.Object arg'
[ExampleAttribute()]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
*/
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind.ThirdKind, Note="This is a note on the assembly.")]
// An enumeration used by the ExampleAttribute class.
public enum ExampleKind
{
FirstKind,
SecondKind,
ThirdKind,
FourthKind
};
// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets.All)]
public class ExampleAttribute : Attribute
{
// Data for properties.
private ExampleKind kindValue;
private string noteValue;
private string[] arrayStrings;
private int[] arrayNumbers;
// Constructors. The parameterless constructor (.ctor) calls
// the constructor that specifies ExampleKind and an array of
// strings, and supplies the default values.
//
public ExampleAttribute(ExampleKind initKind, string[] initStrings)
{
kindValue = initKind;
arrayStrings = initStrings;
}
public ExampleAttribute(ExampleKind initKind) : this(initKind, null) {}
public ExampleAttribute() : this(ExampleKind.FirstKind, null) {}
// Properties. The Note and Numbers properties must be read/write, so they
// can be used as named parameters.
//
public ExampleKind Kind { get { return kindValue; }}
public string[] Strings { get { return arrayStrings; }}
public string Note
{
get { return noteValue; }
set { noteValue = value; }
}
public int[] Numbers
{
get { return arrayNumbers; }
set { arrayNumbers = value; }
}
}
// The example attribute is applied to the test class.
//
[Example(ExampleKind.SecondKind,
new string[] { "String array argument, line 1",
"String array argument, line 2",
"String array argument, line 3" },
Note="This is a note on the class.",
Numbers = new int[] { 53, 57, 59 })]
public class Test
{
// The example attribute is applied to a method, using the
// parameterless constructor and supplying a named argument.
// The attribute is also applied to the method parameter.
//
[Example(Note="This is a note on a method.")]
public void TestMethod([Example] object arg) { }
// Main() gets objects representing the assembly, the test
// type, the test method, and the method parameter. Custom
// attribute data is displayed for each of these.
//
public static void Main()
{
Assembly asm = Assembly.ReflectionOnlyLoad("Source");
Type t = asm.GetType("Test");
MethodInfo m = t.GetMethod("TestMethod");
ParameterInfo[] p = m.GetParameters();
Console.WriteLine("\r\nAttributes for assembly: '{0}'", asm);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm));
Console.WriteLine("\r\nAttributes for type: '{0}'", t);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(t));
Console.WriteLine("\r\nAttributes for member: '{0}'", m);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(m));
Console.WriteLine("\r\nAttributes for parameter: '{0}'", p);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(p[0]));
}
private static void ShowAttributeData(
IList<CustomAttributeData> attributes)
{
foreach( CustomAttributeData cad in attributes )
{
Console.WriteLine(" {0}", cad);
Console.WriteLine(" Constructor: '{0}'", cad.Constructor);
Console.WriteLine(" Constructor arguments:");
foreach( CustomAttributeTypedArgument cata
in cad.ConstructorArguments )
{
ShowValueOrArray(cata);
}
Console.WriteLine(" Named arguments:");
foreach( CustomAttributeNamedArgument cana
in cad.NamedArguments )
{
Console.WriteLine(" MemberInfo: '{0}'",
cana.MemberInfo);
ShowValueOrArray(cana.TypedValue);
}
}
}
private static void ShowValueOrArray(CustomAttributeTypedArgument cata)
{
if (cata.Value.GetType() == typeof(ReadOnlyCollection<CustomAttributeTypedArgument>))
{
Console.WriteLine(" Array of '{0}':", cata.ArgumentType);
foreach (CustomAttributeTypedArgument cataElement in
(ReadOnlyCollection<CustomAttributeTypedArgument>) cata.Value)
{
Console.WriteLine(" Type: '{0}' Value: '{1}'",
cataElement.ArgumentType, cataElement.Value);
}
}
else
{
Console.WriteLine(" Type: '{0}' Value: '{1}'",
cata.ArgumentType, cata.Value);
}
}
}
/* This code example produces output similar to the following:
Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
Constructor: 'Void .ctor(Int32)'
Constructor arguments:
Type: 'System.Int32' Value: '8'
Named arguments:
[System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'Boolean WrapNonExceptionThrows'
Type: 'System.Boolean' Value: 'True'
[ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
Constructor: 'Void .ctor(ExampleKind)'
Constructor arguments:
Type: 'ExampleKind' Value: '2'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the assembly.'
Attributes for type: 'Test'
[ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
Constructor: 'Void .ctor(ExampleKind, System.String[])'
Constructor arguments:
Type: 'ExampleKind' Value: '1'
Array of 'System.String[]':
Type: 'System.String' Value: 'String array argument, line 1'
Type: 'System.String' Value: 'String array argument, line 2'
Type: 'System.String' Value: 'String array argument, line 3'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the class.'
MemberInfo: 'Int32[] Numbers'
Array of 'System.Int32[]':
Type: 'System.Int32' Value: '53'
Type: 'System.Int32' Value: '57'
Type: 'System.Int32' Value: '59'
Attributes for member: 'Void TestMethod(System.Object)'
[ExampleAttribute(Note = "This is a note on a method.")]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on a method.'
Attributes for parameter: 'System.Object arg'
[ExampleAttribute()]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
*/
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
' The example attribute is applied to the assembly.
<Assembly:Example(ExampleKind.ThirdKind, Note:="This is a note on the assembly.")>
' An enumeration used by the ExampleAttribute class.
Public Enum ExampleKind
FirstKind
SecondKind
ThirdKind
FourthKind
End Enum
' An example attribute. The attribute can be applied to all
' targets, from assemblies to parameters.
'
<AttributeUsage(AttributeTargets.All)> _
Public Class ExampleAttribute
Inherits Attribute
' Data for properties.
Private kindValue As ExampleKind
Private noteValue As String
Private arrayStrings() As String
Private arrayNumbers() As Integer
' Constructors. The parameterless constructor (.ctor) calls
' the constructor that specifies ExampleKind and an array of
' strings, and supplies the default values.
'
Public Sub New(ByVal initKind As ExampleKind, ByVal initStrings() As String)
kindValue = initKind
arrayStrings = initStrings
End Sub
Public Sub New(ByVal initKind As ExampleKind)
Me.New(initKind, Nothing)
End Sub
Public Sub New()
Me.New(ExampleKind.FirstKind, Nothing)
End Sub
' Properties. The Note and Numbers properties must be read/write, so they
' can be used as named parameters.
'
Public ReadOnly Property Kind As ExampleKind
Get
Return kindValue
End Get
End Property
Public ReadOnly Property Strings As String()
Get
Return arrayStrings
End Get
End Property
Public Property Note As String
Get
Return noteValue
End Get
Set
noteValue = value
End Set
End Property
Public Property Numbers As Integer()
Get
Return arrayNumbers
End Get
Set
arrayNumbers = value
End Set
End Property
End Class
' The example attribute is applied to the test class.
'
<Example(ExampleKind.SecondKind, _
New String() { "String array argument, line 1", _
"String array argument, line 2", _
"String array argument, line 3" }, _
Note := "This is a note on the class.", _
Numbers := New Integer() { 53, 57, 59 })> _
Public Class Test
' The example attribute is applied to a method, using the
' parameterless constructor and supplying a named argument.
' The attribute is also applied to the method parameter.
'
<Example(Note:="This is a note on a method.")> _
Public Sub TestMethod(<Example()> ByVal arg As Object)
End Sub
' Sub Main gets objects representing the assembly, the test
' type, the test method, and the method parameter. Custom
' attribute data is displayed for each of these.
'
Public Shared Sub Main()
Dim asm As [Assembly] = Assembly.ReflectionOnlyLoad("source")
Dim t As Type = asm.GetType("Test")
Dim m As MethodInfo = t.GetMethod("TestMethod")
Dim p() As ParameterInfo = m.GetParameters()
Console.WriteLine(vbCrLf & "Attributes for assembly: '{0}'", asm)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm))
Console.WriteLine(vbCrLf & "Attributes for type: '{0}'", t)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(t))
Console.WriteLine(vbCrLf & "Attributes for member: '{0}'", m)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(m))
Console.WriteLine(vbCrLf & "Attributes for parameter: '{0}'", p)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(p(0)))
End Sub
Private Shared Sub ShowAttributeData( _
ByVal attributes As IList(Of CustomAttributeData))
For Each cad As CustomAttributeData _
In CType(attributes, IEnumerable(Of CustomAttributeData))
Console.WriteLine(" {0}", cad)
Console.WriteLine(" Constructor: '{0}'", cad.Constructor)
Console.WriteLine(" Constructor arguments:")
For Each cata As CustomAttributeTypedArgument _
In CType(cad.ConstructorArguments, IEnumerable(Of CustomAttributeTypedArgument))
ShowValueOrArray(cata)
Next
Console.WriteLine(" Named arguments:")
For Each cana As CustomAttributeNamedArgument _
In CType(cad.NamedArguments, IEnumerable(Of CustomAttributeNamedArgument))
Console.WriteLine(" MemberInfo: '{0}'", _
cana.MemberInfo)
ShowValueOrArray(cana.TypedValue)
Next
Next
End Sub
Private Shared Sub ShowValueOrArray(ByVal cata As CustomAttributeTypedArgument)
If cata.Value.GetType() Is GetType(ReadOnlyCollection(Of CustomAttributeTypedArgument)) Then
Console.WriteLine(" Array of '{0}':", cata.ArgumentType)
For Each cataElement As CustomAttributeTypedArgument In cata.Value
Console.WriteLine(" Type: '{0}' Value: '{1}'", _
cataElement.ArgumentType, cataElement.Value)
Next
Else
Console.WriteLine(" Type: '{0}' Value: '{1}'", _
cata.ArgumentType, cata.Value)
End If
End Sub
End Class
' This code example produces output similar to the following:
'
'Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
' [System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
' Constructor: 'Void .ctor(Int32)'
' Constructor arguments:
' Type: 'System.Int32' Value: '8'
' Named arguments:
' [System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
' MemberInfo: 'Boolean WrapNonExceptionThrows'
' Type: 'System.Boolean' Value: 'True'
' [ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
' Constructor: 'Void .ctor(ExampleKind)'
' Constructor arguments:
' Type: 'ExampleKind' Value: '2'
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on the assembly.'
'
'Attributes for type: 'Test'
' [ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
' Constructor: 'Void .ctor(ExampleKind, System.String[])'
' Constructor arguments:
' Type: 'ExampleKind' Value: '1'
' Array of 'System.String[]':
' Type: 'System.String' Value: 'String array argument, line 1'
' Type: 'System.String' Value: 'String array argument, line 2'
' Type: 'System.String' Value: 'String array argument, line 3'
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on the class.'
' MemberInfo: 'Int32[] Numbers'
' Array of 'System.Int32[]':
' Type: 'System.Int32' Value: '53'
' Type: 'System.Int32' Value: '57'
' Type: 'System.Int32' Value: '59'
'
'Attributes for member: 'Void TestMethod(System.Object)'
' [ExampleAttribute(Note = "This is a note on a method.")]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on a method.'
'
'Attributes for parameter: 'System.Object arg'
' [ExampleAttribute()]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
注解
此方法提供了方法的替代方法, GetCustomAttributes 该方法始终可以在仅反射上下文中使用。
另请参阅
适用于
GetCustomAttributes(MemberInfo)
- Source:
- CustomAttributeData.cs
- Source:
- CustomAttributeData.cs
- Source:
- CustomAttributeData.cs
返回 CustomAttributeData 对象列表,这些对象表示已应用到目标成员的特性相关数据。
public:
static System::Collections::Generic::IList<System::Reflection::CustomAttributeData ^> ^ GetCustomAttributes(System::Reflection::MemberInfo ^ target);
public static System.Collections.Generic.IList<System.Reflection.CustomAttributeData> GetCustomAttributes (System.Reflection.MemberInfo target);
static member GetCustomAttributes : System.Reflection.MemberInfo -> System.Collections.Generic.IList<System.Reflection.CustomAttributeData>
Public Shared Function GetCustomAttributes (target As MemberInfo) As IList(Of CustomAttributeData)
参数
- target
- MemberInfo
要检索其特性数据的成员。
返回
对象的列表,表示已应用到目标成员的特性相关数据。
例外
target
为 null
。
示例
以下示例定义了一个具有四个构造函数和四个属性的自定义属性。 其中两个属性是只读的,使用构造函数的位置参数进行设置。 其他两个属性是可读/写的,只能使用命名参数进行设置。 一个位置属性是字符串数组,一个命名属性是整数数组。
该特性可应用于程序集、在该程序集中声明的类型、该类型的方法以及该方法的参数。 这些情况使用不同的构造函数。 执行时,程序集将自身加载到仅反射上下文中,并显示自定义属性。
应用于 类型的属性演示具有位置参数和命名参数的数组属性。
方法 GetCustomAttributes 用于 Main()
获取应用于类型和测试方法的自定义属性, (Type 派生自 MemberInfo) 。 的返回值 GetCustomAttributes 将 ShowAttributeData
传递给 方法。
using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
// An enumeration used by the ExampleAttribute class.
public enum class ExampleKind
{
FirstKind, SecondKind, ThirdKind, FourthKind
};
// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets::All)]
public ref class ExampleAttribute: public Attribute
{
private:
// Data for properties.
ExampleKind kindValue;
String^ noteValue;
array<String^>^ arrayStrings;
array<int>^ arrayNumbers;
// Constructors.
void ExampleAttributeInitialize( ExampleKind initKind, array<String^>^ initStrings )
{
kindValue = initKind;
arrayStrings = initStrings;
}
public:
ExampleAttribute()
{
ExampleAttributeInitialize( ExampleKind::FirstKind, nullptr );
}
ExampleAttribute( ExampleKind initKind )
{
ExampleAttributeInitialize( initKind, nullptr );
}
ExampleAttribute( ExampleKind initKind, array<String^>^ initStrings )
{
ExampleAttributeInitialize( initKind, initStrings );
}
// Properties. The Note and Numbers properties must be read/write, so they
// can be used as named parameters.
//
property ExampleKind Kind
{
ExampleKind get()
{
return kindValue;
}
}
property array<String^>^ Strings
{
array<String^>^ get()
{
return arrayStrings;
}
}
property String^ Note
{
String^ get()
{
return noteValue;
}
void set( String^ value )
{
noteValue = value;
}
}
property array<int>^ Numbers
{
array<int>^ get()
{
return arrayNumbers;
}
void set( array<int>^ value )
{
arrayNumbers = value;
}
}
};
// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind::ThirdKind,Note="This is a note on the assembly.")];
// The example attribute is applied to the test class.
//
[Example(ExampleKind::SecondKind,
gcnew array<String^> { "String array argument, line 1",
"String array argument, line 2",
"String array argument, line 3" },
Note="This is a note on the class.",
Numbers = gcnew array<int> { 53, 57, 59 })]
public ref class Test
{
public:
// The example attribute is applied to a method, using the
// parameterless constructor and supplying a named argument.
// The attribute is also applied to the method parameter.
//
[Example(Note="This is a note on a method.")]
void TestMethod( [Example] Object^ arg ){}
// Main() gets objects representing the assembly, the test
// type, the test method, and the method parameter. Custom
// attribute data is displayed for each of these.
//
static void Main()
{
Assembly^ assembly = Assembly::ReflectionOnlyLoad( "Source" );
Type^ t = assembly->GetType( "Test" );
MethodInfo^ m = t->GetMethod( "TestMethod" );
array<ParameterInfo^>^p = m->GetParameters();
Console::WriteLine( "\r\nAttributes for assembly: '{0}'", assembly );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( assembly ) );
Console::WriteLine( "\r\nAttributes for type: '{0}'", t );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( t ) );
Console::WriteLine( "\r\nAttributes for member: '{0}'", m );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( m ) );
Console::WriteLine( "\r\nAttributes for parameter: '{0}'", p );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( p[ 0 ] ) );
}
private:
static void ShowValueOrArray(CustomAttributeTypedArgument^ cata)
{
if (cata->Value->GetType() == ReadOnlyCollection<CustomAttributeTypedArgument>::typeid)
{
Console::WriteLine(" Array of '{0}':", cata->ArgumentType);
for each (CustomAttributeTypedArgument^ cataElement in
(ReadOnlyCollection<CustomAttributeTypedArgument>^) cata->Value)
{
Console::WriteLine(" Type: '{0}' Value: '{1}'",
cataElement->ArgumentType, cataElement->Value);
}
}
else
{
Console::WriteLine( " Type: '{0}' Value: '{1}'",
cata->ArgumentType, cata->Value );
}
}
static void ShowAttributeData( IList< CustomAttributeData^ >^ attributes )
{
for each ( CustomAttributeData^ cad in attributes )
{
Console::WriteLine( " {0}", cad );
Console::WriteLine( " Constructor: '{0}'", cad->Constructor );
Console::WriteLine( " Constructor arguments:" );
for each ( CustomAttributeTypedArgument^ cata in cad->ConstructorArguments )
{
ShowValueOrArray(cata);
}
Console::WriteLine( " Named arguments:" );
for each ( CustomAttributeNamedArgument cana in cad->NamedArguments )
{
Console::WriteLine( " MemberInfo: '{0}'", cana.MemberInfo );
ShowValueOrArray(cana.TypedValue);
}
}
}
};
int main()
{
Test::Main();
}
/* This code example produces output similar to the following:
Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
Constructor: 'Void .ctor(Int32)'
Constructor arguments:
Type: 'System.Int32' Value: '8'
Named arguments:
[System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'Boolean WrapNonExceptionThrows'
Type: 'System.Boolean' Value: 'True'
[ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
Constructor: 'Void .ctor(ExampleKind)'
Constructor arguments:
Type: 'ExampleKind' Value: '2'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the assembly.'
Attributes for type: 'Test'
[ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
Constructor: 'Void .ctor(ExampleKind, System.String[])'
Constructor arguments:
Type: 'ExampleKind' Value: '1'
Array of 'System.String[]':
Type: 'System.String' Value: 'String array argument, line 1'
Type: 'System.String' Value: 'String array argument, line 2'
Type: 'System.String' Value: 'String array argument, line 3'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the class.'
MemberInfo: 'Int32[] Numbers'
Array of 'System.Int32[]':
Type: 'System.Int32' Value: '53'
Type: 'System.Int32' Value: '57'
Type: 'System.Int32' Value: '59'
Attributes for member: 'Void TestMethod(System.Object)'
[ExampleAttribute(Note = "This is a note on a method.")]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on a method.'
Attributes for parameter: 'System.Object arg'
[ExampleAttribute()]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
*/
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind.ThirdKind, Note="This is a note on the assembly.")]
// An enumeration used by the ExampleAttribute class.
public enum ExampleKind
{
FirstKind,
SecondKind,
ThirdKind,
FourthKind
};
// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets.All)]
public class ExampleAttribute : Attribute
{
// Data for properties.
private ExampleKind kindValue;
private string noteValue;
private string[] arrayStrings;
private int[] arrayNumbers;
// Constructors. The parameterless constructor (.ctor) calls
// the constructor that specifies ExampleKind and an array of
// strings, and supplies the default values.
//
public ExampleAttribute(ExampleKind initKind, string[] initStrings)
{
kindValue = initKind;
arrayStrings = initStrings;
}
public ExampleAttribute(ExampleKind initKind) : this(initKind, null) {}
public ExampleAttribute() : this(ExampleKind.FirstKind, null) {}
// Properties. The Note and Numbers properties must be read/write, so they
// can be used as named parameters.
//
public ExampleKind Kind { get { return kindValue; }}
public string[] Strings { get { return arrayStrings; }}
public string Note
{
get { return noteValue; }
set { noteValue = value; }
}
public int[] Numbers
{
get { return arrayNumbers; }
set { arrayNumbers = value; }
}
}
// The example attribute is applied to the test class.
//
[Example(ExampleKind.SecondKind,
new string[] { "String array argument, line 1",
"String array argument, line 2",
"String array argument, line 3" },
Note="This is a note on the class.",
Numbers = new int[] { 53, 57, 59 })]
public class Test
{
// The example attribute is applied to a method, using the
// parameterless constructor and supplying a named argument.
// The attribute is also applied to the method parameter.
//
[Example(Note="This is a note on a method.")]
public void TestMethod([Example] object arg) { }
// Main() gets objects representing the assembly, the test
// type, the test method, and the method parameter. Custom
// attribute data is displayed for each of these.
//
public static void Main()
{
Assembly asm = Assembly.ReflectionOnlyLoad("Source");
Type t = asm.GetType("Test");
MethodInfo m = t.GetMethod("TestMethod");
ParameterInfo[] p = m.GetParameters();
Console.WriteLine("\r\nAttributes for assembly: '{0}'", asm);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm));
Console.WriteLine("\r\nAttributes for type: '{0}'", t);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(t));
Console.WriteLine("\r\nAttributes for member: '{0}'", m);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(m));
Console.WriteLine("\r\nAttributes for parameter: '{0}'", p);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(p[0]));
}
private static void ShowAttributeData(
IList<CustomAttributeData> attributes)
{
foreach( CustomAttributeData cad in attributes )
{
Console.WriteLine(" {0}", cad);
Console.WriteLine(" Constructor: '{0}'", cad.Constructor);
Console.WriteLine(" Constructor arguments:");
foreach( CustomAttributeTypedArgument cata
in cad.ConstructorArguments )
{
ShowValueOrArray(cata);
}
Console.WriteLine(" Named arguments:");
foreach( CustomAttributeNamedArgument cana
in cad.NamedArguments )
{
Console.WriteLine(" MemberInfo: '{0}'",
cana.MemberInfo);
ShowValueOrArray(cana.TypedValue);
}
}
}
private static void ShowValueOrArray(CustomAttributeTypedArgument cata)
{
if (cata.Value.GetType() == typeof(ReadOnlyCollection<CustomAttributeTypedArgument>))
{
Console.WriteLine(" Array of '{0}':", cata.ArgumentType);
foreach (CustomAttributeTypedArgument cataElement in
(ReadOnlyCollection<CustomAttributeTypedArgument>) cata.Value)
{
Console.WriteLine(" Type: '{0}' Value: '{1}'",
cataElement.ArgumentType, cataElement.Value);
}
}
else
{
Console.WriteLine(" Type: '{0}' Value: '{1}'",
cata.ArgumentType, cata.Value);
}
}
}
/* This code example produces output similar to the following:
Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
Constructor: 'Void .ctor(Int32)'
Constructor arguments:
Type: 'System.Int32' Value: '8'
Named arguments:
[System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'Boolean WrapNonExceptionThrows'
Type: 'System.Boolean' Value: 'True'
[ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
Constructor: 'Void .ctor(ExampleKind)'
Constructor arguments:
Type: 'ExampleKind' Value: '2'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the assembly.'
Attributes for type: 'Test'
[ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
Constructor: 'Void .ctor(ExampleKind, System.String[])'
Constructor arguments:
Type: 'ExampleKind' Value: '1'
Array of 'System.String[]':
Type: 'System.String' Value: 'String array argument, line 1'
Type: 'System.String' Value: 'String array argument, line 2'
Type: 'System.String' Value: 'String array argument, line 3'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the class.'
MemberInfo: 'Int32[] Numbers'
Array of 'System.Int32[]':
Type: 'System.Int32' Value: '53'
Type: 'System.Int32' Value: '57'
Type: 'System.Int32' Value: '59'
Attributes for member: 'Void TestMethod(System.Object)'
[ExampleAttribute(Note = "This is a note on a method.")]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on a method.'
Attributes for parameter: 'System.Object arg'
[ExampleAttribute()]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
*/
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
' The example attribute is applied to the assembly.
<Assembly:Example(ExampleKind.ThirdKind, Note:="This is a note on the assembly.")>
' An enumeration used by the ExampleAttribute class.
Public Enum ExampleKind
FirstKind
SecondKind
ThirdKind
FourthKind
End Enum
' An example attribute. The attribute can be applied to all
' targets, from assemblies to parameters.
'
<AttributeUsage(AttributeTargets.All)> _
Public Class ExampleAttribute
Inherits Attribute
' Data for properties.
Private kindValue As ExampleKind
Private noteValue As String
Private arrayStrings() As String
Private arrayNumbers() As Integer
' Constructors. The parameterless constructor (.ctor) calls
' the constructor that specifies ExampleKind and an array of
' strings, and supplies the default values.
'
Public Sub New(ByVal initKind As ExampleKind, ByVal initStrings() As String)
kindValue = initKind
arrayStrings = initStrings
End Sub
Public Sub New(ByVal initKind As ExampleKind)
Me.New(initKind, Nothing)
End Sub
Public Sub New()
Me.New(ExampleKind.FirstKind, Nothing)
End Sub
' Properties. The Note and Numbers properties must be read/write, so they
' can be used as named parameters.
'
Public ReadOnly Property Kind As ExampleKind
Get
Return kindValue
End Get
End Property
Public ReadOnly Property Strings As String()
Get
Return arrayStrings
End Get
End Property
Public Property Note As String
Get
Return noteValue
End Get
Set
noteValue = value
End Set
End Property
Public Property Numbers As Integer()
Get
Return arrayNumbers
End Get
Set
arrayNumbers = value
End Set
End Property
End Class
' The example attribute is applied to the test class.
'
<Example(ExampleKind.SecondKind, _
New String() { "String array argument, line 1", _
"String array argument, line 2", _
"String array argument, line 3" }, _
Note := "This is a note on the class.", _
Numbers := New Integer() { 53, 57, 59 })> _
Public Class Test
' The example attribute is applied to a method, using the
' parameterless constructor and supplying a named argument.
' The attribute is also applied to the method parameter.
'
<Example(Note:="This is a note on a method.")> _
Public Sub TestMethod(<Example()> ByVal arg As Object)
End Sub
' Sub Main gets objects representing the assembly, the test
' type, the test method, and the method parameter. Custom
' attribute data is displayed for each of these.
'
Public Shared Sub Main()
Dim asm As [Assembly] = Assembly.ReflectionOnlyLoad("source")
Dim t As Type = asm.GetType("Test")
Dim m As MethodInfo = t.GetMethod("TestMethod")
Dim p() As ParameterInfo = m.GetParameters()
Console.WriteLine(vbCrLf & "Attributes for assembly: '{0}'", asm)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm))
Console.WriteLine(vbCrLf & "Attributes for type: '{0}'", t)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(t))
Console.WriteLine(vbCrLf & "Attributes for member: '{0}'", m)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(m))
Console.WriteLine(vbCrLf & "Attributes for parameter: '{0}'", p)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(p(0)))
End Sub
Private Shared Sub ShowAttributeData( _
ByVal attributes As IList(Of CustomAttributeData))
For Each cad As CustomAttributeData _
In CType(attributes, IEnumerable(Of CustomAttributeData))
Console.WriteLine(" {0}", cad)
Console.WriteLine(" Constructor: '{0}'", cad.Constructor)
Console.WriteLine(" Constructor arguments:")
For Each cata As CustomAttributeTypedArgument _
In CType(cad.ConstructorArguments, IEnumerable(Of CustomAttributeTypedArgument))
ShowValueOrArray(cata)
Next
Console.WriteLine(" Named arguments:")
For Each cana As CustomAttributeNamedArgument _
In CType(cad.NamedArguments, IEnumerable(Of CustomAttributeNamedArgument))
Console.WriteLine(" MemberInfo: '{0}'", _
cana.MemberInfo)
ShowValueOrArray(cana.TypedValue)
Next
Next
End Sub
Private Shared Sub ShowValueOrArray(ByVal cata As CustomAttributeTypedArgument)
If cata.Value.GetType() Is GetType(ReadOnlyCollection(Of CustomAttributeTypedArgument)) Then
Console.WriteLine(" Array of '{0}':", cata.ArgumentType)
For Each cataElement As CustomAttributeTypedArgument In cata.Value
Console.WriteLine(" Type: '{0}' Value: '{1}'", _
cataElement.ArgumentType, cataElement.Value)
Next
Else
Console.WriteLine(" Type: '{0}' Value: '{1}'", _
cata.ArgumentType, cata.Value)
End If
End Sub
End Class
' This code example produces output similar to the following:
'
'Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
' [System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
' Constructor: 'Void .ctor(Int32)'
' Constructor arguments:
' Type: 'System.Int32' Value: '8'
' Named arguments:
' [System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
' MemberInfo: 'Boolean WrapNonExceptionThrows'
' Type: 'System.Boolean' Value: 'True'
' [ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
' Constructor: 'Void .ctor(ExampleKind)'
' Constructor arguments:
' Type: 'ExampleKind' Value: '2'
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on the assembly.'
'
'Attributes for type: 'Test'
' [ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
' Constructor: 'Void .ctor(ExampleKind, System.String[])'
' Constructor arguments:
' Type: 'ExampleKind' Value: '1'
' Array of 'System.String[]':
' Type: 'System.String' Value: 'String array argument, line 1'
' Type: 'System.String' Value: 'String array argument, line 2'
' Type: 'System.String' Value: 'String array argument, line 3'
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on the class.'
' MemberInfo: 'Int32[] Numbers'
' Array of 'System.Int32[]':
' Type: 'System.Int32' Value: '53'
' Type: 'System.Int32' Value: '57'
' Type: 'System.Int32' Value: '59'
'
'Attributes for member: 'Void TestMethod(System.Object)'
' [ExampleAttribute(Note = "This is a note on a method.")]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on a method.'
'
'Attributes for parameter: 'System.Object arg'
' [ExampleAttribute()]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
注解
此方法重载获取类型、嵌套类型和类型成员的自定义属性数据,因为 Type 命名空间中 System.Reflection 表示类型成员的类和类都派生自 MemberInfo。
另请参阅
适用于
GetCustomAttributes(Module)
- Source:
- CustomAttributeData.cs
- Source:
- CustomAttributeData.cs
- Source:
- CustomAttributeData.cs
返回 CustomAttributeData 对象列表,这些对象表示已应用到目标模块的特性相关数据。
public:
static System::Collections::Generic::IList<System::Reflection::CustomAttributeData ^> ^ GetCustomAttributes(System::Reflection::Module ^ target);
public static System.Collections.Generic.IList<System.Reflection.CustomAttributeData> GetCustomAttributes (System.Reflection.Module target);
static member GetCustomAttributes : System.Reflection.Module -> System.Collections.Generic.IList<System.Reflection.CustomAttributeData>
Public Shared Function GetCustomAttributes (target As Module) As IList(Of CustomAttributeData)
参数
- target
- Module
要检索其自定义特性数据的模块。
返回
对象的列表,表示已应用到目标模块的特性相关数据。
例外
target
为 null
。
适用于
GetCustomAttributes(ParameterInfo)
- Source:
- CustomAttributeData.cs
- Source:
- CustomAttributeData.cs
- Source:
- CustomAttributeData.cs
返回 CustomAttributeData 对象列表,这些对象表示已应用到目标参数的特性相关数据。
public:
static System::Collections::Generic::IList<System::Reflection::CustomAttributeData ^> ^ GetCustomAttributes(System::Reflection::ParameterInfo ^ target);
public static System.Collections.Generic.IList<System.Reflection.CustomAttributeData> GetCustomAttributes (System.Reflection.ParameterInfo target);
static member GetCustomAttributes : System.Reflection.ParameterInfo -> System.Collections.Generic.IList<System.Reflection.CustomAttributeData>
Public Shared Function GetCustomAttributes (target As ParameterInfo) As IList(Of CustomAttributeData)
参数
- target
- ParameterInfo
要检索其特性数据的参数。
返回
对象的列表,表示已应用到目标参数的特性相关数据。
例外
target
为 null
。
示例
以下示例定义了一个具有四个构造函数和四个属性的自定义属性。 其中两个属性是只读的,使用构造函数的位置参数进行设置。 其他两个属性是可读/写的,只能使用命名参数进行设置。 一个位置属性是字符串数组,一个命名属性是整数数组。
该特性可应用于程序集、在该程序集中声明的类型、该类型的方法以及该方法的参数。 这些情况使用不同的构造函数。 执行时,程序集将自身加载到仅反射上下文中,并显示自定义属性。
应用于 类型的属性演示具有位置参数和命名参数的数组属性。
方法 GetCustomAttributes 用于检索 Main()
应用于测试方法参数的自定义属性。 的返回值 GetCustomAttributes 将 ShowAttributeData
传递给 方法。
using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
// An enumeration used by the ExampleAttribute class.
public enum class ExampleKind
{
FirstKind, SecondKind, ThirdKind, FourthKind
};
// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets::All)]
public ref class ExampleAttribute: public Attribute
{
private:
// Data for properties.
ExampleKind kindValue;
String^ noteValue;
array<String^>^ arrayStrings;
array<int>^ arrayNumbers;
// Constructors.
void ExampleAttributeInitialize( ExampleKind initKind, array<String^>^ initStrings )
{
kindValue = initKind;
arrayStrings = initStrings;
}
public:
ExampleAttribute()
{
ExampleAttributeInitialize( ExampleKind::FirstKind, nullptr );
}
ExampleAttribute( ExampleKind initKind )
{
ExampleAttributeInitialize( initKind, nullptr );
}
ExampleAttribute( ExampleKind initKind, array<String^>^ initStrings )
{
ExampleAttributeInitialize( initKind, initStrings );
}
// Properties. The Note and Numbers properties must be read/write, so they
// can be used as named parameters.
//
property ExampleKind Kind
{
ExampleKind get()
{
return kindValue;
}
}
property array<String^>^ Strings
{
array<String^>^ get()
{
return arrayStrings;
}
}
property String^ Note
{
String^ get()
{
return noteValue;
}
void set( String^ value )
{
noteValue = value;
}
}
property array<int>^ Numbers
{
array<int>^ get()
{
return arrayNumbers;
}
void set( array<int>^ value )
{
arrayNumbers = value;
}
}
};
// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind::ThirdKind,Note="This is a note on the assembly.")];
// The example attribute is applied to the test class.
//
[Example(ExampleKind::SecondKind,
gcnew array<String^> { "String array argument, line 1",
"String array argument, line 2",
"String array argument, line 3" },
Note="This is a note on the class.",
Numbers = gcnew array<int> { 53, 57, 59 })]
public ref class Test
{
public:
// The example attribute is applied to a method, using the
// parameterless constructor and supplying a named argument.
// The attribute is also applied to the method parameter.
//
[Example(Note="This is a note on a method.")]
void TestMethod( [Example] Object^ arg ){}
// Main() gets objects representing the assembly, the test
// type, the test method, and the method parameter. Custom
// attribute data is displayed for each of these.
//
static void Main()
{
Assembly^ assembly = Assembly::ReflectionOnlyLoad( "Source" );
Type^ t = assembly->GetType( "Test" );
MethodInfo^ m = t->GetMethod( "TestMethod" );
array<ParameterInfo^>^p = m->GetParameters();
Console::WriteLine( "\r\nAttributes for assembly: '{0}'", assembly );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( assembly ) );
Console::WriteLine( "\r\nAttributes for type: '{0}'", t );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( t ) );
Console::WriteLine( "\r\nAttributes for member: '{0}'", m );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( m ) );
Console::WriteLine( "\r\nAttributes for parameter: '{0}'", p );
ShowAttributeData( CustomAttributeData::GetCustomAttributes( p[ 0 ] ) );
}
private:
static void ShowValueOrArray(CustomAttributeTypedArgument^ cata)
{
if (cata->Value->GetType() == ReadOnlyCollection<CustomAttributeTypedArgument>::typeid)
{
Console::WriteLine(" Array of '{0}':", cata->ArgumentType);
for each (CustomAttributeTypedArgument^ cataElement in
(ReadOnlyCollection<CustomAttributeTypedArgument>^) cata->Value)
{
Console::WriteLine(" Type: '{0}' Value: '{1}'",
cataElement->ArgumentType, cataElement->Value);
}
}
else
{
Console::WriteLine( " Type: '{0}' Value: '{1}'",
cata->ArgumentType, cata->Value );
}
}
static void ShowAttributeData( IList< CustomAttributeData^ >^ attributes )
{
for each ( CustomAttributeData^ cad in attributes )
{
Console::WriteLine( " {0}", cad );
Console::WriteLine( " Constructor: '{0}'", cad->Constructor );
Console::WriteLine( " Constructor arguments:" );
for each ( CustomAttributeTypedArgument^ cata in cad->ConstructorArguments )
{
ShowValueOrArray(cata);
}
Console::WriteLine( " Named arguments:" );
for each ( CustomAttributeNamedArgument cana in cad->NamedArguments )
{
Console::WriteLine( " MemberInfo: '{0}'", cana.MemberInfo );
ShowValueOrArray(cana.TypedValue);
}
}
}
};
int main()
{
Test::Main();
}
/* This code example produces output similar to the following:
Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
Constructor: 'Void .ctor(Int32)'
Constructor arguments:
Type: 'System.Int32' Value: '8'
Named arguments:
[System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'Boolean WrapNonExceptionThrows'
Type: 'System.Boolean' Value: 'True'
[ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
Constructor: 'Void .ctor(ExampleKind)'
Constructor arguments:
Type: 'ExampleKind' Value: '2'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the assembly.'
Attributes for type: 'Test'
[ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
Constructor: 'Void .ctor(ExampleKind, System.String[])'
Constructor arguments:
Type: 'ExampleKind' Value: '1'
Array of 'System.String[]':
Type: 'System.String' Value: 'String array argument, line 1'
Type: 'System.String' Value: 'String array argument, line 2'
Type: 'System.String' Value: 'String array argument, line 3'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the class.'
MemberInfo: 'Int32[] Numbers'
Array of 'System.Int32[]':
Type: 'System.Int32' Value: '53'
Type: 'System.Int32' Value: '57'
Type: 'System.Int32' Value: '59'
Attributes for member: 'Void TestMethod(System.Object)'
[ExampleAttribute(Note = "This is a note on a method.")]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on a method.'
Attributes for parameter: 'System.Object arg'
[ExampleAttribute()]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
*/
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind.ThirdKind, Note="This is a note on the assembly.")]
// An enumeration used by the ExampleAttribute class.
public enum ExampleKind
{
FirstKind,
SecondKind,
ThirdKind,
FourthKind
};
// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets.All)]
public class ExampleAttribute : Attribute
{
// Data for properties.
private ExampleKind kindValue;
private string noteValue;
private string[] arrayStrings;
private int[] arrayNumbers;
// Constructors. The parameterless constructor (.ctor) calls
// the constructor that specifies ExampleKind and an array of
// strings, and supplies the default values.
//
public ExampleAttribute(ExampleKind initKind, string[] initStrings)
{
kindValue = initKind;
arrayStrings = initStrings;
}
public ExampleAttribute(ExampleKind initKind) : this(initKind, null) {}
public ExampleAttribute() : this(ExampleKind.FirstKind, null) {}
// Properties. The Note and Numbers properties must be read/write, so they
// can be used as named parameters.
//
public ExampleKind Kind { get { return kindValue; }}
public string[] Strings { get { return arrayStrings; }}
public string Note
{
get { return noteValue; }
set { noteValue = value; }
}
public int[] Numbers
{
get { return arrayNumbers; }
set { arrayNumbers = value; }
}
}
// The example attribute is applied to the test class.
//
[Example(ExampleKind.SecondKind,
new string[] { "String array argument, line 1",
"String array argument, line 2",
"String array argument, line 3" },
Note="This is a note on the class.",
Numbers = new int[] { 53, 57, 59 })]
public class Test
{
// The example attribute is applied to a method, using the
// parameterless constructor and supplying a named argument.
// The attribute is also applied to the method parameter.
//
[Example(Note="This is a note on a method.")]
public void TestMethod([Example] object arg) { }
// Main() gets objects representing the assembly, the test
// type, the test method, and the method parameter. Custom
// attribute data is displayed for each of these.
//
public static void Main()
{
Assembly asm = Assembly.ReflectionOnlyLoad("Source");
Type t = asm.GetType("Test");
MethodInfo m = t.GetMethod("TestMethod");
ParameterInfo[] p = m.GetParameters();
Console.WriteLine("\r\nAttributes for assembly: '{0}'", asm);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm));
Console.WriteLine("\r\nAttributes for type: '{0}'", t);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(t));
Console.WriteLine("\r\nAttributes for member: '{0}'", m);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(m));
Console.WriteLine("\r\nAttributes for parameter: '{0}'", p);
ShowAttributeData(CustomAttributeData.GetCustomAttributes(p[0]));
}
private static void ShowAttributeData(
IList<CustomAttributeData> attributes)
{
foreach( CustomAttributeData cad in attributes )
{
Console.WriteLine(" {0}", cad);
Console.WriteLine(" Constructor: '{0}'", cad.Constructor);
Console.WriteLine(" Constructor arguments:");
foreach( CustomAttributeTypedArgument cata
in cad.ConstructorArguments )
{
ShowValueOrArray(cata);
}
Console.WriteLine(" Named arguments:");
foreach( CustomAttributeNamedArgument cana
in cad.NamedArguments )
{
Console.WriteLine(" MemberInfo: '{0}'",
cana.MemberInfo);
ShowValueOrArray(cana.TypedValue);
}
}
}
private static void ShowValueOrArray(CustomAttributeTypedArgument cata)
{
if (cata.Value.GetType() == typeof(ReadOnlyCollection<CustomAttributeTypedArgument>))
{
Console.WriteLine(" Array of '{0}':", cata.ArgumentType);
foreach (CustomAttributeTypedArgument cataElement in
(ReadOnlyCollection<CustomAttributeTypedArgument>) cata.Value)
{
Console.WriteLine(" Type: '{0}' Value: '{1}'",
cataElement.ArgumentType, cataElement.Value);
}
}
else
{
Console.WriteLine(" Type: '{0}' Value: '{1}'",
cata.ArgumentType, cata.Value);
}
}
}
/* This code example produces output similar to the following:
Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
Constructor: 'Void .ctor(Int32)'
Constructor arguments:
Type: 'System.Int32' Value: '8'
Named arguments:
[System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'Boolean WrapNonExceptionThrows'
Type: 'System.Boolean' Value: 'True'
[ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
Constructor: 'Void .ctor(ExampleKind)'
Constructor arguments:
Type: 'ExampleKind' Value: '2'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the assembly.'
Attributes for type: 'Test'
[ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
Constructor: 'Void .ctor(ExampleKind, System.String[])'
Constructor arguments:
Type: 'ExampleKind' Value: '1'
Array of 'System.String[]':
Type: 'System.String' Value: 'String array argument, line 1'
Type: 'System.String' Value: 'String array argument, line 2'
Type: 'System.String' Value: 'String array argument, line 3'
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on the class.'
MemberInfo: 'Int32[] Numbers'
Array of 'System.Int32[]':
Type: 'System.Int32' Value: '53'
Type: 'System.Int32' Value: '57'
Type: 'System.Int32' Value: '59'
Attributes for member: 'Void TestMethod(System.Object)'
[ExampleAttribute(Note = "This is a note on a method.")]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
MemberInfo: 'System.String Note'
Type: 'System.String' Value: 'This is a note on a method.'
Attributes for parameter: 'System.Object arg'
[ExampleAttribute()]
Constructor: 'Void .ctor()'
Constructor arguments:
Named arguments:
*/
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
' The example attribute is applied to the assembly.
<Assembly:Example(ExampleKind.ThirdKind, Note:="This is a note on the assembly.")>
' An enumeration used by the ExampleAttribute class.
Public Enum ExampleKind
FirstKind
SecondKind
ThirdKind
FourthKind
End Enum
' An example attribute. The attribute can be applied to all
' targets, from assemblies to parameters.
'
<AttributeUsage(AttributeTargets.All)> _
Public Class ExampleAttribute
Inherits Attribute
' Data for properties.
Private kindValue As ExampleKind
Private noteValue As String
Private arrayStrings() As String
Private arrayNumbers() As Integer
' Constructors. The parameterless constructor (.ctor) calls
' the constructor that specifies ExampleKind and an array of
' strings, and supplies the default values.
'
Public Sub New(ByVal initKind As ExampleKind, ByVal initStrings() As String)
kindValue = initKind
arrayStrings = initStrings
End Sub
Public Sub New(ByVal initKind As ExampleKind)
Me.New(initKind, Nothing)
End Sub
Public Sub New()
Me.New(ExampleKind.FirstKind, Nothing)
End Sub
' Properties. The Note and Numbers properties must be read/write, so they
' can be used as named parameters.
'
Public ReadOnly Property Kind As ExampleKind
Get
Return kindValue
End Get
End Property
Public ReadOnly Property Strings As String()
Get
Return arrayStrings
End Get
End Property
Public Property Note As String
Get
Return noteValue
End Get
Set
noteValue = value
End Set
End Property
Public Property Numbers As Integer()
Get
Return arrayNumbers
End Get
Set
arrayNumbers = value
End Set
End Property
End Class
' The example attribute is applied to the test class.
'
<Example(ExampleKind.SecondKind, _
New String() { "String array argument, line 1", _
"String array argument, line 2", _
"String array argument, line 3" }, _
Note := "This is a note on the class.", _
Numbers := New Integer() { 53, 57, 59 })> _
Public Class Test
' The example attribute is applied to a method, using the
' parameterless constructor and supplying a named argument.
' The attribute is also applied to the method parameter.
'
<Example(Note:="This is a note on a method.")> _
Public Sub TestMethod(<Example()> ByVal arg As Object)
End Sub
' Sub Main gets objects representing the assembly, the test
' type, the test method, and the method parameter. Custom
' attribute data is displayed for each of these.
'
Public Shared Sub Main()
Dim asm As [Assembly] = Assembly.ReflectionOnlyLoad("source")
Dim t As Type = asm.GetType("Test")
Dim m As MethodInfo = t.GetMethod("TestMethod")
Dim p() As ParameterInfo = m.GetParameters()
Console.WriteLine(vbCrLf & "Attributes for assembly: '{0}'", asm)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm))
Console.WriteLine(vbCrLf & "Attributes for type: '{0}'", t)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(t))
Console.WriteLine(vbCrLf & "Attributes for member: '{0}'", m)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(m))
Console.WriteLine(vbCrLf & "Attributes for parameter: '{0}'", p)
ShowAttributeData(CustomAttributeData.GetCustomAttributes(p(0)))
End Sub
Private Shared Sub ShowAttributeData( _
ByVal attributes As IList(Of CustomAttributeData))
For Each cad As CustomAttributeData _
In CType(attributes, IEnumerable(Of CustomAttributeData))
Console.WriteLine(" {0}", cad)
Console.WriteLine(" Constructor: '{0}'", cad.Constructor)
Console.WriteLine(" Constructor arguments:")
For Each cata As CustomAttributeTypedArgument _
In CType(cad.ConstructorArguments, IEnumerable(Of CustomAttributeTypedArgument))
ShowValueOrArray(cata)
Next
Console.WriteLine(" Named arguments:")
For Each cana As CustomAttributeNamedArgument _
In CType(cad.NamedArguments, IEnumerable(Of CustomAttributeNamedArgument))
Console.WriteLine(" MemberInfo: '{0}'", _
cana.MemberInfo)
ShowValueOrArray(cana.TypedValue)
Next
Next
End Sub
Private Shared Sub ShowValueOrArray(ByVal cata As CustomAttributeTypedArgument)
If cata.Value.GetType() Is GetType(ReadOnlyCollection(Of CustomAttributeTypedArgument)) Then
Console.WriteLine(" Array of '{0}':", cata.ArgumentType)
For Each cataElement As CustomAttributeTypedArgument In cata.Value
Console.WriteLine(" Type: '{0}' Value: '{1}'", _
cataElement.ArgumentType, cataElement.Value)
Next
Else
Console.WriteLine(" Type: '{0}' Value: '{1}'", _
cata.ArgumentType, cata.Value)
End If
End Sub
End Class
' This code example produces output similar to the following:
'
'Attributes for assembly: 'source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
' [System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
' Constructor: 'Void .ctor(Int32)'
' Constructor arguments:
' Type: 'System.Int32' Value: '8'
' Named arguments:
' [System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNonExceptionThrows = True)]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
' MemberInfo: 'Boolean WrapNonExceptionThrows'
' Type: 'System.Boolean' Value: 'True'
' [ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
' Constructor: 'Void .ctor(ExampleKind)'
' Constructor arguments:
' Type: 'ExampleKind' Value: '2'
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on the assembly.'
'
'Attributes for type: 'Test'
' [ExampleAttribute((ExampleKind)1, new String[3] { "String array argument, line 1", "String array argument, line 2", "String array argument, line 3" }, Note = "This is a note on the class.", Numbers = new Int32[3] { 53, 57, 59 })]
' Constructor: 'Void .ctor(ExampleKind, System.String[])'
' Constructor arguments:
' Type: 'ExampleKind' Value: '1'
' Array of 'System.String[]':
' Type: 'System.String' Value: 'String array argument, line 1'
' Type: 'System.String' Value: 'String array argument, line 2'
' Type: 'System.String' Value: 'String array argument, line 3'
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on the class.'
' MemberInfo: 'Int32[] Numbers'
' Array of 'System.Int32[]':
' Type: 'System.Int32' Value: '53'
' Type: 'System.Int32' Value: '57'
' Type: 'System.Int32' Value: '59'
'
'Attributes for member: 'Void TestMethod(System.Object)'
' [ExampleAttribute(Note = "This is a note on a method.")]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
' MemberInfo: 'System.String Note'
' Type: 'System.String' Value: 'This is a note on a method.'
'
'Attributes for parameter: 'System.Object arg'
' [ExampleAttribute()]
' Constructor: 'Void .ctor()'
' Constructor arguments:
' Named arguments:
注解
此方法提供了方法的替代方法, GetCustomAttributes 该方法始终可以在仅反射上下文中使用。