Ex: enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri},
enum Mode {Create, Delete, Modify}
If we can add description to each enum value then we can display the particular description instead of enum value. This article will focus on adding descriptive attribute to enum using extension method.
Step 1:
Create sample enum like below which is use to store form
name categories and add following directive
using
System.ComponentModel;
(To add the descriptive attribute for each enum we should
use above directive)
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ComponentModel;
namespace
EnumExample
{
public enum FormCategory
{
RegistrationForm,
DisplayClient,
DeleteClient,
EditClient,
AddNewClient,
}
}
Step 2 :
Then add the descriptive attribute to each enum which is
going to display to end user.
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ComponentModel;
namespace
EnumExample
{
public enum FormCategory
{
[Description("Client Registration Form")]
RegistrationForm,
[Description("Display Client Information Form")]
DisplayClient,
[Description("Delete Client Form")]
DeleteClient,
[Description("Edit Existing Client Form")]
EditClient,
[Description("Add New Client Form")]
AddNewClient,
}
}
Step 3 :
Mechanism behind displaying enum description instead of enum
value is .Net Reflection. The extension method will use reflection to examine
the values in any enumeration and look for the Description attribute. Where
present, the description will be returned. If the description is absent, the
constant name will be returned instead.
Create static class
called EnumExampleExtensions.cs and add following
directives,
using System.Reflection;
using
System.ComponentModel;
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Reflection;
namespace
EnumExample
{
public static class EnumExampleExtensions
{
}
}
Step 4:
Create static extension method Description which takes enum
as parameter like below.
public static string
Description(this Enum
enumValue)
{
var
enumType = enumValue.GetType();
var
field = enumType.GetField(enumValue.ToString());
var
attributes = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
return
attributes.Length == 0
? enumValue.ToString()
: ((DescriptionAttribute)attributes[0]).Description;
}
In above code take the enum value and then resolve its
description. GetField method is called, passing the name of the enumeration
value provided in the method's parameter. This obtains the field information
for the value. Finally check the description attribute and if attribute
contains description return the description if not return the enum constant name.
Step 5:
Now it's time to test the code. I'm using console
application for test the code.
Code :
Console.WriteLine(string.Format("Enum Value{0}",
FormCategory.RegistrationForm.ToString()));
Console.WriteLine(
string.Format("Enum Description{0}",
FormCategory.RegistrationForm.Description()));
Console.ReadKey();
Output:
No comments:
Post a Comment