|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Click Image to Enlarge IntroductionI had been looking everywhere about information on BackgroundI was in the process of creating a 3D engine in C#, and I was making tools to edit materials and a lot of more stuff. Each material, texture, etc, can have tons of properties, and I was aiming to discover a way of making my UI fast and with no mistakes. The usual way (making labels and textboxes/comboboxes/whatever) would be just too time-consuming, and I was (am) on a tight schedule. So I came across the As the C# newbie I am, I started fiddling around the namespace Immerse.Editables
{
[DefaultProperty("Name")]
public class EditableIBitmap
{
private IBitmap bmp;
public EditableIBitmap(IBitmap bmp)
{
this.bmp = bmp;
}
So, this is not complete, but might give you a nice idea about how I got my editable wrappers implemented. Now, when put on a So I made my own For example, public enum TextureTileTypes
{
Tile,
Clmp,
ClmpB,
ClmpE,
}
So in the Trying to find some documentation in the net, everyone suggested that I should make a " So, using the public enum TextureTileTypes
{
[Description("Tile")] Tile,
[Description("Clamp")] Clmp,
[Description("Clamp to Border")] ClmpB,
[Description("Clamp To Edge")] ClmpE,
}
They're, much easier to edit, much easier to code, and definitely no hassle to get a human-readable description of it. Now I just needed a way for the Using the codeThe code should be self-explanatory for anyone who has ever done any kind of custom Notice the following function: public static string GetEnumDescription(Enum value)
{
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes
(typeof(DescriptionAttribute), false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();
}
That should return the human-readable description set to the The other way around: public static string GetEnumName(System.Type value, string description)
{
FieldInfo [] fis = value.GetFields();
foreach(FieldInfo fi in fis)
{
DescriptionAttribute [] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes
(typeof(DescriptionAttribute), false);
if(attributes.Length>0)
{
if(attributes[0].Description == description)
{
return fi.Name;
}
}
}
return description;
}
So you can use those separately to retrieve any human-readable-form About (the juicy stuff), using the [System.ComponentModel.TypeConverter(typeof(Jcl.Util.EnumDescConverter))]
public MyEnumType MyProperty
{ get { return myEnumVariable; } set { myEnumVariable = value; } }
Of course, you are 100% allowed to change the namespace and put it on your own :-) Points of InterestThere are pretty much unbearable bugs appearing up when you start writing LicenseYou can use the code for free, you may include it in your applications, and you might modify it. Basically, do whatever you want with it. If you get to use it in your application, make sure you notify me about it, and credit would be appreciated, while not required. HistoryFirst version of this document on 25th Feb 2004. Will try to make an example project soon. ContactYou may contact me for anything related to this article at jcl at vmslabs dot com.
|
||||||||||||||||||||||