C# AttributeUsage

C# AttributeUsage

预定义特性 AttributeUsage 描述了如何使用一个自定义特性类。它规定了特性可应用到的项目的类型。

规定该特性的语法如下:

[AttributeUsage(
validon,
AllowMultiple=allowmultiple,
Inherited=inherited
)]

validon:自定义特性的对象,可以是类、方法、属性等对象(默认值是 AttributeTargets.All)AttributeTargets 所有类型如下 可以使用 或运算符 |
AttributeTargets.All = AttributeTargets.Assembly | AttributeTargets.Module 等所有组合

    [Flags]
    public enum AttributeTargets
    {
        Assembly = 1,
        Module = 2,
        Class = 4,
        Struct = 8,
        Enum = 16,
        Constructor = 32,
        Method = 64,
        Property = 128,
        Field = 256,
        Event = 512,
        Interface = 1024,
        Parameter = 2048,
        Delegate = 4096,
        ReturnValue = 8192,
        GenericParameter = 16384,
        All = 32767
    }

AllowMultiple:是否允许被多次使用(默认值为false:单用的)
Inherited:是否可被派生类继承(默认值为false:不能)

定义如下

using System;

// 定义一个 NpcAttribute,
// AttributeTargets.Class标记为类Class 使用
// AllowMultiple = true 可以多次使用
// Inherited = true 可以被继承/派生子类
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class NpcAttribute : Attribute
{
    public NpcAttribute()
    {
    }
}

使用如下

// 标记 NpcClass 类使用 NpcAttribute 属性
[Npc]
public class NpcClass
{
}
NpcClassExtend  继承 NpcClass
public class NpcClassExtend : NpcClass
{
}

标记了属性,如何使用、获取如下

private void NpcTest()
    {
        // 实例一个对象
        NpcClass npcClass = new NpcClass();

        // 获取对象类型
        Type t = npcClass.GetType();
        // 打印类型名
        Debug.LogError("t.Name:" + t.Name);

        // 获取 typeof(NpcAttribute) 属性, true 包含继承的
        object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);
        if (null != objAttrs && objAttrs.Length > 0)
        {
            for (int i = 0; i < objAttrs.Length; ++i)
            {
                object temp = objAttrs[i];
                // 类型转换
                NpcAttribute npcAttribute = temp as NpcAttribute;
                // 获取类型
                Debug.LogError("NpcClass 获取到 NpcAttribute:" + npcAttribute.ToString());
            }
        }
        else
        {
            Debug.LogError("NpcClass 未找到 NpcAttribute");
        }

        // 上面执行打印结果
        // t.Name:NpcClass
        // NpcClass 获取到 NpcAttribute:NpcAttribute
    }

    private void NpcExtendTest()
    {
        // 实例一个对象
        NpcClassExtend npcClassExtend = new NpcClassExtend();

        // 获取对象类型
        Type t = npcClassExtend.GetType();

        // 打印类型名
        Debug.LogError("t.Name:" + t.Name);

        // 获取 typeof(NpcAttribute) 属性, true 包含继承的
        object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), false);
        if (null != objAttrs && objAttrs.Length > 0)
        {
            for (int i = 0; i < objAttrs.Length; ++i)
            {
                object temp = objAttrs[i];
                // 类型转换
                NpcAttribute npcAttribute = temp as NpcAttribute;
                // 获取类型
                Debug.LogError("NpcClassExtend 获取到 NpcAttribute:" + npcAttribute.ToString());
            }
        }
        else
        {
            Debug.LogError("NpcClassExtend 未找到 NpcAttribute");
        }

        // 上面执行打印结果
        // t.Name:NpcClassExtend
        // NpcClassExtend 获取到 NpcAttribute:NpcAttribute

        // 如果上面代码修改,第二个参数 inherit 为 false
        // object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), false);
        // 执行打印结果
        // t.Name:NpcClassExtend
        // NpcClassExtend 未找到 NpcAttribute
        // 因为 NpcClass 标记了[Npc],NpcClassExtend 继承了 NpcClass
        // 获取时如果函数 GetCustomAttributes(Type attributeType, bool inherit)中 inherit 赋值为 false
        // 则 NpcClassExtend 无法获取到 NpcAttribute 属性
    }

修改

// Inherited = false 不允许派生、继承
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class NpcAttribute : Attribute
{
    public NpcAttribute()
    {
    }
}

下面获取的 objAttrs 中是获取不到 NpcAttribute 的

 // 实例一个对象
        NpcClassExtend npcClassExtend = new NpcClassExtend();

        // 获取对象类型
        Type t = npcClassExtend.GetType();

        // 打印类型名
        Debug.LogError("t.Name:" + t.Name);

        // 获取 typeof(NpcAttribute) 属性, true 包含继承的
        object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);

AllowMultiple = true 作用,允许多次使用,代码如下

[Npc]
[Npc]
[Npc]
public class NpcClass
{

}
    private void NpcTest()
    {
        // 实例一个对象
        NpcClass npcClass = new NpcClass();

        // 获取对象类型
        Type t = npcClass.GetType();
        // 打印类型名
        Debug.LogError("t.Name:" + t.Name);

        // 获取 typeof(NpcAttribute) 属性, true 包含继承的
        object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);
        if (null != objAttrs && objAttrs.Length > 0)
        {
            for (int i = 0; i < objAttrs.Length; ++i)
            {
                object temp = objAttrs[i];
                // 类型转换
                NpcAttribute npcAttribute = temp as NpcAttribute;
                // 获取类型
                Debug.LogError("NpcClass 获取到 NpcAttribute:" + npcAttribute.ToString());
            }
        }
        else
        {
            Debug.LogError("NpcClass 未找到 NpcAttribute");
        }

        // 上面执行打印结果
        // t.Name:NpcClass
        // NpcClass 获取到 NpcAttribute:NpcAttribute
        // NpcClass 获取到 NpcAttribute:NpcAttribute
        // NpcClass 获取到 NpcAttribute:NpcAttribute
    }

上面代码打印了三行 NpcClass 获取到 NpcAttribute:NpcAttribute,是因为NpcClass使用了三次标记[Npc]

修改代码,将 AllowMultiple 赋值为 false

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class NpcAttribute : Attribute
{
    public NpcAttribute()
    {
    }
}

则 NpcClass 报错,多次使用了 Npc 属性
Assets\Script\NpcClass.cs(6,2): error CS0579: Duplicate ‘Npc’ attribute

定义可传参数的属性,修改代码如下

using System;

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class NpcAttribute : Attribute
{
    private string _descript;
    public NpcAttribute(string descript)
    {
        _descript = descript;
    }

    public string Descript
    {
        get { return _descript; }
    }
}

[Npc("Npc 添加一次 ")]
[Npc("Npc 添加二次 ")]
[Npc("Npc 添加三次 ")]
public class NpcClass
{
}

public class NpcClassExtend : NpcClass
{

}

    private void NpcTest()
    {
        // 实例一个对象
        NpcClass npcClass = new NpcClass();

        // 获取对象类型
        Type t = npcClass.GetType();
        // 打印类型名
        Debug.LogError("t.Name:" + t.Name);

        // 获取 typeof(NpcAttribute) 属性, true 包含继承的
        object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);
        if (null != objAttrs && objAttrs.Length > 0)
        {
            for (int i = 0; i < objAttrs.Length; ++i)
            {
                object temp = objAttrs[i];
                // 类型转换
                NpcAttribute npcAttribute = temp as NpcAttribute;
                // 获取类型
                Debug.LogError("NpcClass 获取到 NpcAttribute:" + npcAttribute.ToString() + "   " + npcAttribute.Descript);
            }
        }
        else
        {
            Debug.LogError("NpcClass 未找到 NpcAttribute");
        }

        // 上面执行打印结果
        // t.Name:NpcClass
        // NpcClass 获取到 NpcAttribute:NpcAttribute   Npc 添加一次 
        // NpcClass 获取到 NpcAttribute:NpcAttribute   Npc 添加二次 
        // NpcClass 获取到 NpcAttribute:NpcAttribute   Npc 添加三次 
    }

    private void NpcExtendTest()
    {
        // 实例一个对象
        NpcClassExtend npcClassExtend = new NpcClassExtend();

        // 获取对象类型
        Type t = npcClassExtend.GetType();

        // 打印类型名
        Debug.LogError("t.Name:" + t.Name);

        // 获取 typeof(NpcAttribute) 属性, true 包含继承的
        object[] objAttrs = t.GetCustomAttributes(typeof(NpcAttribute), true);
        if (null != objAttrs && objAttrs.Length > 0)
        {
            for (int i = 0; i < objAttrs.Length; ++i)
            {
                object temp = objAttrs[i];
                // 类型转换
                NpcAttribute npcAttribute = temp as NpcAttribute;
                // 获取类型
                Debug.LogError("NpcClassExtend 获取到 NpcAttribute:" + npcAttribute.ToString() + "   " + npcAttribute.Descript);
            }
        }
        else
        {
            Debug.LogError("NpcClassExtend 未找到 NpcAttribute");
        }

        // 上面执行打印结果
        // t.Name:NpcClassExtend
        // NpcClassExtend 获取到 NpcAttribute:NpcAttribute   Npc 添加一次 
        // NpcClassExtend 获取到 NpcAttribute:NpcAttribute   Npc 添加二次 
        // NpcClassExtend 获取到 NpcAttribute:NpcAttribute   Npc 添加三次 
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值