• C# - 反射动态添加/删除Attribute特性


    API: 

    TypeDescriptor.AddAttributes

    TypeDescriptor.GetAttributes

    注意:TypeDescriptor.AddAttributes添加的特性需要使用 TypeDescriptor.GetAttributes获取

    根据api可以看到,该接口不仅可以给指定类(Type)添加特性,还能给其他任意object类型对象添加特性

    添加:
    1. public class DynamicCacheBufferAtrribute : Attribute
    2. {
    3. public int Value;
    4. public DynamicCacheBufferAtrribute(int v)
    5. {
    6. Value = v;
    7. }
    8. }
    9. public class MyClass1
    10. {
    11. }
    12. public class MyClass2
    13. {
    14. public MyClass1 Class1Obj;
    15. }
    16. //需要添加的特性集
    17. var attributes = new Attribute[]
    18. {
    19. new DynamicCacheBufferAtrribute(123)
    20. };
    21. //给 MyClass1 类型添加、获取特性
    22. TypeDescriptor.AddAttributes(typeof(MyClass1), attributes);
    23. var attr = TypeDescriptor.GetAttributes(typeof(MyClass1))[typeof(DynamicCacheBufferAtrribute)] as DynamicCacheBufferAtrribute;
    24. //var attr = TypeDescriptor.GetAttributes(field).OfType<DynamicCacheBufferAtrribute>().FirstOrDefault();
    25. //给类对象添加、获取特性
    26. var class1Inst = new MyClass1();
    27. TypeDescriptor.AddAttributes(class1Inst , attributes);
    28. var attr = TypeDescriptor.GetAttributes(class1Inst)[typeof(DynamicCacheBufferAtrribute)] as DynamicCacheBufferAtrribute;
    29. //给FieldInfo添加、获取特性
    30. var class2Inst = new MyClass2();
    31. class2Inst.Class1Obj = new MyClass1();
    32. var fieldInfo = class2Inst.GetType().GetField("Class1Obj");
    33. TypeDescriptor.AddAttributes(fieldInfo, attributes);
    34. var attr = TypeDescriptor.GetAttributes(fieldInfo)[typeof(DynamicCacheBufferAtrribute)] as DynamicCacheBufferAtrribute;
    35. //给PropertyInfo添加、获取特性
    36. var class2Inst = new MyClass2();
    37. class2Inst.Class1Obj = new MyClass1();
    38. var propertyInfo = class2Inst.GetType().GetProperty("Class1Obj");
    39. TypeDescriptor.AddAttributes(propertyInfo, attributes);
    40. var attr = TypeDescriptor.GetAttributes(propertyInfo)[typeof(DynamicCacheBufferAtrribute)] as DynamicCacheBufferAtrribute;
    41. //其他object类型均可
    删除:
    1. //清空MyClaas1类型的所有DynamicCacheBufferAtrribute特性
    2. var attrs = TypeDescriptor.GetAttributes(typeof(MyClass1)).OfType<DynamicCacheBufferAtrribute>().ToArray();
    3. ArrayUtility.Clear(ref attrs);
    4. TypeDescriptor.AddAttributes(typeof(MyClass1), attrs);
    附加API解释: 

    TypeDescriptor.GetProperties(object)获取对象的特性

    如果对象是一个属性(Property)

    那么需要使用一下方式获取其特性

    TypeDescriptor.GetProperties(belongClassType)[propertyName].Attributes​​​​​​​

    1. // 假设我们有一个类和一个特性
    2. public class MyClass
    3. {
    4. [MyAttribute]
    5. public int MyProperty { get; set; }
    6. }
    7. public class MyAttribute : Attribute { }
    8. // 获取类的特性
    9. MyClass myClass = new MyClass();
    10. AttributeCollection attributes = TypeDescriptor.GetAttributes(myClass);
    11. // 获取属性的特性
    12. PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(typeof(MyClass))["MyProperty"];
    13. AttributeCollection propertyAttributes = propertyDescriptor.Attributes;

     

  • 相关阅读:
    1149 Dangerous Goods Packaging
    vue3中axios的使用方法
    计算机专业毕业设计项目,如何去做?一位大龄过期“初级”程序员来讲讲
    蓝牙核心规范(V5.4)10.2-BLE 入门笔记之CIS篇
    ssm+共享图书管理系统 毕业设计-附源码151121
    VMware虚拟机中的Linux通过NAT模式共享主机网卡实现与外部设备通信
    C# Onnx 特征匹配 DeDoDe 检测,不描述---描述,不检测
    JAVA-中国矿业大学作业-编写程序,将字符串“你好,欢迎来到JAVA世界”对其中的“java“进行截取,输出截取字母和它在字符串中的位置
    刷题记录----字符串
    Code-Audit(代码审计)习题记录4-5
  • 原文地址:https://blog.csdn.net/smile_otl/article/details/137913064