Share via


Merging two different objects

Question

Thursday, May 19, 2011 4:38 AM

Hi there,

I have two objects which have almost identical structures (let's call them "source" and "destination" for ease of explaination). I want to be able to copy one object in to the second object without using polymorphism.

"Source" is populated with information, and "destination" is empty, but effectively a copy of "source"'s primitive members. What I need to do is cast the data from "source" in to "destination", keeping the data in the commonly named members; the members that don't exist in "destination" aren't important for this usage. 

This sounds like an ideal candidate for an Interface, but the "destination" object needs to be completely standalone - any shared constructs will break my model.

What I normally do in these instances is basically instantiate the new object and copy the data over manually (made a lot easier in recent years by object initializers), but while I'm developing, this object will change regularly, and there are going to be a lot more objects I need to do this with.

Here's some sample code. First, the objects:

class source {
    public source(int ID) {
        populateFromExternal();
    }
    public int Test;
    public string Test2;
    public void DoSomething();
}

class destination {
    public int Test;
    public string Test2;
}

source.DoSomething() is not important, nor is the constructor, I do not want the functionality in the "destination" class. What I would normally do is the following:

 

var s = new source(1);
var d = new destination() {
Test = s.Test,
Test2 = s.Test2
}

 

Obviously this can get a bit unweildy if there are more members in "source", or if members are renamed etc. Ideally I would like to cast in some way:

 

var s = new source(1);
destination d = (destination)s;

 

But of course, the compiler won't allow this as there's no common interface or base class.

Does this methodology have a name, and is there any reliable way of implementing it? I imagine Reflection is the way to go, but I wanted to check if c# already had a method of doing this?

All replies (7)

Thursday, May 19, 2011 5:33 AM

http://automapper.codeplex.com/


Thursday, May 19, 2011 5:48 AM

As I said, I was wondering if there was an already built in method in C#. I'm aware of AutoMapper's existance...


Thursday, May 19, 2011 5:49 AM

I don't know if this is what you are after, but i've tried to mimic something similar

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = new source(1);

            var d = new destination
            {
                Test = s.Test,
                Test2 = s.Test2
            };


            s = new source(1);
            d = (destination)s;

        }
    }

    public class source
    {
        public source()
        {

        }
        public source(int ID)
        {
            //populateFromExternal();
        }
        public int Test;
        public string Test2;
        public void DoSomething()
        { 

        }

    }

    public class destination : source
    {
        public int Test;
        public string Test2;

        public destination(int x):base(x)
        {

        }

        public destination()
        {
            // TODO: Complete member initialization
        }
    }
}

Thursday, May 19, 2011 7:29 AM

That's exactly what I wasn't looking for ;) I don't want to use polymorphism - I need complete abstraction from the source class.


Thursday, May 19, 2011 7:40 AM

Well, I really can't think about an alternative of OOP here, exceptly for stupidly introducing an extension method for object class (given that suorce class ultimately comes from object) and returning ano;tehr object.


Saturday, April 21, 2012 1:36 PM

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. 


Sunday, April 22, 2012 5:17 AM

Hai

http://stackoverflow.com/questions/3850913/combine-two-objects-into-one