Bug with NullableConverter ?

When attempting to convert Decimal to Decimal  at runtime

Type targetType = typeof(decimal );

decimal newPropertyValue = 2.3;

--------------------------------------

                    NullableConverter nullConverter = new NullableConverter(targetType);< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

                    object nonNullableValue = nullConverter.UnderlyingTypeConverter.ConvertTo(newPropertyValue, nullConverter.UnderlyingType);

 

System.Exception: System.NotSupportedException: 'DecimalConverter' is unable to convert 'System.Decimal' to 'System.Decimal'.

   at System.ComponentModel.TypeConverter.GetConvertToException(Object value, Type destinationType)

   at System.ComponentModel.TypeConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType)

   at System.ComponentModel.BaseNumberConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType)

   at System.ComponentModel.DecimalConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType)

   at System.ComponentModel.TypeConverter.ConvertTo(Object value, Type destinationType)




Answer this question

Bug with NullableConverter ?

  • Sheena192783

    I think that it's becase in .NET 2.0 final release they rewrote the Nullable types and now they convert every Nullable with a value different then Null to it's primitive type (hence (decimal ) 5 = (decimal) 5).

  • GCF_Pick2

    That's all good but it still doesn't solve the isssue stated above. Please help.



  • David Roberto Johnson

    Thats because Int32Converter doesn't support converting to an InstanceDescriptor. InstanceDescriptors are normally used when you need to choose a specific constructor or method of instantiating the object. Int32s are pretty simple so they don't convert to InstanceDescriptors.



  • mkfl

    Now i get the problem..

    i’m playing here with a sample.. but i got a new question...

    why this :

    object d = (decimal )2;

    returns a object containing a decimal and not a "decimal "

    do u understand it


  • Stormblade

    Thanks, but this doesnt work. I get:

    'Int32Converter' is unable to convert 'System.Int32' to 'System.ComponentModel.Design.Serialization.InstanceDescriptor'.



  • jagilbert

    I'm writing a generic base class for a ORM (NHibernate like) framework and I need to be able to convert data comming from the database (DataTables) to .NET class properties which are not known at design time. We are using reflection to discover and map the class properties.

    The important thing here to note is that this worked fine in the 2.0 Beta but the INullable interface and Nullable.Wrap and Nullable.Unwrap methods were removed in the final release which broke our code and we can't even compile the project with the final 2.0 release.



  • crim

    Hi,

    excuse-me if i don’t get it.. but why can’t you use a simple cast

    like that : (decimal )dValue


  • Mark Bennion

    This actually has nothing to do with NullableConverter.  DecimalConverter does not support Decimal as a target type (since its the same as the source type).

    Instead, try using typeof(InstanceDescriptor) as in:

    object nonNullableValue = nullConverter.UnderlyingTypeConverter.ConvertTo(newPropertyValue, typeof(InstanceDescriptor));

    and it should work.

    In general, TypeConverters do not support "no-op" operations.  Meaning they are usually intended to convert from and to String and InstanceDescriptor.

     



  • Bug with NullableConverter ?