Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are already creating a collection like this:</p> <pre><code>PropertyDescriptorCollection propsCollection = new PropertyDescriptorCollection(instanceProps.Cast&lt;PropertyDescriptor&gt;().ToArray()); </code></pre> <p>But the collection you are creating only has the existing properties, not your new properties.</p> <p>You need to supply a single array concatenated from the existing properties and your new properties.</p> <p>Something like this:</p> <pre><code>instanceProps.Cast&lt;PropertyDescriptor&gt;().Concat(customProperties).ToArray() </code></pre> <p>Next problem: you need <code>customProperties</code> which is a collection of <code>PropertyDescriptor</code>. Unfortunately <code>PropertyDescriptor</code> is an abstract class so you don't have an easy way to create one.</p> <p>We can fix this though, just define your own <code>CustomPropertyDescriptor</code> class by deriving from <code>PropertyDescriptor</code> and implementing all the abstract methods.</p> <p>Something like this:</p> <pre><code>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; } } </code></pre> <p>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.</p> <p>Then you need to create an array of <code>CustomPropertyDescriptor</code> filled in with information appropriate to your dynamic properties, and concatenate it to the basic properties as I described initially.</p> <p>The <code>PropertyDescriptor</code> not only <strong>describes</strong> 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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload