我有一个类似的错误的这个,但遗憾的是不一样的简单解决方案。下面是代码:
I've a similar error to this one, but unfortunately not the same simple solution. Here is the code:
public virtual void MapObject(T obj, IViewModel<T> viewModel, ITPSDataAccess dataAccess) { var objProps = obj.GetType().GetProperties(); var dtoProps = viewModel.GetType().GetProperties(); foreach (var dtoProp in dtoProps) { var objProp = dtoProps.SingleOrDefault(x => x.Name == dtoProp.Name); if (objProp != null) { var dtoVal = dtoProp.GetValue(viewModel, null); objProp.SetValue(obj, dtoVal, null); // ERROR HERE } } ... }在指定的点时的错误,说明对象不匹配目标类型。它看起来像我将正确的对象设置 - 因此,我不能要解决的问题。
The error occurs at the point indicated, stating "Object does not match target type". It looks like I'm passing the correct object to be set - I am therefore unable to solve the problem.
我也试图采取的这个解决方案,并检查类型物业我试图设置哪些抛出异常 - 在有关财产是一个字符串,并且设置不会出现,因为它工作正常,无反射的情况下被打破。
I've also tried to take the advice of this solution, and examine the types of property I'm attempting to set which throw the exception - the property in question is a string, and the setter does not appear to be broken since it works under normal, non-reflection circumstances.
推荐答案这个错误似乎是在这里:
The mistake seems to be here:
var objProp = dtoProps.SingleOrDefault(x => x.Name == dtoProp.Name);
我觉得你的意思是:
I think you meant:
var objProp = objProps.SingleOrDefault(x => x.Name == dtoProp.Name);更多推荐
PropertyInfo.SetValue对象不匹配目标类型
发布评论