Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Implementing Enum Converter Problem!!!

Implementing Enum Converter Problem!!!

Scheduled Pinned Locked Moved ASP.NET
helpcsharpcssvisual-studiodesign
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    e laj
    wrote on last edited by
    #1

    (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 */

    M 1 Reply Last reply
    0
    • E e laj

      (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 */

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, In the ConvertTo method, you should return an InstanceDescriptor object when the destination type is the InstanceDescriptor. The InstanceDescriptor object then helps the page parser create the value for the Letter 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);
      

      }

      E 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, In the ConvertTo method, you should return an InstanceDescriptor object when the destination type is the InstanceDescriptor. The InstanceDescriptor object then helps the page parser create the value for the Letter 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);
        

        }

        E Offline
        E Offline
        e laj
        wrote on last edited by
        #3

        Thank you very much!! It's been done. Thank again MAN:-)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups