S
shapper
Hello,
I am creating a way to convert en enum to a friendly and localized
string.
I am using the following Type Converter:
public class MyEnumToString : TypeConverter {
// CanConvertFrom
public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
return typeof(MyEnum).IsAssignableFrom(sourceType);
} // CanConvertFrom
// CanConvertTo
public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
return destinationType == typeof(String);
} // CanConvertTo
// ConvertTo
public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
MyEnum myEnum = (MyEnum)value;
switch (myEnum) {
case MyEnum.AAA:
return "This is AAA description";
case MyEnum.BBB:
return "This is BBB description";
default:
return String.Empty;
}
} // ConvertTo
} // MyEnumToString
And a String extension named ToStringType:
public static class StringExtensions {
public static string ToStringType<T>(this T o) {
var converter = System.ComponentModel.TypeDescriptor.GetConverter
(o);
if (o != null) return converter.ConvertToString(o);
return Convert.ToString(o);
}
}
Then I use, for example:
String aaa = MyEnum.AAA.ToStringType();
And aaa will become "This is AAA description".
This is working. My question is:
- Is this the best way to implement this?
- And is there a way to use the default ToString method?
(I am able to use String aaa = MyEnum.AAA.ToString() but I get
"AAA").
I have use Type converters to convert a List<T> to a CSV String. But
not enums.
Thanks,
Miguel
I am creating a way to convert en enum to a friendly and localized
string.
I am using the following Type Converter:
public class MyEnumToString : TypeConverter {
// CanConvertFrom
public override Boolean CanConvertFrom(ITypeDescriptorContext
context, Type sourceType) {
return typeof(MyEnum).IsAssignableFrom(sourceType);
} // CanConvertFrom
// CanConvertTo
public override Boolean CanConvertTo(ITypeDescriptorContext
context, Type destinationType) {
return destinationType == typeof(String);
} // CanConvertTo
// ConvertTo
public override Object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, Object value, Type destinationType) {
MyEnum myEnum = (MyEnum)value;
switch (myEnum) {
case MyEnum.AAA:
return "This is AAA description";
case MyEnum.BBB:
return "This is BBB description";
default:
return String.Empty;
}
} // ConvertTo
} // MyEnumToString
And a String extension named ToStringType:
public static class StringExtensions {
public static string ToStringType<T>(this T o) {
var converter = System.ComponentModel.TypeDescriptor.GetConverter
(o);
if (o != null) return converter.ConvertToString(o);
return Convert.ToString(o);
}
}
Then I use, for example:
String aaa = MyEnum.AAA.ToStringType();
And aaa will become "This is AAA description".
This is working. My question is:
- Is this the best way to implement this?
- And is there a way to use the default ToString method?
(I am able to use String aaa = MyEnum.AAA.ToString() but I get
"AAA").
I have use Type converters to convert a List<T> to a CSV String. But
not enums.
Thanks,
Miguel