Mar*_*ell 11
需要看什么属性?如果它是数据绑定等,TypeDescriptor应该工作:
TypeDescriptor.AddAttributes(type, attribs);
TypeDescriptor.AddAttributes(instance, attribs);
Run Code Online (Sandbox Code Playgroud)
这只会影响System.ComponentModel使用(不是直接反射),但这通常就足够了 - 例如,您可以TypeConverter通过上面的方式关联.
如果"属性"你的意思是"属性",那么(同样,就数据绑定而言)TypeDescriptor也有潜力 - 但它是非平凡的; 你需要ICustomTypeDescriptor在对象上实现,或者CustomTypeDescriptor为类型编写一个- 在任何一种情况下,你需要编写自己的PropertyDescriptor实现(通常与每个实例字典交谈等).这将被使用的任何东西使用:
// only works if you use TypeDescriptionProvider
PropertyDescriptorCollection typeProps = TypeDescriptor.GetProperties(type);
// works via TypeDescriptionProvider or ICustomTypeDescriptor
PropertyDescriptorCollection objProps = TypeDescriptor.GetProperties(obj);
Run Code Online (Sandbox Code Playgroud)
同样,这涵盖了广泛的数据绑定和类似场景.举一个例子,请看这里 - 然而,这远非微不足道.示例用法(来自链接)在运行时添加两个属性:
Bag.AddProperty<int>("TestProp", new DefaultValueAttribute(5));
Bag.AddProperty<string>("Name");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14531 次 |
| 最近记录: |