Try our conversational search powered by Generative AI!

Class ResourceEnumConverter

Defines a type converter for enum values that converts enum values to and from string representations using resources

Inheritance
System.Object
ResourceEnumConverter
Namespace: Mediachase.Commerce.Shared
Assembly: Mediachase.Commerce.dll
Version: 13.30.0
Syntax
public class ResourceEnumConverter : EnumConverter
Remarks

This class makes localization of display values for enums in a project easy. Simply derive a class from this class and pass the ResourceManagerin the constructor.

class LocalizedEnumConverter : ResourceEnumConverter
{
   public LocalizedEnumConverter(Type type)
       : base(type, Properties.Resources.ResourceManager)
   {
   }
}    
Public Class LocalizedEnumConverter

   Inherits ResourceEnumConverter
   Public Sub New(ByVal sType as Type)
       MyBase.New(sType, My.Resources.ResourceManager)
   End Sub
End Class    

Then define the enum values in the resource editor. The names of the resources are simply the enum value prefixed by the enum type name with an underscore separator eg MyEnum_MyValue. You can then use the TypeConverter attribute to make the LocalizedEnumConverter the default TypeConverter for the enums in your project.

Constructors

ResourceEnumConverter(Type)

Create a new instance of the converter using translations from the given resource manager

Declaration
public ResourceEnumConverter(Type type)
Parameters
Type Name Description
System.Type type

Methods

ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

Convert string values to enum values

Declaration
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
Parameters
Type Name Description
System.ComponentModel.ITypeDescriptorContext context
System.Globalization.CultureInfo culture
System.Object value
Returns
Type Description
System.Object

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

Convert the enum value to a string

Declaration
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
Parameters
Type Name Description
System.ComponentModel.ITypeDescriptorContext context
System.Globalization.CultureInfo culture
System.Object value
System.Type destinationType
Returns
Type Description
System.Object

ConvertToString(Enum)

Convert the given enum value to string using the registered type converter

Declaration
public static string ConvertToString(Enum value)
Parameters
Type Name Description
System.Enum value

The enum value to convert to string

Returns
Type Description
System.String

The localized string value for the enum

GetValues(Type)

Return a list of the enum values and their associated display text for the given enum type in the current UI Culture

Declaration
public static List<KeyValuePair<Enum, string>> GetValues(Type enumType)
Parameters
Type Name Description
System.Type enumType

The enum type to get the values for

Returns
Type Description
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<System.Enum, System.String>>

A list of KeyValuePairs where the key is the enum value and the value is the text to display

Remarks

This method can be used to provide localized binding to enums in ASP.NET applications. Unlike windows forms the standard ASP.NET controls do not use TypeConverters to convert from enum values to the displayed text. You can bind an ASP.NET control to the list returned by this method by setting the DataValueField to "Key" and theDataTextField to "Value".

GetValues(Type, CultureInfo)

Return a list of the enum values and their associated display text for the given enum type

Declaration
public static List<KeyValuePair<Enum, string>> GetValues(Type enumType, CultureInfo culture)
Parameters
Type Name Description
System.Type enumType

The enum type to get the values for

System.Globalization.CultureInfo culture

The culture to get the text for

Returns
Type Description
System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<System.Enum, System.String>>

A list of KeyValuePairs where the key is the enum value and the value is the text to display

Remarks

This method can be used to provide localized binding to enums in ASP.NET applications. Unlike windows forms the standard ASP.NET controls do not use TypeConverters to convert from enum values to the displayed text. You can bind an ASP.NET control to the list returned by this method by setting the DataValueField to "Key" and theDataTextField to "Value".