Friday, 12 June 2009

Getting all the fields in the structure Color

If you ever wanted to know how to get all the colour fields in the structure COlor, you can use the following bit of code:

Type t = typeof(Color);
System.Reflection.PropertyInfo[] colours = t.GetProperties();

foreach (System.Reflection.MemberInfo c in colours)
{
// How you would use c, cboxBackColour is a combo box
cboxBackColour.Items.Add(c.Name.ToString());
}

Basically, System.Type class uses the typeof method that can then be adopted to get all the properies within that class using reflection.

No comments: