在运行时添加的属性 [英] Add properties at runtime
问题描述
我有一个类,程序员可以使用它来动态地添加新的特性。对于它实现了 ICustomTypeDescriptor
来能够覆盖的GetProperties()
方法。
I have a class which the programmer can use to dynamically add new properties. For that it implements the ICustomTypeDescriptor
to be able to override GetProperties()
method.
public class DynamicProperty
{
public object Value { get; set; }
public Type Type { get; set; }
public string Name { get; set; }
public Collection<Attribute> Attributes { get; set; }
}
public class DynamicClass : ICustomTypeDescriptor
{
// Collection to code add dynamic properties
public KeyedCollection<string, DynamicProperty> Properties
{
get;
private set;
}
// ICustomTypeDescriptor implementation
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
// Properties founded within instance
PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Fill property collection with founded properties
PropertyDescriptorCollection propsCollection =
new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray());
// Fill property collection with dynamic properties (Properties)
foreach (var prop in Properties)
{
// HOW TO?
}
return propsCollection;
}
}
是否有可能遍历属性列表中的每个属性添加到 PropertyDescriptorCollection
?
基本上我想程序员能够在 DynamicProperty
添加到将被的GetProperties
处理集合。是这样的:
Basically I want the programmer to be able to add a DynamicProperty
to a collection which will be handled by GetProperties
. Something like:
new DynamicClass()
{
Properties = {
new DynamicProperty() {
Name = "StringProp",
Type = System.String,
Value = "My string here"
},
new DynamicProperty() {
Name = "IntProp",
Type = System.Int32,
Value = 10
}
}
}
现在的属性
将设置好的,以实例的属性,只要的GetProperties
被调用。我在这想以正确的方式?
Now those Properties
would be setted to instance properties whenever GetProperties
is called. Am I thinking this the right way?
推荐答案
您已经创建这样一个集合:
You are already creating a collection like this:
PropertyDescriptorCollection propsCollection =
new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray());
但你只能创建集合有现有的属性,不是你的新属性。
But the collection you are creating only has the existing properties, not your new properties.
您需要提供从现有的性能和新特性串接一个阵列。
You need to supply a single array concatenated from the existing properties and your new properties.
事情是这样的:
instanceProps.Cast<PropertyDescriptor>().Concat(customProperties).ToArray()
下一个问题是:你需要对象customProperties
是的PropertyDescriptor
的集合。不幸的是的PropertyDescriptor
是一个抽象类,所以你没有一个简单的方法来创建一个。
Next problem: you need customProperties
which is a collection of PropertyDescriptor
. Unfortunately PropertyDescriptor
is an abstract class so you don't have an easy way to create one.
我们虽然可以解决这个问题,只是由的PropertyDescriptor
继承和实现所有抽象定义自己的 CustomPropertyDescriptor
类方法。
We can fix this though, just define your own CustomPropertyDescriptor
class by deriving from PropertyDescriptor
and implementing all the abstract methods.
事情是这样的:
public class CustomPropertyDescriptor : PropertyDescriptor
{
private Type propertyType;
private Type componentType;
public CustomPropertyDescriptor(string propertyName, Type propertyType, Type componentType)
: base(propertyName, new Attribute[] { })
{
this.propertyType = propertyType;
this.componentType = componentType;
}
public override bool CanResetValue(object component) { return true; }
public override Type ComponentType { get { return componentType; } }
public override object GetValue(object component) { return 0; /* your code here to get a value */; }
public override bool IsReadOnly { get { return false; } }
public override Type PropertyType { get { return propertyType; } }
public override void ResetValue(object component) { SetValue(component, null); }
public override void SetValue(object component, object value) { /* your code here to set a value */; }
public override bool ShouldSerializeValue(object component) { return true; }
}
我还没有填写的电话来获取和设置你的属性;这些调用取决于如何你在引擎盖下实现的动态属性。
I haven't filled in the calls to get and set your properties; those calls depend on how you've implemented the dynamic properties under the hood.
然后你需要创建一个数组 CustomPropertyDescriptor
填写的信息适用于您的动态属性,正如我最初描述它串联到基本属性。
Then you need to create an array of CustomPropertyDescriptor
filled in with information appropriate to your dynamic properties, and concatenate it to the basic properties as I described initially.
的的PropertyDescriptor
不仅描述您的属性,它也使客户真正得到和设置这些属性的值。这是整点,是不是!
The PropertyDescriptor
not only describes your properties, it also enables client to actually get and set the values of those properties. And that's the whole point, isn't it!
这篇关于在运行时添加的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!