Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 映射两个类的属性和值Linq_C#_.net_Linq - Fatal编程技术网

C# 映射两个类的属性和值Linq

C# 映射两个类的属性和值Linq,c#,.net,linq,C#,.net,Linq,我正在使用linq映射两个相似的对象,但我遇到了一个问题,它没有映射子类,甚至在GetProperties()中也看不到它们。子类被标记为public,所以我有点困惑为什么这个代码不起作用。。。关于使用其他方法的任何想法或建议。提前谢谢你的帮助 foreach (PropertyInfo pInfo in _WorkRequest.GetType().GetProperties()) { _WorkRequestV1.GetType().GetProperty(pInfo.Name).Set

我正在使用linq映射两个相似的对象,但我遇到了一个问题,它没有映射子类,甚至在GetProperties()中也看不到它们。子类被标记为public,所以我有点困惑为什么这个代码不起作用。。。关于使用其他方法的任何想法或建议。提前谢谢你的帮助

foreach (PropertyInfo pInfo in _WorkRequest.GetType().GetProperties()) 
{
 _WorkRequestV1.GetType().GetProperty(pInfo.Name).SetValue(_WorkRequestV1, pInfo.GetValue(_WorkRequest, null), null);
}
更新

在研究了这一点之后,我注意到在类ex中声明子类时:

public Person myPerson; 
GetProperties()不查看Person类,但如果我添加

public Person myPerson {get;set;}
GetProperties()是否查看myPerson

最后,我想补充一点

public Person myPerson = new Person()
GetProperties()没有看到此人


为什么需要{get;set}?

我无法从您的代码中看出为什么这不起作用,但有一种更好的方法可以将一个对象的属性复制到另一个对象,那就是使用

你可以做:

Mapper.CreateMap<WorkRequest, WorkRequestV1>();
Mapper.Map(_WorkRequest, _WorkRequestV1);
Mapper.CreateMap();
Map(_WorkRequest,_WorkRequestV1);

感谢您的快速回复。我曾尝试使用AutoMapper,但在尝试映射这两个对象时出现异常。我已经使这些类完全相同,但仍然得到一个错误。这可能是由于子类的原因吗?抛出了AutoMapper.automappingexception。那么,
public Person myPerson
不是一个属性,而是一个字段
PublicPerson myPerson{get;set;}
是一个属性。在
public Person myPerson=new Person()
之后如何执行
GetProperties
?@GertArnold你是什么意思我如何执行GetProperties我做的是这个列表myList=\u WorkRequest.GetType().GetProperties().ToList();在我的_WorkRequest类中,Person myPerson=new Person是一个属性。您应该显示更多代码。如果Person是WorkRequest类的公共属性,则它应该显示在GetProperties()中。