Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction

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.

Implementation

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(
    myGrid.Color,
    "The color of the lines used to draw the grid",
    "Grid");

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

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

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(
        name,
        PropertyAttributes.None,
        s.Value.GetType(),
        new Type[] {}); 
    Type objType = s.Value.GetType(); 

    //define the get method for the property 
    MethodBuilder getMethod = tb.DefineMethod(
        "get_"+name,
        MethodAttributes.Public,
        objType,
        new Type[]{}); 

    ILGenerator ilg = getMethod.GetILGenerator(); 
    ilg.DeclareLocal(objType); 
    ilg.Emit(OpCodes.Ldarg_0); 
    ilg.Emit(OpCodes.Ldfld,hash); 
    ilg.Emit(OpCodes.Ldstr,name); 
    ilg.EmitCall(OpCodes.Callvirt,
        typeof(Hashtable).GetMethod("get_Item"),
        null); 

    if(objType.IsValueType) 
    { 
        ilg.Emit(OpCodes.Unbox,objType); 
        if(typeHash[objType]!=null) 
            ilg.Emit((OpCode)typeHash[objType]); 
        else ilg.Emit(OpCodes.Ldobj,objType); 
    } 
    else 
        ilg.Emit(OpCodes.Castclass,objType);
 
    ilg.Emit(OpCodes.Stloc_0); 
    ilg.Emit(OpCodes.Br_S,(byte)0); 
    ilg.Emit(OpCodes.Ldloc_0); 
    ilg.Emit(OpCodes.Ret); 

    //now we generate the set method for the property 
    MethodBuilder setMethod = tb.DefineMethod(
        "set_"+name,
        MethodAttributes.Public,
        null,
        new Type[]{objType}); 

    ilg = setMethod.GetILGenerator(); 
    ilg.Emit(OpCodes.Ldarg_0); 
    ilg.Emit(OpCodes.Ldfld,hash); 
    ilg.Emit(OpCodes.Ldstr,name); 
    ilg.Emit(OpCodes.Ldarg_1); 

    if(objType.IsValueType) 
        ilg.Emit(OpCodes.Box,objType); 
    ilg.EmitCall(OpCodes.Callvirt,
        typeof(Hashtable).GetMethod("set_Item"),
        null); 

    ilg.Emit(OpCodes.Ret); 

    //put the get/set methods in with the property 
    pb.SetGetMethod(getMethod); 
    pb.SetSetMethod(setMethod); 
}

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.

History

Update: Minor text changes/grammar.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Ben Ratzlaff

United States United States
Graduate of the Computer Science department at the University of Arizona

Comments and Discussions

 
GeneralSubproperty Pinmemberviciouskinid3-Apr-10 15:36 
GeneralSubproperties PinmemberHeptagonal29-Feb-08 13:15 
QuestionNewbie - Confused Pinmembermcampster19-Sep-07 2:23 
AnswerRe: Newbie - Confused Pinmemberchris1755-Nov-07 2:56 
NewsUpdate Pinmemberchris17512-Sep-07 16:35 
Questionreadonly property Pinmemberxupi13-Jul-07 2:46 
AnswerRe:resolved -> readonly property Pinmemberxupi13-Jul-07 3:05 
GeneralComment..... Pinmemberchris1754-Jun-07 8:00 
After taking a lot of time and modifing your work. I have come to the conclusion that almost anything is possible with your framework. I have been successful in using your code to do the following.....
 
1. Create a custom Font class that has the look and feel like placing the font object on the property grid except the GDICharSet and GdiVerticalFont properties have been removed. This includes having the font property look like an expandable tree node.
2. Create the ability to put custom TypeConverters and Editors for certain properties.
3. The ability to have update events sent back to the calling class. Ex: I have a property in the property grid called "MyFont". I update the Strikeout to be true under the property "MyFont". After doing so an event is fire and received in my "CustomClass" which I can then set the font for "CustomClass" to be strikeout.
4. Dynamically Loading Assemblies that contain classes that use your framework still works!!!!. Ex: Project2 references Project1. Project1.exe load up a class in Project2.dll and loads it onto the properties grid in Project1.exe.
 

The only thing my version of your framework that I am having trouble with is two or more properties with the same name. Other than that your frameworks works VERY VERY nicely.
 
Chris
GeneralRe: Comment..... PinmemberBen Ratzlaff22-Jun-07 5:42 
GeneralHide GdiVerticalFont and GdiCharSet from Font in Property Grid PinmemberPiotr Jozwiak28-Jan-10 20:56 
NewsAnother Update needed. Pinmemberchris17525-Apr-07 6:06 
NewsSuggestions Pinmemberchris17513-Apr-07 7:39 
GeneralICustomTypeDescriptor PinprotectorHeath Stewart7-Nov-05 20:11 
GeneralRe: ICustomTypeDescriptor PinmemberBen Ratzlaff9-Nov-05 9:53 
GeneralRe: ICustomTypeDescriptor Pinmemberchris17525-Apr-07 4:21 
GeneralUsing dynamic Enums PinmemberSampath Narayanan6-Nov-05 20:20 
GeneralRe: Using dynamic Enums Pinmemberchris17525-Apr-07 3:51 
GeneralAdjusting read access at run time for diffrence permission PinmemberIanJamesWood[]7-Sep-05 5:08 
GeneralHiding Properties in PropertyGrid PinmemberVarun Gulati2-Jan-05 23:34 
GeneralRe: Hiding Properties in PropertyGrid Pinmemberchris17525-Apr-07 3:50 
GeneralWhy am i getting Key as the Property name and ICollection as the value Pinmemberjen0s22-Nov-04 5:43 
GeneralAdding attributes for PropertyGrid's use PinsussAnonymous17-Sep-04 16:40 
GeneralRe: Adding attributes for PropertyGrid's use PinmemberBen Ratzlaff17-Sep-04 18:06 
GeneralRe: Adding attributes for PropertyGrid's use PinsussAnonymous21-Sep-04 7:20 
GeneralRe: Adding attributes for PropertyGrid's use PinmemberBen Ratzlaff23-Sep-04 6:54 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 31 Jan 2004
Article Copyright 2004 by Ben Ratzlaff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid