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

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



deloford 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!

iair bender said...

i think this also writes back to changes to passed object when Pressed OK button isnt it?? that really good..