Click here to Skip to main content
15,886,026 members
Articles / Desktop Programming / Windows Forms

Generic user control to edit any arbitrary object

Rate me:
Please Sign up or sign in to vote.
4.50/5 (7 votes)
15 Nov 2009CPOL2 min read 32.1K   349   17   8
This article will provide insight into how Reflection could be helpful in developing generic desktop user control interfaces.

Introduction

An arbitrary data type in any assembly (.NET DLL) can be instantiated (created), explored (what fields are there), and modified by the user (as long as the user meets the security criteria of the assembly provider).

This article will provide an insight into how Reflection could be helpful in developing generic desktop user control interfaces. It will walk you through creating a generic Windows Form which will accept user input for any arbitrary object.

Background

The power of Reflection lies with the creativity of a developer. Reflection has proven to be useful in many frameworks in Java and .NET based languages. However, we should understand the performance implications of using Reflection in our applications. A good article to read on performance implications in .NET is by Joel Pobar. As more projects are using Service Oriented Architecture (where atomic functionality and stateless operation is key for a successful service offering) in their business solutions, it has increased the amount of test cases needed for quality control. For a practical example of using this control in an application, see SoapPanda (a SOAP client to test Web Services). I guess this much philosophy is good enough on the subject… let's look in to the control code.

How to use this control

The application starts with a demo form. You will browse and select an assembly .NET DLL of your choice or one provided with this code sample.

Image 1

You can select a data type of the assembly from the list box, and click “Create”. It will create an instance of the data type and an object form for you to edit its fields.

Image 2

As with any dialog form, you will make modifications and save them by clicking OK. You can click Edit to verify your changes are still in the application.

Alternatively, you can select a data type of the assembly from the list box and click “Create Array”. You can create multiple objects of the selected type and add them in the list. You can click “Edit Array” to modify this list from the demo form.

Image 3

Code and design

There are two main User Control classes (ComplexType and ComplexTypeArray) which are provide bulk of the features for the generic object edit control. Let’s begin with exploring the constructor of ComplexType.

C#
public ComplexType(Assembly assembly, Object instance, Control parent, bool bProperty)
{
    this.parent = parent;
    this.instance = instance;
    this.clientAssembly = assembly;
    
    this.isProperty = bProperty;

    InitializeComponent();

    if(isProperty)
        AddPropertyControls(this.instance.GetType(), this.panel1);
    else
        AddFieldControls(this.instance.GetType(), this.panel1);
}

The main design difficulty I had to overcome was allowing any number of objects and the depth of an object within the objects being expanded and collapsed to any extent as long as we have the definition in the assembly (.NET DLL). In my example code, you will notice that I have an Employee data type referencing its own type, making it a good example of expanding the control to any depth.

You will notice that the two main functions are called AddPropertyControl and AddFieldControl. As the names suggest, the fields can be public and private member variables of the datatype, whereas the property is mostly public.

C#
public void AddFieldControls(Type moduleType, Panel panl)
{
    FieldInfo[] fields = moduleType.GetFields(BindingFlags.Instance | 
                            BindingFlags.Public | BindingFlags.NonPublic);
    //true for if moduleType is enum,int,string,bool,double etc
    if (IsBasicType(moduleType))
    {
        //create basic controls
    }else{
        foreach (FieldInfo fieldInfo in fields)
        {
            //create controls for each field type

        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSome more thoughts Pin
Asher Barak31-Jan-10 1:42
professionalAsher Barak31-Jan-10 1:42 
GeneralNice!!! Pin
RAND 45586614-Dec-09 8:01
RAND 45586614-Dec-09 8:01 
GeneralThoughts Pin
konikula17-Nov-09 4:59
konikula17-Nov-09 4:59 
QuestionDid you consider using PropertyGrid? Pin
Yoram Singer16-Nov-09 12:20
Yoram Singer16-Nov-09 12:20 
AnswerRe: Did you consider using PropertyGrid? Pin
Anantjot Anand16-Nov-09 17:53
Anantjot Anand16-Nov-09 17:53 
GeneralThoughts Pin
PIEBALDconsult15-Nov-09 17:18
mvePIEBALDconsult15-Nov-09 17:18 
GeneralRe: Thoughts Pin
Anantjot Anand16-Nov-09 7:04
Anantjot Anand16-Nov-09 7:04 
GeneralRe: Thoughts Pin
Adrian Cole16-Nov-09 9:34
Adrian Cole16-Nov-09 9:34 

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.