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;
}
}
Is it possible to iterate over the Properties list to add each property to PropertyDescriptorCollection?
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
}
}
}
Now those Properties would be setted to instance properties whenever GetPropertiesis called. Am I thinking this the right way?
You are already creating a collection like this:
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:
Next problem: you need
customPropertieswhich is a collection ofPropertyDescriptor. UnfortunatelyPropertyDescriptoris an abstract class so you don’t have an easy way to create one.We can fix this though, just define your own
CustomPropertyDescriptorclass by deriving fromPropertyDescriptorand implementing all the abstract methods.Something like this:
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
CustomPropertyDescriptorfilled in with information appropriate to your dynamic properties, and concatenate it to the basic properties as I described initially.The
PropertyDescriptornot 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!