Tuesday, August 05, 2008

Simple reflection form

Haven't you had some poco's (plain old CLR object) you just want to edit, but you had to make a form for each object?

The solution to this is dot net's reflection. The SimpleReflectionForm takes an object like the Customer below and shows this in a form.


public class Customer
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public bool Married { get; set; }
}

Just call the form:


Customer c = new Customer
{
Id = Guid.NewGuid(),
Name = "Frank",
Age = 31,
Married = true
};

using (SimpleReflectionForm f
= new SimpleReflectionForm(c, "Id"))
{
f.ShowDialog();
}

The following form is shown:


As you can see the Id is ignored, by the optional parameter string[] propertiesToExclude.
The form supports all native .net types (int, string, datetime, guid etc). Guid and numeric types are verified when the Ok button is pressed.


You can download the source of SimpleReflectionForm
here.

Enjoy!

2 comments:

Delly said...

Nice one, was looking and looking for this, thank god I didnt have to write it! Karma

Mic said...

This should be a standard feature of .NET!