merging two objects in C#

后端 未结 6 1883
無奈伤痛
無奈伤痛 2020-11-29 07:06

I have an object model MyObject with various properties. At one point, I have two instances of these MyObject: instance A and instance B. I\'d like to copy and replace the p

6条回答
  •  离开以前
    2020-11-29 07:54

    I have tried Merge Two Objects into an Anonymous Type by Kyle Finley and it is working perfect.

    With the TypeMerger the merging is as simple as

    var obj1 = new {foo = "foo"};
    
    var obj2 = new {bar = "bar"};
    
    var mergedObject = TypeMerger.MergeTypes(obj1 , obj2 );
    

    That's it you got the merged object, apart from that, there is a provision to ignore specific properties too. You can use the same thing for MVC3 too.

提交回复
热议问题