Click here to Skip to main content
13,349,033 members (70,434 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked
Posted 30 Jan 2004

Populating a PropertyGrid using Reflection.Emit

, 30 Jan 2004
Rate this:
Please Sign up or sign in to vote.
How to create a class at runtime so you can use its properties in a PropertyGrid.


I like using the PropertyGrid as a way for the user to specify various options when using my programs, but I got tired of constantly having to change the class that stored those options. I wanted an easy way to change what properties the grid would show without me having to change anything in a class structure.

Since the PropertyGrid automatically shows the properties of an object and the property's value, the idea came to me that I could dynamically create a type at runtime using the classes contained in Reflection.Emit, so that's what I did.


I needed a good way to store all the information needed to create each property. I did this with the Setting object. Setting objects store the property name, the initial value of the property, a description and category to be shown in the PropertyGrid, and an EventHandler so you can catch when the value of the property changes.

Secondly, I wanted to create an easy storage device for my Setting objects. This is what the Settings object is for. It is essentially a wrapper around a HashTable that does the casting for you. The Settings object is what makes the CustomPropertyGrid come to life.

By setting the Settings property of the CustomPropertyGrid object to your own Settings object, an internal type will be created having the properties that were specified as Setting objects. The type will have an internal HashTable to make the emitted code easy to write. For instance, if you have the following code:

Settings settings = new Settings();

settings["GridColor"]=new Setting(
    "The color of the lines used to draw the grid",

the type that is created will have a property written like so:

[Description("The color of the lines used to draw the grid")]
public Color GridColor
  get{return (Color)myHash["GridColor"];} 

Emitting the bytecode

Doing this was fairly tricky, as this was a learning experience for me. The procedure consisted of writing a small class having the property signature I desired and using ildasm and peverify to examine the bytecodes. From that, I gathered enough information to emit code that works. Working with objects is easy, as there is a bytecode to load and store objects no problem, but value types are another matter. To store a value type, you must box it, and then you can treat it like an object. To retrieve a value type, you must unbox it (naturally) and then you have to load it based on what type it is. If it is a numeric type, there are different bytecodes to load ints, chars, doubles, floats, etc. If you have a value type that is not a numeric type (like a Color, or an enum) then it's just a matter of loading the type of the object. Figuring out how to load the value types was the hardest part of this project for me to figure out.

private void emitProperty(
    TypeBuilder tb,
    FieldBuilder hash,
    Setting s,
    string name) 
    //to figure out what opcodes to emit, i would compile a small class 
    //having the functionality i wanted, and viewed it with ildasm. 
    //peverify is also kinda nice to use to see what errors there are. 
    //define the property first 
    PropertyBuilder pb = tb.DefineProperty(
        new Type[] {}); 
    Type objType = s.Value.GetType(); 

    //define the get method for the property 
    MethodBuilder getMethod = tb.DefineMethod(
        new Type[]{}); 

    ILGenerator ilg = getMethod.GetILGenerator(); 

        else ilg.Emit(OpCodes.Ldobj,objType); 

    //now we generate the set method for the property 
    MethodBuilder setMethod = tb.DefineMethod(
        new Type[]{objType}); 

    ilg = setMethod.GetILGenerator(); 



    //put the get/set methods in with the property 

Once all the properties are emitted, I use reflection to create an object of that type and set the SelectedObject property to this new object. When the form is shown, all the properties and their values are in a categorized fashion.

There is another custom property in the CustomPropertyGrid called InstantUpdate. By default, InstantUpdate is on and what it does is, when a value is changed in the PropertyGrid, an event is fired, the CustomPropertyGrid catches the event and passes it along to the event in the specific Setting object. The demo program included shows three different ways of handling these events. If you do not wish these events to be fired, just set the InstantUpdate property to false. Also, if you do not construct a Setting with an EventHandler, the event will not be fired regardless of the value of InstantUpdate.


Update: Minor text changes/grammar.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Ben Ratzlaff
Software Developer (Senior)
United States United States
Graduate of the Computer Science department at the University of Arizona

You may also be interested in...


Comments and Discussions

GeneralRe: Adding attributes for PropertyGrid's use Pin
Ben Ratzlaff17-Sep-04 19:06
memberBen Ratzlaff17-Sep-04 19:06 
GeneralRe: Adding attributes for PropertyGrid's use Pin
Anonymous21-Sep-04 8:20
sussAnonymous21-Sep-04 8:20 
GeneralRe: Adding attributes for PropertyGrid's use Pin
Ben Ratzlaff23-Sep-04 7:54
memberBen Ratzlaff23-Sep-04 7:54 
GeneralRe: Adding attributes for PropertyGrid's use Pin
chris17525-Apr-07 4:48
memberchris17525-Apr-07 4:48 
GeneralDynamic creation of Property Grid Info Pin
ananthrags15-Jul-04 17:04
memberananthrags15-Jul-04 17:04 
GeneralRe: Dynamic creation of Property Grid Info Pin
Ben Ratzlaff15-Jul-04 19:54
memberBen Ratzlaff15-Jul-04 19:54 
GeneralRe: Dynamic creation of Property Grid Info Pin
ananthrags18-Jul-04 16:50
memberananthrags18-Jul-04 16:50 
GeneralAdding Expandable Properties Pin
jnewhall30-Jun-04 9:20
memberjnewhall30-Jun-04 9:20 

I am using the Custom Property grid, and it is working well for me.

I now need to add expandable properties or sub-properties. This list of sub-properties needs to be dynamic (i.e. added to or subtracted from at runtime).

The Windows Property Grid supports expandale property lists through the use of Type Converters. But, the sub-properties in those lists must be known at compile time.

Can I use or modify the custom grid to support dynamic lists of (expandable) sub-properties?


GeneralApply TypeConverter attribute Pin
Naga Sudha22-Apr-04 4:57
memberNaga Sudha22-Apr-04 4:57 
GeneralRe: Apply TypeConverter attribute Pin
chris17525-Apr-07 4:48
memberchris17525-Apr-07 4:48 
GeneralSave Settings Pin
kuerbis31-Jan-04 23:01
memberkuerbis31-Jan-04 23:01 
GeneralRe: Save Settings Pin
blotch1-Feb-04 11:37
memberblotch1-Feb-04 11:37 
GeneralUserfriendly Property-Names Pin
kuerbis31-Jan-04 15:27
memberkuerbis31-Jan-04 15:27 
GeneralRe: Userfriendly Property-Names Pin
Ben Ratzlaff31-Jan-04 16:32
memberBen Ratzlaff31-Jan-04 16:32 
GeneralRe: Userfriendly Property-Names Pin
blotch1-Feb-04 11:33
memberblotch1-Feb-04 11:33 
GeneralUse CustomPropertyGrid Pin
Rothmans16-Jun-05 4:39
memberRothmans16-Jun-05 4:39 
GeneralRe: Userfriendly Property-Names Pin
sylvainbouche13-Jan-06 6:41
membersylvainbouche13-Jan-06 6:41 
GeneralRe: Userfriendly Property-Names Pin
chris17525-Apr-07 4:48
memberchris17525-Apr-07 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 31 Jan 2004
Article Copyright 2004 by Ben Ratzlaff
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid