Implementing Enum Converter Problem!!!
-
(Sorry if my english is little bad) Well, i search all over the internet (maybe less) but found no solution to this problem. The problem is: I implementd an EnumConverter in order to display friendly names in the designer. All works fine. Even debuging the converter in design time works very well (using two instances of VS.net). The problem is in the running mode: I get the following exception: -----Error------------------------------------------------------------- 'LetterConverter' is unable to convert 'TestEnum.Letter' to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NotSupportedException: 'LetterConverter' is unable to convert 'TestEnum.Letter' to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'. -----End error---------------------------------------------------------- Here is the code for the enum:
public enum Letter { a, b, c }
Here is the converter:public class LetterConverter : EnumConverter { public LetterConverter(): base(typeof(Letter)) { } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if(value is string) { string str = (string)value; if(str == "The 'a' letter") //if(str == "a") // this works { return Letter.a; } else if(str == "The 'b' letter") //else if(str == "b") // this works { return Letter.b; } else if(str == "The 'c' letter") //else if(str == "c") // this works { return Letter.c; } } return base.ConvertFrom (context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if(destinationType == typeof(string)) { switch((Letter)value) { case Letter.a: //return "a"; // this works return "The 'a' letter"; case Letter.b: //return "b"; // this works return "The 'b' letter"; case Letter.c: //return "c"; // this works return "The 'c' letter"; } } if(destinationType == typeof(TypeDescriptor)) { /* To solve the problem - i have tried to return TypeDescriptor - however, enum is a value type - and hence this couldn't be done */
-
(Sorry if my english is little bad) Well, i search all over the internet (maybe less) but found no solution to this problem. The problem is: I implementd an EnumConverter in order to display friendly names in the designer. All works fine. Even debuging the converter in design time works very well (using two instances of VS.net). The problem is in the running mode: I get the following exception: -----Error------------------------------------------------------------- 'LetterConverter' is unable to convert 'TestEnum.Letter' to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NotSupportedException: 'LetterConverter' is unable to convert 'TestEnum.Letter' to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'. -----End error---------------------------------------------------------- Here is the code for the enum:
public enum Letter { a, b, c }
Here is the converter:public class LetterConverter : EnumConverter { public LetterConverter(): base(typeof(Letter)) { } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if(value is string) { string str = (string)value; if(str == "The 'a' letter") //if(str == "a") // this works { return Letter.a; } else if(str == "The 'b' letter") //else if(str == "b") // this works { return Letter.b; } else if(str == "The 'c' letter") //else if(str == "c") // this works { return Letter.c; } } return base.ConvertFrom (context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if(destinationType == typeof(string)) { switch((Letter)value) { case Letter.a: //return "a"; // this works return "The 'a' letter"; case Letter.b: //return "b"; // this works return "The 'b' letter"; case Letter.c: //return "c"; // this works return "The 'c' letter"; } } if(destinationType == typeof(TypeDescriptor)) { /* To solve the problem - i have tried to return TypeDescriptor - however, enum is a value type - and hence this couldn't be done */
Hi there, In the
ConvertTo
method, you should return anInstanceDescriptor
object when the destination type is theInstanceDescriptor
. TheInstanceDescriptor
object then helps the page parser create the value for theLetter
enum. The sample code looks something like:public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
object value, Type destinationType)
{
if(destinationType == typeof(string))
{
switch((Letter)value)
{
case Letter.a:
//return "a"; // this works
return "The 'a' letter";case Letter.b: //return "b"; // this works return "The 'b' letter"; case Letter.c: //return "c"; // this works return "The 'c' letter"; } } if ((destinationType == typeof(InstanceDescriptor)) && (value != null)) { FieldInfo fieldInfo = this.EnumType.GetField(value.ToString()); if (fieldInfo != null) { return new InstanceDescriptor(fieldInfo, null); } } return base.ConvertTo(context, culture, value, destinationType);
}
-
Hi there, In the
ConvertTo
method, you should return anInstanceDescriptor
object when the destination type is theInstanceDescriptor
. TheInstanceDescriptor
object then helps the page parser create the value for theLetter
enum. The sample code looks something like:public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
object value, Type destinationType)
{
if(destinationType == typeof(string))
{
switch((Letter)value)
{
case Letter.a:
//return "a"; // this works
return "The 'a' letter";case Letter.b: //return "b"; // this works return "The 'b' letter"; case Letter.c: //return "c"; // this works return "The 'c' letter"; } } if ((destinationType == typeof(InstanceDescriptor)) && (value != null)) { FieldInfo fieldInfo = this.EnumType.GetField(value.ToString()); if (fieldInfo != null) { return new InstanceDescriptor(fieldInfo, null); } } return base.ConvertTo(context, culture, value, destinationType);
}