Introduction
In C#, we use enum
s frequently. Enum
can only inherit from types byte
, sbyte
, short
, ushort
, int
, uint
, long
, or ulong
. But sometimes, we also may want to assign text value with it to extend its uses.
Background
I have been working with Entity framework code first. And I had been using enum
s to hold information like:
UserType
(SuperUser
, AdminUser
, … etc.) Permission
(UserManage
, UnitManage
, … etc.) and few more
It was going quite well. But a problem was introduced when I went to show the information based on enum
values to the real user.
Real user doesn’t know what emun
is. User type values like (0
, 1
, 2
… etc.) or (SuperUser
, AdminUser
, … etc.) are confusing to them. Words like (Super User, Admin User, … etc.) seem quite suitable to them.
So I started to add some extra code using attributes and extinction methods, so that my emun
could hold some little text description regarding it.
Using the Code
Here is the enum
which will hold our text values. Here, we are using two custom attributes CustomEnum
and TextValue
which we are going to see in detail within moments.
[CustomEnum(true)]
public enum PermissionEnum
{
[TextValue("User setup manage")]
UserManage = 10,
[TextValue("Unit setup manage")]
UnitManage = 2,
[TextValue("Exam setup manage")]
ExamManage = 3,
ExaminerManage = 1
}
Once we have created the enum
, we will use it like this. Here, TextValue()
is an extension method to get the assigned text.
const PermissionEnum aPermission = PermissionEnum.UserManage;
String name = aPermission.ToString();
String textValue = aPermission.TextValue();
int value = (int)aPermission;
Here are the two custom attributes, CustomEnum
and TextValue
that we mentioned earlier.
[AttributeUsage(AttributeTargets.Enum)]
public class CustomEnumAttribute : Attribute
{
public bool IsCustomEnum { get; set; }
public CustomEnumAttribute(bool isCustomEnum) : this()
{
IsCustomEnum = isCustomEnum;
}
private CustomEnumAttribute()
{
IsCustomEnum = false;
}
}
[AttributeUsage(AttributeTargets.Field)]
class TextValueAttribute : CustomEnumAttribute
{
public String Value { get; set; }
public TextValueAttribute(string textValue) : this()
{
if (textValue == null)
{
throw new NullReferenceException("Null not allowed in textValue at TextValue attribute");
}
Value = textValue;
}
private TextValueAttribute() : base(true)
{
Value = string.Empty;
}
}
And last of all, we need the extension method for the enum
which will help us to get the assigned text.
public static class CustomEnumUtility
{
public static string TextValue(this Enum myEnum)
{
string value = string.Empty;
var customEnumAttribute = (CustomEnumAttribute) myEnum
.GetType()
.GetCustomAttributes(typeof(CustomEnumAttribute), false)
.FirstOrDefault();
if (customEnumAttribute == null)
{
throw new Exception("The enum don't contain CustomEnumAttribute");
}
else if (customEnumAttribute.IsCustomEnum == false)
{
throw new Exception("The enum is not a custom enum");
}
var textValueAttribute = (TextValueAttribute)myEnum
.GetType().GetMember(myEnum.ToString()).Single()
.GetCustomAttributes(typeof(TextValueAttribute), false)
.FirstOrDefault();
value = (textValueAttribute != null) ? textValueAttribute.Value : string.Empty;
return value;
}
}
You may find the project in the attachment.