Click here to Skip to main content
Licence 
First Posted 27 Apr 2002
Views 56,623
Bookmarked 17 times

Use Reflection to generate a complete HatchStyle chart

By | 27 Apr 2002 | Article
A utility application to generate a chart displaying all HatchStyles

Sample Image - ReflectionHatch.jpg

While doing our ASPChart.Net project we wanted to 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Softomatix

Web Developer

United States United States

Member

To learn more about us, Please visit us at http://www.netomatix.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThink speed PinmemberPaul Selormey20:33 30 Oct '05  
GeneralRe: Think speed PinmemberRolias10:42 7 Nov '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 28 Apr 2002
Article Copyright 2002 by Softomatix
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid