Pardesi Services Logo Pardesi Services Logo

Softomatix
Web Development
Windows Development


Use Reflection To Generate Complete HatchStyle Chart

In our previous article on reflection we discussed how to generate complete color chart. While doing our ASPChart.Net project we wanted o add support for hatched backgrounds. So we decided to generate a chart that will show us what all HatchStyle values defined in the enumeration look like. So this time we decided to use the same concepts of reflection to get the complete list of HatchStyle values and generate the chart.

The technique remains same as our previous artcile with the difference that you need to call GetFields method on Type object instead of GetProperties method. The reason is pretty simple. The enumeration values are not properties. They are fields of the object.

  • Get Type of System.Drawing.Drawing2D.HatchStyle enumeration.
  • Call GetFields method on Type instance. Make sure that you use the appropriate BindingFlags attribute to get only the properties that are of interest. In our case we are only interested in public static properties. Therefore we will use BindingFlags.Public, BindingFlags.Static and BindingFlags.DeclaredOnly ORed together. The purpose of specifying BindingFlags.DeclaredOnly attribute is to get only the static properties that are declared only in this structure only. We are not interested in properties of any parent class or structure.
  • Iterate through each item in FieldInfo array value returned by GetFields method. Call Name property on FieldInfo object to get the name of each color. And call GetValue method to get the actual value of HatchStyle.
  • Rest is just implementation detail and book keeping on how to arrange all the hatch boxes on the bitmap and display it as a chart.

The complete code is attached with the article. Take a look at it for details.

HatchStyle testStyle = HatchStyle.Cross;
Type hatchType = testStyle.GetType();
if (null != hatchType)
{
	FieldInfo[] membersList =
	 hatchType.GetFields(BindingFlags.Static|BindingFlags.DeclaredOnly|BindingFlags.Public);
	int nNumProps = propInfoList.Length;
	for (int i = 0; i < nNumRows; i++)
	{
		FieldInfo fieldInfo = (FieldInfo)membersList[nIdx];
		HatchStyle hatchStyle = (HatchStyle)fieldInfo.GetValue(testStyle);
		string strHatchName = fieldInfo.Name;
	}
}							
									

Please feel free to send your suggestions directly to us at softomatix@pardesiservices.com.


Home     About us    Contact us  Comments/Queries
Copyright   Privacy Policy  Shipping Policy  Return Policy

Copyright (c) 1999-2002 Pardesi Services LLC


Hit Counter