我目前正在开发一个应用程序,我需要从SQL数据库加载数据,然后将检索到的值分配给对象的属性.我这样做是通过使用反射,因为属性名称和列名称是相同的.但是,许多属性都使用自定义结构类型,它基本上是十进制类型的货币包装器.我在我的struct中定义了一个隐式转换:
public static implicit operator Currency(decimal d)
{
return new Currency(d);
}
Run Code Online (Sandbox Code Playgroud)
我在代码中使用它时工作正常.但是,当我有这个:
foreach (PropertyInfo p in props)
{
p.SetValue(this, table.Rows[0][p.Name], null);
}
Run Code Online (Sandbox Code Playgroud)
它抛出一个ArgumentException,声明它无法从System.Decimal转换为Currency.我很困惑,因为它在任何其他情况下都能正常工作.