Sign in to follow this  

[C#] Adding properties to objects at runtime

Recommended Posts

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);

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!

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
It looked like the CodeProject article was a good solution, but it seems to be much too limiting.

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

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!

[Edited by - ricekrispyw on June 18, 2006 11:36:45 AM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this