[C#] Adding properties to objects at runtime

Started by
3 comments, last by ricekrispyw 17 years, 10 months ago
I was wondering if it is possible to add properties to an object at runtime. I ask, because I want to have the user be able to define a custom "object" and define the properties for it. I then want them to be able to edit those properties in a PropertyGrid. So I might have a class BaseEntity that has properties for scale, etc... and I want the user to be able to create something like: Bird and give it properties like FeatherColor, Species, or something. The definitions for the custom things would be stored in an XML file, and then loaded at runtime. So, as for how to do it... Let's say I wanted to add a custom property to a button at runtime. I had thought you could do something like:
[sourcelang="c#"]Button guy = new Button();
PropertyDescriptor prop;
PropertyDescriptorCollection guyProps = TypeDescriptor.GetProperties(guy);
prop = TypeDescriptor.CreateProperty(this.GetType(), "SampleProp", "".GetType(), null);
guyProps.Add(prop);



But I get a "Specified method not supported" (in reference to guyProps.Add()) so it would seem that you cannot. Was I clear on what I am trying to do? Is it possible? Thanks very much!
The best way to predict the future is to invent it.
Advertisement
Yes, it is possible to add properties to objects, at least in .NET version 2.0. For example, the Table layout container uses property addition to add the row span and column span properties to its contained views, in the Forms designer.

However, I haven't done it myself, so I'll leave the specific reflection API description to those with actual hands-on experience.

At first glance, I'd look at TypeBuilder.DefineProperty, and the System.Reflection.Emit name space.
enum Bool { True, False, FileNotFound };
Yeah. I found an article about Reflection.Emit. It seems like a less-than-optimal solution, though. But I suppose it's something if all else fails.

Thanks for the quick response.
The best way to predict the future is to invent it.
That's not how TypeDescriptors work. You have to create your own class derived from TypeDescriptor. You then override the GetProperties method and come up with your own list of properties. Here are a few links I found on the topic that might help:

MSDN: Make Your Components Really RAD with Visual Studio .NET Property Browser
MSDN: ICustomTypeDescriptor, Part 1
MSDN: ICustomTypeDesciptor, Part 2
The Code Project: Dynamic Properties in the PropertyGrid
[EDIT]
It looked like the CodeProject article was a good solution, but it seems to be much too limiting.
[/EDIT]

Thank you very much! Those articles were extremely informative and helpful!

[SOLUTION]
For those looking at this to figure something out for themselves:

I ended up using this article and the associated control to accomplish what I wanted to do. See the comment made by jmonw (me) to see how I used it.

Best of luck!
[/SOLUTION]

[Edited by - ricekrispyw on June 18, 2006 11:36:45 AM]
The best way to predict the future is to invent it.

This topic is closed to new replies.

Advertisement