PropertyInfo GetValue()对象与目标类型不匹配 [英] PropertyInfo GetValue() Object does not match target type
本文介绍了PropertyInfo GetValue()对象与目标类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读取T类型的值
public virtual ActionResult Edit(TEditDTO editedDTO)
{
if (!ModelState.IsValid) return View(editedDTO);
var t = editedDTO.GetType();
var prop = t.GetProperty("Id") ;
var Id = prop.GetValue(t); // get exception
}
但是得到
对象与目标类型不匹配
Object does not match target type
推荐答案
您应该将 TEditDTO
的实例传递给 GetValue
方法,而不是类型实例.
You should pass the instance of TEditDTO
to GetValue
method not the type instance.
var Id = prop.GetValue(editedDTO);
这篇关于PropertyInfo GetValue()对象与目标类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文