• 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;

     

  • 相关阅读:
    Openssl数据安全传输平台003:Protobuf - 部署
    算法---消除游戏(Kotlin)
    电气专业发展到头了?
    欧盟《数据治理法》说明
    【成为红帽工程师】第三天 web服务器
    Java Slf4j日志框架
    NPDP如何申请退考?你学会了吗?
    利用 Amazon CodeWhisperer 激发孩子的编程兴趣
    垃圾回收器-G1垃圾回收器详解
    大数据技术之HBase+Redis详解
  • 原文地址:https://blog.csdn.net/smile_otl/article/details/137913064