0

I am using linq to map two similar objects, but I am having an issue that it does not map subclasses it does not even see them in the GetProperties(). The sub classes are marked as public so I am a little confused why this code is not working... Any ideas or suggestions on using the other method/s. Thanks for your help in advance.

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

UPDATE

After looking into this I have noticed that when declaring a subclass within a class ex:

public Person myPerson; 

GetProperties() does not see Person class but if I add

public Person myPerson {get;set;}

GetProperties() does see myPerson

Lastly if I add

public Person myPerson = new Person()

GetProperties() does not see the person.

How come it required {get;set}?

4

1 回答 1

1

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

你可以做:

Mapper.CreateMap<WorkRequest, WorkRequestV1>();
Mapper.Map(_WorkRequest, _WorkRequestV1);
于 2013-03-13T18:46:53.717 回答