Attribute特性使用

Attribute特性

特性是用于在运行时传递程序中各种元素行为信息的声明性标签加粗样式 ,一个声明标签是通过放置在它所应用的元素前的方括号[]来描述。

在.Net框架里提供了两种类型的特性:

  1. 预定义特性
  2. 自定义特性

预定义特性

  • AttributeUsage: 用于描述如何使用一个自定义特性;
[AttributeUsage(AttributeTargets.Class,AllowMultiple =true,Inherited =true)]

AttributeTargets——选择被指定使用的某种元素(默认all全部)
AllowMultiple——bool类型的特性属性,true为多用,false为单用
Inherited——bool类型的特性属性,true为可被派生类继承,false为不可

  • Conditional:标记一个指定的预处理标识符
[Conditional("DEBUG")]

常用的conditionSymbol有“DEBUG”和“Trace”

  • Obsolete:标记不应被使用的程序实体
[Obsolete("Dont't use OldMethod,use NewMethod instead",true)]

自定义特性

.Net框架允许创建自定义特性,用于存储声明性的信息,且可以在运行时被检索。

  • 声明自定义特性
  • 构建自定义特性
  • 应用自定义特性
  • 利用反射访问特性

例子:
构建自定义特性

    //创建一个存储一个属性和一个方法的特性
    [AttributeUsage(AttributeTargets.Class,AllowMultiple =true,Inherited =true)]
    //特性必须继承Attribute类
    public class DescriptionAttribute:Attribute
    {
        public DescriptionAttribute()
        {
            Console.WriteLine($"{nameof(DescriptionAttribute)}被构造");

        }
        public string Description { get; set; }
        public void show()
        {
            Console.WriteLine(nameof(DescriptionAttribute));
            Console.WriteLine(this.Description);
        }
    }

应用自定义特性

   [Description(Description ="这是普通学生")]
    public class NormalStudent:Student
    {
        public override void Study()
        {
            Console.WriteLine("普通学生上课");
        }
    }

利用反射访问特性

        //用于获取student类中的特性,得到DA后执行show方法
        public static void ManageStudent<S>(S student)where S : Student
        {
            Console.WriteLine($"{student.Id}_{student.Name }");
            student.Study();

            //反射
            Type type = student.GetType();
            if (type.IsDefined(typeof(DescriptionAttribute), true))
            {
                DescriptionAttribute DA= (DescriptionAttribute)type.GetCustomAttribute(typeof(DescriptionAttribute), true);
                DA.show();
            }
        }

**记录一个通过反射+泛型进行类型转换的写法

        public static void Main(string[] args)
        {
            NormalStudent normal = new NormalStudent()
            {
                Id = 123,
                Name = "lok",
                Age = 19
            };
            Worker worker = Trans<NormalStudent, Worker>(normal);
        }
        private static TOut Trans<TIn, TOut>(TIn tIn)
        {
            Type typeIn = typeof(TIn);
            Type typeOut = typeof(TOut);

            TOut tOut = (TOut)Activator.CreateInstance(typeOut);
            foreach (var item in typeOut.GetProperties())
            {
                PropertyInfo info = typeIn.GetProperty(item.Name);
                object typeOutValue = info.GetValue(tIn);
                item.SetValue(tOut, typeOutValue);
            }
            return tOut;
        }

NormalStudent 和 Worker 是两个具有相同属性的类

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值