From this article you can learn how to define and use enum description type converter so sytem will know how to handle enum to string and string to enum values (very helpful in PropertyGrid for example).
Implementation is available both in C# and VB.Net languages.
Implementation
Below you can find implementation we use in Karmian Framework as enum description type converter. It can be easily extended to work also as localizable enum description type converter - Description attribute can be used as a key of localized resource.
EnumDescriptionTypeConverter class
usingSystem;usingSystem.ComponentModel;usingSystem.Globalization;namespace Karmian.Core.TypeConverters{publicclass EnumDescriptionTypeConverter : EnumConverter
{public EnumDescriptionTypeConverter(Type type):base(type){}publicoverridebool CanConvertFrom(ITypeDescriptorContext context, Type sourceType){return sourceType ==typeof(string)|| TypeDescriptor.GetConverter(typeof(Enum)).CanConvertFrom(context, sourceType);}publicoverrideobject ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value){if(value isstring)return GetEnumValue(EnumType, (string) value);if(value isEnum)return GetEnumDescription((Enum) value);returnbase.ConvertFrom(context, culture, value);}publicoverrideobject ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType){return value isEnum&& destinationType ==typeof(string)? GetEnumDescription((Enum)value):(value isstring&& destinationType ==typeof(string)? GetEnumDescription(EnumType, (string)value):base.ConvertTo(context, culture, value, destinationType));}publicstaticstring GetEnumDescription(Enum value){
var fieldInfo = value.GetType().GetField(value.ToString());
var attributes =(DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);return(attributes.Length>0)? attributes[0].Description: value.ToString();}publicstaticstring GetEnumDescription(Type value, string name){
var fieldInfo = value.GetField(name);
var attributes =(DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);return(attributes.Length>0)? attributes[0].Description: name;}publicstaticobject GetEnumValue(Type value, string description){
var fields = value.GetFields();foreach(var fieldInfo in fields){
var attributes =(DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);if(attributes.Length>0&& attributes[0].Description== description)return fieldInfo.GetValue(fieldInfo.Name);if(fieldInfo.Name== description)return fieldInfo.GetValue(fieldInfo.Name);}return description;}}}
Imports System.ComponentModelImports System.GlobalizationNamespace Karmian.Core.TypeConvertersPublicClass EnumDescriptionTypeConverter
Inherits EnumConverter
PublicSubNew(type As Type)MyBase.New(type)EndSubPublicOverridesFunction CanConvertFrom(context As ITypeDescriptorContext, sourceType As Type)AsBooleanReturn sourceType IsGetType(String) OrElse TypeDescriptor.GetConverter(GetType([Enum])).CanConvertFrom(context, sourceType)EndFunctionPublicOverridesFunction ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value AsObject)AsObjectIf TypeOf value IsStringThenReturn GetEnumValue(EnumType, DirectCast(value, String))EndIfIf TypeOf value Is[Enum]ThenReturn GetEnumDescription(DirectCast(value, [Enum]))EndIfReturnMyBase.ConvertFrom(context, culture, value)EndFunctionPublicOverridesFunction ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value AsObject, destinationType As Type)AsObjectReturnIf(TypeOf value Is[Enum]AndAlso destinationType IsGetType(String), GetEnumDescription(DirectCast(value, [Enum])), (If(TypeOf value IsStringAndAlso destinationType IsGetType(String), GetEnumDescription(EnumType, DirectCast(value, String)), MyBase.ConvertTo(context, culture, value, destinationType))))EndFunctionPublicSharedFunction GetEnumDescription(value As[Enum])AsStringDim fieldInfo = value.[GetType]().GetField(value.ToString())Dim attributes = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())ReturnIf((attributes.Length > 0), attributes(0).Description, value.ToString())EndFunctionPublicSharedFunction GetEnumDescription(value As Type, name AsString)AsStringDim fieldInfo = value.GetField(name)Dim attributes = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())ReturnIf((attributes.Length > 0), attributes(0).Description, name)EndFunctionPublicSharedFunction GetEnumValue(value As Type, description AsString)AsObjectDim fields = value.GetFields()ForEach fieldInfo As FieldInfo In fields
Dim attributes = DirectCast(fieldInfo.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())If attributes.Length > 0AndAlso attributes(0).Description= description ThenReturn fieldInfo.GetValue(fieldInfo.Name)EndIfIf fieldInfo.Name= description ThenReturn fieldInfo.GetValue(fieldInfo.Name)EndIfNextReturn description
EndFunctionEndClassEndNamespace
We would like to cover the costs of maintaining the server for longer than a year.
We would like to extend our development environment with some commercial products.
We would like to get rid of the ads from our site.
We would like to take over Karmian.com in order to unify Karmian.
But most of all - we want to provide more free solutions, therefore consider pressing the button below as the absolute expression of your own will and... mood. ;)