日落20190901002 - C#基础之Attribute的检测应用

环境

系统:Windows 10
引擎:VS2017

目的

通过制作自定义的Attribute类,进行检测目标元素是否绑定某特性的实例。

实例

using System;
using System.Reflection;

namespace AttributeTest
{
    #region 特性类
    [AttributeUsage(AttributeTargets.All, Inherited = true)] // 可应用于各种目标元素,并可应用于该目标元素的派生元素。
    internal class AattrAttribute : Attribute { }

    [AttributeUsage(AttributeTargets.Class, Inherited = false)] // 可应用的目标元素类型是“类”,但不可应用于该目标元素的派生元素。
    internal class BattrAttribute : Attribute { }

    [AttributeUsage(AttributeTargets.Method, Inherited = true)] // 可应用的目标元素类型是“类方法”,并可应用于该目标元素的派生元素。
    internal class CattrAttribute : Attribute { }
    #endregion

    #region 测试类
    [Aattr][Battr]
    internal class BaseType
    {
        [Cattr]
        protected virtual void DoWhat() { }
    }

    internal class DerideType : BaseType
    {
        protected override void DoWhat() { }
    }
    #endregion

    class Program
    {
        static void Main(string[] args)
        {
            MemberInfo attrInfo = typeof(DerideType);
            Type attrType = typeof(BattrAttribute);

            #region IsDefined检测法
            Console.WriteLine("IsDefined检测法:");
            Boolean isDefined = Attribute.IsDefined(attrInfo, attrType);
            if (isDefined == true)
                Console.WriteLine("{0} is used to {1}", attrType, attrInfo);
            else
                Console.WriteLine("{0} is not used to {1}", attrType, attrInfo);
            Console.WriteLine();
            #endregion

            #region GetCustomAttribute检测法
            Console.WriteLine("GetCustomAttribute检测法:");
            Attribute singleAttr = Attribute.GetCustomAttribute(attrInfo, attrType);
            if (singleAttr != null)
                Console.WriteLine("{0} is used to {1}", attrType, attrInfo);
            else
                Console.WriteLine("{0} is not used to {1}", attrType, attrInfo);
            Console.WriteLine();
            #endregion

            #region GetCustomAttributes检测法
            Console.WriteLine("GetCustomAttributes检测法:");
            Attribute[] attrs = Attribute.GetCustomAttributes(attrInfo);
            foreach (Attribute attr in attrs)
            {
                Console.WriteLine("{0} is used to {1}", attr.GetType().ToString(), attrInfo);
            }
            Console.WriteLine();
            #endregion
        }
    }
}

结果

IsDefined检测法:
AttributeTest.BattrAttribute is not used to AttributeTest.DerideType

GetCustomAttribute检测法:
AttributeTest.BattrAttribute is not used to AttributeTest.DerideType

GetCustomAttributes检测法:
AttributeTest.AattrAttribute is used to AttributeTest.DerideType

总结

由上述实例可以联想到的用法是:通过检测目标元素A是否绑定特性B,而在另一程序段C去选择是否调用目标元素A。类似于标记的作用。

以上简单回顾。

参考资料:

《CLR via C#(第3版)》第18.2节和第18.4节

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值