【发布时间】:2011-05-23 03:04:46
【问题描述】:
这行得通:
int valueAsInt = 0;
if (Int32.TryParse(value, out valueAsInt))
{
record.GetType().GetProperty(property).SetValue(record, valueAsInt, null);
}
else
{
record.GetType().GetProperty(property).SetValue(record, value, null);
}
然而,事实并非如此。不知道为什么。
Int32.TryParse(value, out valueAsInt) ? record.GetType().GetProperty(property).SetValue(record, valueAsInt, null) :record.GetType().GetProperty(property).SetValue(record, value, null);
我收到以下错误消息:
错误1 只有赋值、调用、递增、递减和new对象表达式可以作为语句使用
错误 2 无法确定条件表达式的类型,因为 'void' 和 'void' 之间没有隐式转换
我只是好奇为什么三元运算符在这种情况下会失败。谢谢!
【问题讨论】:
标签: c# reflection