开发者

How to copy data from different classes by matching field or property names

开发者 https://www.devze.com 2023-01-16 08:25 出处:网络
I am looking to find a way to take two objects that have identical properties and make a call to copy the property values from one object into the other. The the example below assume I have an instanc

I am looking to find a way to take two objects that have identical properties and make a call to copy the property values from one object into the other. The the example below assume I have an instance of A and I want to use the data of that instance to hydrate a new instance or C (to keep things terse I used fields instead of properties in the example below)

public class A : B
{
    public string prop1;
    public int prop2;
}

public class B
{
    public byte propX;
    public float propY;
}

public class C
{
    public byte propX;
    public float propY;
    public string prop1开发者_开发技巧;
    public int prop2;
}

public class Merger
{
    public static object Merge(object copyFrom, object copyTo)
    { 
        //do some work
        //maybe <T> generically refactor?
    }
}

The merger class is just a psuedo-example, doing this through generics would be optimal but the first thing I question is whether such a capability already exists. I could imagine using reflection to do this myself but just wanted to toss it out for better ideas first.

Real world context: This is actually an MVVM related issue as I am trying to use disparate classes coming back from EF to populate a ViewModel instance.


Check out tools and libraries like AutoMapper - those would handle cases like this with ease - and much more! No need to re-invent the wheel - just use the tool! :-)

You would basically define a map between classes A and C like this:

Mapper.CreateMap<A, C>();

and then later on, you can have AutoMapper do the mapping, based on that map, from an instance of A into an instance of C, something like this:

C yourC = Mapper.Map<A, C>(instanceOfA);

AutoMapper does a default mapping based on property names (and types), but you can extend and influence it in a great many ways to include mappings from one property to another, even if the names (or types) don't match 100%. It's quite flexible and well established - definitely worth a serious look!


using System;
using System.Linq;
using System.Reflection;

public class Merger
{
    public static TTarget Merge<TTarget>(object copyFrom) where TTarget : new()
    {
        var flags = BindingFlags.Instance | BindingFlags.Public |
                    BindingFlags.NonPublic;
        var targetDic = typeof(TTarget).GetFields(flags)
                                       .ToDictionary(f => f.Name);
        var ret = new TTarget();
        foreach (var f in copyFrom.GetType().GetFields(flags))
        {
            if (targetDic.ContainsKey(f.Name))
                targetDic[f.Name].SetValue(ret, f.GetValue(copyFrom));
            else
                throw new InvalidOperationException(string.Format(
                    "The field “{0}” has no corresponding field in the type “{1}”.",
                    f.Name, typeof(TTarget).FullName));
        }
        return ret;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var a = new A { prop1 = "one", prop2 = 2, propX = 127, propY = 0.47f };
        var c = Merger.Merge<C>(a);
        Console.WriteLine(c.prop1);  // prints one
        Console.WriteLine(c.prop2);  // prints 2
        Console.WriteLine(c.propX);  // prints 127
        Console.WriteLine(c.propY);  // prints 0.47
    }
}


This isn't the best solution by far, but based on the object graph you've provided, you could probably accomplish this by XML serializing the first object, and XML deserializing the XML stream into the second object.

Your proposed Merger method could possibly look like this:

public class Merger
{
    public static object Merge(object copyFrom, object copyTo)
    { 
        var xmlContent = MyXMLSerializationMethod(copyFrom);
        MyXMLDeserializationMethod(xmlContent, typeof(copyTo), out copyTo);
        return copyTo;
    }
}


Good post about using AutoMapper to solve this problem in the context of MVVM and MVC

http://www.bengtbe.com/blog/post/2009/04/14/Using-AutoMapper-to-map-view-models-in-ASPNET-MVC.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消