c# 反射动态添加Attribute
//测试类
var type = typeof(TestClass);
//给类对象添加、获取特性
TypeDescriptor.AddAttributes(type, indexAttr);
//var attr = TypeDescriptor.GetAttributes(type)[typeof(DynamicCacheBufferAtrribute)] as DynamicCacheBufferAtrribute;
//给FieldInfo添加、获取特性
//var class2Inst = new MyClass2();
//class2Inst.Class1Obj = new MyClass1();
var fieldInfo = type.GetField("Class1Obj");
TypeDescriptor.AddAttributes(fieldInfo, indexAttr);
//var attr = TypeDescriptor.GetAttributes(fieldInfo)[typeof(DynamicCacheBufferAtrribute)] as DynamicCacheBufferAtrribute;
//给Property添加、获取特性
var propertyInfo = type.GetProperty("Class1Obj");
TypeDescriptor.AddAttributes(propertyInfo, indexAttr);
//var attr = TypeDescriptor.GetAttributes(propertyInfo)[typeof(DynamicCacheBufferAtrribute)] as DynamicCacheBufferAtrribute;
留待后查,同时方便他人
联系我:renhanlinbsl@163.com
联系我:renhanlinbsl@163.com