PropertyInfo.SetValue对象不匹配目标类型

编程入门 行业动态 更新时间:2024-10-28 16:18:05
本文介绍了PropertyInfo.SetValue对象不匹配目标类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个类似的错误的这个,但遗憾的是不一样的简单解决方案。下面是代码:

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对象不匹配目标类型

本文发布于:2023-11-03 07:02:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554515.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不匹配   对象   目标   类型   PropertyInfo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!