CLSA 3.6.2 - Type Coercion from Int16 to Nullable<Enum> is not supported?

CLSA 3.6.2 - Type Coercion from Int16 to Nullable<Enum> is not supported?

Old forum URL: forums.lhotka.net/forums/t/6725.aspx


rfcdejong posted on Thursday, April 02, 2009

I have an enumeration defined like:

public enum MyColor
{
      red = 1,
      white = 2,
      blue = 3
}

From my datareader i have an value as valuetype System.Int16
In my businessobject i have a property Nullable<MyColor>

Using LoadProperty(), Doing TypeCoerce, it can't convert to the desired type.

Why?

Is this not supported or a missing Coercion?
I traced the code and at cnv.CanConvertFrom(valueType) it steps into the else statement.

cnv = "System.ComponentModel.EnumConverter"
desiredType = "MyNameSpace.MyColor"
valueType = "System.Int16"
value = "1"

catch
{
      
TypeConverter cnv = TypeDescriptor.GetConverter(desiredType);
      
if (cnv != null && cnv.CanConvertFrom(valueType))
            
return cnv.ConvertFrom(value);
      
else
            
throw;
}

I really don't know and i must use the type coercion since it's my generic mapper calling LoadProperty without knowing which type a property is.
I've no other option then converting it myself first before calling LoadProperty.

Why can't the EnumConverter convert from Int16 ?

rfcdejong replied on Thursday, April 02, 2009

Ok i know why:
http://msdn.microsoft.com/en-us/library/at9d6zbw.aspx

"This converter can only convert an enumeration object to and from a string."


In this case i want to ask if the type coerce functionality can be extended with support to convert from int to enum, please.

RockfordLhotka replied on Thursday, April 02, 2009

I'll add this to the wish list.

Copyright (c) Marimer LLC