我得到错误Object does not match target type
。我明白为什么我会出错。但是我正在将double
分配给double
。我不知道为什么我会在下面的代码中出现错误。
我遇到问题的代码:
...
var specTran = new SpecTran(gapRecord);
foreach (var result in yields._predictions)
{
var type = specTran.GetType(); // --> {Name = "SpecTran" FullName = "GAP.Models.Database.SpecTran"}
var prop = type.GetProperty(result.Key); // --> {Double DexaCWT}
if (prop == null)
continue;
prop.SetValue(
(double)result.Value, // --> {[DexaCWT, 0]}
specTran);
}
预测结果类别:
PredictionResult {
...
public Dictionary<string, double> _predictions { get; set; }
}
您收到错误是因为根据文档,SetValue
的参数是向后的。
应该是:
prop.SetValue(
specTran,
(double)result.Value); // --> {[DexaCWT, 0]}