How to automatically map the values between instances of two different classes if both have same properties?

后端 未结 5 1840
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 11:10

I have two classes with exactly same members (properties and fields) with same datatypes. I want to map the members from one to other in automated way. I know there are more

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 11:50

    There are a number of tools that do this. Look at the "TranslateTo" routine in service stack as an example. They have excellent auto mapping (https://github.com/ServiceStack/ServiceStack/wiki/Auto-mapping).

    Using this, all you would have to do is:

     obj2 = obj1.TranslateTo();
    

    Simple and elegant!

    In case you are interested a few other references to similar topics:

    • https://stackoverflow.com/questions/286294/object-to-object-mapper
    • Best Practices for mapping one object to another
    • http://www.softwarerockstar.com/2011/05/complex-object-mapping-using-automapper/

提交回复
热议问题