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.
Something like this:
instanceProps.Cast<PropertyDescriptor>().Concat(customProperties).ToArray()
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.
We can fix this though, just define your own CustomPropertyDescriptor
class by deriving from PropertyDescriptor
and implementing all the abstract methods.
Something like this:
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.
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.
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!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…