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?
Check out my blog and tell me what I've done wrong :)
Please mark helpful posts as answers. Points are tasty.
Please do not PM me with questions, as I may miss them - use the forums.
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
}
}
}
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.
Spikeh
Participant
1344 Points
273 Posts
Merging two different objects
May 19, 2011 08:38 AM|LINK
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:
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?
Please mark helpful posts as answers. Points are tasty.
Please do not PM me with questions, as I may miss them - use the forums.
raduenuca
All-Star
24675 Points
4250 Posts
Re: Merging two different objects
May 19, 2011 09:33 AM|LINK
http://automapper.codeplex.com/
Radu Enuca | Blog
Spikeh
Participant
1344 Points
273 Posts
Re: Merging two different objects
May 19, 2011 09:48 AM|LINK
As I said, I was wondering if there was an already built in method in C#. I'm aware of AutoMapper's existance...
Please mark helpful posts as answers. Points are tasty.
Please do not PM me with questions, as I may miss them - use the forums.
shashankgwl
All-Star
18926 Points
3662 Posts
Re: Merging two different objects
May 19, 2011 09:49 AM|LINK
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 } } }All is well if it runs well.
blog
Spikeh
Participant
1344 Points
273 Posts
Re: Merging two different objects
May 19, 2011 11:29 AM|LINK
That's exactly what I wasn't looking for ;) I don't want to use polymorphism - I need complete abstraction from the source class.
Please mark helpful posts as answers. Points are tasty.
Please do not PM me with questions, as I may miss them - use the forums.
shashankgwl
All-Star
18926 Points
3662 Posts
Re: Merging two different objects
May 19, 2011 11:40 AM|LINK
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.
All is well if it runs well.
blog
naveenkumar1...
Member
2 Points
1 Post
Re: Merging two different objects
Apr 21, 2012 05:36 PM|LINK
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.
abiruban
All-Star
16072 Points
2736 Posts
Re: Merging two different objects
Apr 22, 2012 09:17 AM|LINK
Hai
http://stackoverflow.com/questions/3850913/combine-two-objects-into-one
***DON'T FORGET TO CLICK “MARK AS ANSWER” ON THE POST IF HELPED YOU.