How to deep copy between objects of different types in C#.NET

前端 未结 7 1256
孤街浪徒
孤街浪徒 2020-12-06 01:55

I have a requirement to map all of the field values and child collections between ObjectV1 and ObjectV2 by field name. ObjectV2 is in a different namspace to ObjectV1.

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 02:36

    If if you control the instantiation of the destination object, try using JavaScriptSerializer. It doesn't spit out any type information.

    new JavaScriptSerializer().Serialize(new NamespaceA.Person{Id = 1, Name = "A"})
    

    returns

    {Id: 1, Name: "A"}
    

    From this it should possible to deserialize any class with the same property names.

提交回复
热议问题