System.convert to custom object

we have our own object of the "Money" data type, which is used to represent monetary values ​​in our application. at the moment we are trying to implement some custom formatting in the grid component, however, the exception "InvalidCastException" arises from System.Convert.

text from the exception:

System.InvalidCastException occurred
  Message="Invalid cast from 'System.Int32' to 'System.Money'."
  Source="mscorlib"
  StackTrace: at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
  InnerException:

note that our Money data type implements IConvertible and also has its own TypeConverter.

How can we overcome this exception?

EDIT: This issue is related to the DevExpress XtraGrid Formatting Conditions, which are evaluated on the column associated with our custom Money data type. The Money type can be attached to the column without problems, it raises only an InvalidCastException when a format condition is evaluated in the column, i.e. GridColumnValue> 0

+3
source share
1 answer

It seems that the problem is that here the value intis int not convertible to Money , since it does not know about it (but Moneycan be convertible to int).

(), , Convert.ChangeType - .

, , int, , /t string, TypeConverter ( PropertyDescriptor.Converter). , TypeConverter, ConvertFrom.

, # , (implicit/explicit).

+2

Source: https://habr.com/ru/post/1727188/


All Articles