c# 反射動(dòng)態(tài)添加Attribute
//測(cè)試類
var type = typeof(TestClass);
//給類對(duì)象添加、獲取特性
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;
留待后查,同時(shí)方便他人
聯(lián)系我:renhanlinbsl@163.com
聯(lián)系我:renhanlinbsl@163.com

浙公網(wǎng)安備 33010602011771號(hào)