MemberInfo.GetCustomAttributes 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中重写时,返回应用于此成员的自定义属性。
重载
| 名称 | 说明 |
|---|---|
| GetCustomAttributes(Boolean) |
在派生类中重写时,返回应用于此成员的所有自定义属性的数组。 |
| GetCustomAttributes(Type, Boolean) |
在派生类中重写时,返回应用于此成员并由该成员标识 Type的自定义属性数组。 |
GetCustomAttributes(Boolean)
- Source:
- MemberInfo.cs
- Source:
- MemberInfo.cs
- Source:
- MemberInfo.cs
- Source:
- MemberInfo.cs
- Source:
- MemberInfo.cs
在派生类中重写时,返回应用于此成员的所有自定义属性的数组。
public:
abstract cli::array <System::Object ^> ^ GetCustomAttributes(bool inherit);
public abstract object[] GetCustomAttributes(bool inherit);
abstract member GetCustomAttributes : bool -> obj[]
Public MustOverride Function GetCustomAttributes (inherit As Boolean) As Object()
参数
- inherit
- Boolean
true 若要搜索此成员的继承链以查找属性,则为 ;否则,为 false. 对于属性和事件,将忽略此参数。
返回
一个数组,其中包含应用于此成员的所有自定义属性;如果没有定义任何属性,则包含零个元素的数组。
实现
例外
此成员属于加载到仅反射上下文中的类型。 请参阅 如何:将程序集加载到 Reflection-Only 上下文中。
无法加载自定义属性类型。
示例
下面的示例定义一个自定义属性,并将该属性与 MyClass.MyMethod该属性相关联,在运行时检索该属性,并显示结果。
using System;
using System.Reflection;
// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{
private string myName;
public MyAttribute(string name)
{
myName = name;
}
public string Name
{
get
{
return myName;
}
}
}
// Define a class that has the custom attribute associated with one of its members.
public class MyClass1
{
[MyAttribute("This is an example attribute.")]
public void MyMethod(int i)
{
return;
}
}
public class MemberInfo_GetCustomAttributes
{
public static void Main()
{
try
{
// Get the type of MyClass1.
Type myType = typeof(MyClass1);
// Get the members associated with MyClass1.
MemberInfo[] myMembers = myType.GetMembers();
// Display the attributes for each of the members of MyClass1.
for(int i = 0; i < myMembers.Length; i++)
{
Object[] myAttributes = myMembers[i].GetCustomAttributes(true);
if(myAttributes.Length > 0)
{
Console.WriteLine("\nThe attributes for the member {0} are: \n", myMembers[i]);
for(int j = 0; j < myAttributes.Length; j++)
Console.WriteLine("The type of the attribute is {0}.", myAttributes[j]);
}
}
}
catch(Exception e)
{
Console.WriteLine("An exception occurred: {0}", e.Message);
}
}
}
Imports System.Reflection
' Define a custom attribute with one named parameter.
<AttributeUsage(AttributeTargets.All)> Public Class MyAttribute
Inherits Attribute
Private myName As String
Public Sub New(ByVal name As String)
myName = name
End Sub
Public ReadOnly Property Name() As String
Get
Return myName
End Get
End Property
End Class
' Define a class that has the custom attribute associated with one of its members.
Public Class MyClass1
<MyAttribute("This is an example attribute.")> Public Sub MyMethod(ByVal i As Integer)
Return
End Sub
End Class
Public Class MemberInfo_GetCustomAttributes
Public Shared Sub Main()
Try
' Get the type of MyClass1.
Dim myType As Type = GetType(MyClass1)
' Get the members associated with MyClass1.
Dim myMembers As MemberInfo() = myType.GetMembers()
' Display the attributes for each of the members of MyClass1.
Dim i As Integer
For i = 0 To myMembers.Length - 1
Dim myAttributes As [Object]() = myMembers(i).GetCustomAttributes(False)
If myAttributes.Length > 0 Then
Console.WriteLine("The attributes for the member {0} are: ", myMembers(i))
Dim j As Integer
For j = 0 To myAttributes.Length - 1
Console.WriteLine("The type of the attribute is: {0}", myAttributes(j))
Next j
End If
Next i
Catch e As Exception
Console.WriteLine("An exception occurred: {0}.", e.Message)
End Try
End Sub
End Class
注解
此方法忽略 inherit 属性和事件的参数。 若要在继承链中搜索属性和事件的属性,请使用方法的 Attribute.GetCustomAttributes 相应重载。
另请参阅
适用于
GetCustomAttributes(Type, Boolean)
- Source:
- MemberInfo.cs
- Source:
- MemberInfo.cs
- Source:
- MemberInfo.cs
- Source:
- MemberInfo.cs
- Source:
- MemberInfo.cs
在派生类中重写时,返回应用于此成员并由该成员标识 Type的自定义属性数组。
public:
abstract cli::array <System::Object ^> ^ GetCustomAttributes(Type ^ attributeType, bool inherit);
public abstract object[] GetCustomAttributes(Type attributeType, bool inherit);
abstract member GetCustomAttributes : Type * bool -> obj[]
Public MustOverride Function GetCustomAttributes (attributeType As Type, inherit As Boolean) As Object()
参数
- attributeType
- Type
要搜索的属性的类型。 仅返回可分配给此类型的属性。
- inherit
- Boolean
true 若要搜索此成员的继承链以查找属性,则为 ;否则,为 false. 对于属性和事件,将忽略此参数。
返回
应用于此成员的自定义特性的数组;如果没有可 attributeType 分配的属性,则为零元素的数组。
实现
例外
无法加载自定义属性类型。
null如果是attributeType。
此成员属于加载到仅反射上下文中的类型。 请参阅 如何:将程序集加载到 Reflection-Only 上下文中。
示例
以下示例定义一 BaseClass 个名为具有两个非继承成员的类:一 total 个名为线程静态字段,一个名为 MethodA不符合 CLS 的方法。 命名的 DerivedClass 类继承 BaseClass 并重写其 MethodA 方法。 请注意,不会对成员 DerivedClass应用任何属性。 该示例循环访问要确定CLSCompliantAttribute应用于它们的成员DerivedClass或ThreadStaticAttribute属性。
true因此inherit,该方法将搜索指定属性的DerivedClass继承层次结构。 如示例中的输出所示, total 该字段使用 ThreadStaticAttribute 属性进行修饰,并使用 MethodA 特性修饰 CLSCompliantAttribute 该方法。
using System;
public class BaseClass
{
[ThreadStatic] public int total;
[CLSCompliant(false)] public virtual uint MethodA()
{
return (uint) 100;
}
}
public class DerivedClass : BaseClass
{
public override uint MethodA()
{
total++;
return 200;
}
}
public class Example
{
public static void Main()
{
Type t = typeof(DerivedClass);
Console.WriteLine("Members of {0}:", t.FullName);
foreach (var m in t.GetMembers())
{
bool hasAttribute = false;
Console.Write(" {0}: ", m.Name);
if (m.GetCustomAttributes(typeof(CLSCompliantAttribute), true).Length > 0) {
Console.Write("CLSCompliant");
hasAttribute = true;
}
if (m.GetCustomAttributes(typeof(ThreadStaticAttribute), true).Length > 0) {
Console.Write("ThreadStatic");
hasAttribute = true;
}
if (!hasAttribute)
Console.Write("No attributes");
Console.WriteLine();
}
}
}
// The example displays the following output:
// Members of DerivedClass:
// MethodA: CLSCompliant
// ToString: No attributes
// Equals: No attributes
// GetHashCode: No attributes
// typeof: No attributes
// .ctor: No attributes
// total: ThreadStatic
Public Class BaseClass
<ThreadStatic> Public total As Integer
<CLSCompliant(False)> Public Overridable Function MethodA() As UInt32
Return CUInt(100)
End Function
End Class
Public Class DerivedClass : Inherits BaseClass
Public Overrides Function MethodA() As UInt32
total += 1
Return 200
End Function
End Class
Module Example
Public Sub Main()
Dim t As Type = GetType(DerivedClass)
Console.WriteLine("Members of {0}:", t.FullName)
For Each m In t.GetMembers()
Dim hasAttribute As Boolean = False
Console.Write(" {0}: ", m.Name)
If m.GetCustomAttributes(GetType(CLSCompliantAttribute), True).Length > 0 Then
Console.Write("CLSCompliant")
hasAttribute = True
End If
If m.GetCustomAttributes(GetType(ThreadStaticAttribute), True).Length > 0 Then
Console.Write("ThreadStatic")
hasAttribute = True
End If
If Not hasAttribute Then
Console.Write("No attributes")
End If
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Members of DerivedClass:
' MethodA: CLSCompliant
' ToString: No attributes
' Equals: No attributes
' GetHashCode: No attributes
' GetType: No attributes
' .ctor: No attributes
' total: ThreadStatic
注解
此方法忽略 inherit 属性和事件的参数。 若要在继承链中搜索属性和事件的属性,请使用方法的 Attribute.GetCustomAttributes 相应重载。