I'll try to explain what I want to do:
If I have this class:
public class MyClass
{
public string foo { get; set; }
public string bar { get; set; }
public string age { get; set; }
}
And I instantiate three classes in three different objects like this:
void Main()
{
var myA = new MyClass() { foo = "foo", bar = "bar", age = "age" };
var myB = new MyClass() { foo = "foo", bar = "change" };
var myC = new MyClass() { foo = "xxx", bar = "yyy", age = "zzz" };
//I want myC with this values: foo = "xxx", bar = "change", age = "zzz"
}
So that I want that only the different not null properties from myA
comparing myB
be copied to myC
. Only myB.bar
is a not null different property comparing myA.bar
and this should be the only change copied to myC.bar
How I should I do that? Using Automapper? Or maybe using System.Reflection? Which is the easiest and best practice to do this?
EDIT I'm now using a modified version of this solution: Apply properties values from one object to another of the same type automatically?
Passing the myC
object like a parameter but I'm guessing if this is the best solution