We know, All the enum items have only two properties : value and name. Let's consider the following enum
public enum BackGroundCheckStatus : int
{
NotStarted = 0,
Created = 1,
Pending = 2,
Declined = 3,
Approved = 4
}
Here the "Created " item of the above enum has only two properties : Name="Created " and Value="1". Now if anybody wants to add one more property to describe each item then he can do it using Attribute. For that
1. Declare an Attribute like
[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field, AllowMultiple = false)]
public sealed class EnumDescription : Attribute
{
private string _description;
public string Description
{
get
{
return this._description;
}
}
public EnumDescription(string description)
: base()
{
this._description = description;
}
}
2. Modify the enum as follows
public enum BackGroundCheckStatus : int
{
[EnumDescription("Not Started")]
NotStarted = 0,
[EnumDescription("Query Created")]
Created = 1,
[EnumDescription("Query Sent & Response Pending")]
Pending = 2,
[EnumDescription("Declined To Answer")]
Declined = 3,
[EnumDescription("Responsed")]
Approved = 4
}
3. Add an extension function as follows
public static class EnumExtension
{
public static string ToDescription(this Enum value)
{
Type type = value.GetType();
MemberInfo[] memInfo = type.GetMember(value.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(
typeof(EnumDescription),
false);
if (attrs != null && attrs.Length > 0)
return ((EnumDescription)attrs[0]).Description;
}
return value.ToString();
}
}
4. Get the description of any item
string enumDescription=BackGroundCheckStatus.Pending.ToDescription();
That's all. Hope it will help. Thanks.
Tuesday, September 1, 2009
Extension Function For Enumeration Type in C#
I have the following enum
enum OrderStatus : int
{
Draft = 1,
Pending = 2,
Approved = 3,
Rejected = 4
}
Now if i want to get the enum value to a variable then i have to write the following line of code
int currentOrderStatus=(int)OrderStatus.Draft;
I can do the same thing in an alternative manner i.e by adding an extension function instead of explicit conversion. Here is the code for that
public static class EnumExtension
{
public static int ToInteger(this Enum value)
{
object objValue = Enum.Parse(value.GetType(), value.ToString());
return (int)objValue;
}
}
Now I can write
int currentOrderStatus=OrderStatus.Draft.ToInteger();
Happy coding.... Thanks
enum OrderStatus : int
{
Draft = 1,
Pending = 2,
Approved = 3,
Rejected = 4
}
Now if i want to get the enum value to a variable then i have to write the following line of code
int currentOrderStatus=(int)OrderStatus.Draft;
I can do the same thing in an alternative manner i.e by adding an extension function instead of explicit conversion. Here is the code for that
public static class EnumExtension
{
public static int ToInteger(this Enum value)
{
object objValue = Enum.Parse(value.GetType(), value.ToString());
return (int)objValue;
}
}
Now I can write
int currentOrderStatus=OrderStatus.Draft.ToInteger();
Happy coding.... Thanks
Subscribe to:
Posts (Atom)