C# implicit conversions
Posted
by Chris Boden
on Stack Overflow
See other posts from Stack Overflow
or by Chris Boden
Published on 2010-05-05T19:22:15Z
Indexed on
2010/05/05
19:28 UTC
Read the original article
Hit count: 282
c#
|implicit-conversion
Hello,
I'm currently working on an application where I need to load data from an SQL database and then assign the retrieved values into the properties of an object. I'm doing this by using reflection since the property names and column names are the same. However, many of the properties are using a custom struct type that is basically a currency wrapper for the decimal type. I have defined an implicit conversion in my struct:
public static implicit operator Currency(decimal d)
{
return new Currency(d);
}
This works fine when I use it in code. However, when I have this:
foreach (PropertyInfo p in props)
{
p.SetValue(this, table.Rows[0][p.Name], null);
}
It throws an ArgumentException stating that it cannot convert from System.Decimal to Currency. I'm confused since it works fine in any other circumstance.
© Stack Overflow or respective owner