Modifying a Type at runtime is always difficult.
Requirement: Object must be assigned to a PropertyGrid. To expand the nested complex types in the Object, TypeConvertor attributes should be added to the properties of the type. Type should be modified not be modified at design time.
Solution: The PropertyGrid does not expand the nested complex types of an object assigned to it, if the complex type is a user-defined object. We need to add TypeConverterAttribute(typeof(ExpandableObjectConverter) to the user-defined types. Since I cannot modify the user-defined type at design time, it was not a feasible option. That leaves me with two alternatives:
Alternative 1: Tweak the Property Grid to show the nested Complex types. Given the Complexity of Property Grid and the time constraint to solve the problem, I had to go to the second alternative.
Alternative 2: After searching lot on the internet, I stumbled here. The way to achieve the task is to bluff the Property Grid. Before you create the object, add the attributes to the properties of the type at runtime using COM (not Reflection). What you do is tell the Property Grid that the Type you used has the Attributes required for the former display the nested complex types.
The Code goes something like this:
AttributeCollection oldAttributes = TypeDescriptor.GetAttributes(type);
Attribute[] newAttributes = new Attribute[oldAttributes.Count + 1];
oldAttributes.CopyTo(newAttributes, 0);
newAttributes[oldAttributes.Count] = new TypeConverterAttribute(typeof(ExpandableObjectConverter));
TypeDescriptor.AddAttributes(type, newAttributes);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }