CustomAttribute 结构 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供有关自定义属性的信息。
public value class CustomAttribute
	public struct CustomAttribute
	public readonly struct CustomAttribute
	type CustomAttribute = struct
	Public Structure CustomAttribute
		- 继承
 
示例
此示例演示如何打印应用于类型定义的所有自定义属性:
class MyAttribute : Attribute
{
    public int X { get; set; }
}
[My(X = 1)]
class ExampleType1 { }
[My(X = 2)]
class ExampleType2 { }
static void PrintCustomAttributes(MetadataReader mr, TypeDefinition t)
{
    // Enumerate custom attributes on the type definition
    foreach (CustomAttributeHandle attrHandle in t.GetCustomAttributes())
    {
        CustomAttribute attr = mr.GetCustomAttribute(attrHandle);
        // Display the attribute type full name
        if (attr.Constructor.Kind == HandleKind.MethodDefinition)
        {
            MethodDefinition mdef = mr.GetMethodDefinition((MethodDefinitionHandle)attr.Constructor);
            TypeDefinition tdef = mr.GetTypeDefinition(mdef.GetDeclaringType());
            Console.WriteLine($"Type:  {mr.GetString(tdef.Namespace)}.{mr.GetString(tdef.Name)}");
        }
        else if (attr.Constructor.Kind == HandleKind.MemberReference)
        {
            MemberReference mref = mr.GetMemberReference((MemberReferenceHandle)attr.Constructor);
            if (mref.Parent.Kind == HandleKind.TypeReference)
            {
                TypeReference tref = mr.GetTypeReference((TypeReferenceHandle)mref.Parent);
                Console.WriteLine($"Type:  {mr.GetString(tref.Namespace)}.{mr.GetString(tref.Name)}");
            }
            else if (mref.Parent.Kind == HandleKind.TypeDefinition)
            {
                TypeDefinition tdef = mr.GetTypeDefinition((TypeDefinitionHandle)mref.Parent);
                Console.WriteLine($"Type:  {mr.GetString(tdef.Namespace)}.{mr.GetString(tdef.Name)}");
            }
        }
        // Display the attribute value
        byte[] data = mr.GetBlobBytes(attr.Value);
        Console.Write("Value: ");
        for (int i = 0; i < data.Length; i++) Console.Write($"{data[i]:X2} ");
        Console.WriteLine();
    }
}
static void PrintTypesCustomAttributes(MetadataReader mr)
{
    foreach (TypeDefinitionHandle tdh in mr.TypeDefinitions)
    {
        TypeDefinition t = mr.GetTypeDefinition(tdh);
        Console.WriteLine($"{mr.GetString(t.Namespace)}.{mr.GetString(t.Name)}");
        PrintCustomAttributes(mr, t);
    }
}
	注解
自定义属性是一个批注,它将其他信息与元数据元素(例如程序集、类型或方法)相关联。 可以使用 GetCustomAttribute(CustomAttributeHandle) 方法获取自定义属性实例。 有关 .NET 中属性的详细信息,请参阅 使用特性扩展元数据。
属性
| Constructor | 
		 获取自定义属性类型的构造函数(MethodDefinitionHandle 或 MemberReferenceHandle)。  | 
        	
| Parent | 
		 获取应用了特性的元数据实体的句柄。  | 
        	
| Value | 
		 获取属性的值。  | 
        	
方法
| DecodeValue<TType>(ICustomAttributeTypeProvider<TType>) | 
		 对以值 blob 形式编码的参数进行解码。  |