如果两个属性具有相同的属性,如何在两个不同类的实例之间自动映射值? [英] How to automatically map the values between instances of two different classes if both have same properties?

查看:68
本文介绍了如果两个属性具有相同的属性,如何在两个不同类的实例之间自动映射值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,它们的成员(属性和字段)完全相同,数据类型相同.我想以自动方式将成员从一个映射到另一个.我知道在开发方面要处理更多实用的方法.一种简单的解决方案是手动将每个成员从一个实例映射到另一个实例.但是,我想将其自动化为一些常规解决方案.

I have two classes with exactly same members (properties and fields) with same datatypes. I want to map the members from one to other in automated way. I know there are more practical means development-wise to handle. One simple solution is to manually map each member from one instance to other. But, I want to automate this as some general solution.

假设您具有以下代码:

public MyObject1 AssignTest (MyObject1 obj1, MyObject2 obj2)
{
    //Code here for auto map
}

其中 MyObject1 MyObject2 具有相同的属性和相同的数据类型.我不想遍历并单独分配值(即 MyObject1.Property1 = MyObject2.Property1 等).

Where MyObject1 and MyObject2 have the exact same properties with same datatype. I do not want to go through and assign the values individually (i.e. MyObject1.Property1 = MyObject2.Property1 etc.). Is it possible to assign all the values that have been specified in MyObject1 to MyObject2 automatically?

推荐答案

进行此操作(例如,出于创建自己的自动映射器或了解其基本工作原理的一种可能性)将是使用(如已建议的)反射.代码如下所示:

One possibility to make this (for example for the purpose of creating your own automapper or understand how it basically works) would be to use (as already suggested) Reflection. The code can look like this:

// TODO: error handling
// Test classes
public class A
{
    public string Name { get; set; }
    public int Count;
}

public class B
{
    public string Name { get; set; }
    public int Count;
}
// copy routine
public B CopyAToB(A a)
{
    B b = new B();
    // copy fields
    var typeOfA = a.GetType();
    var typeOfB = b.GetType();
    foreach (var fieldOfA in typeOfA.GetFields())
    {
        var fieldOfB = typeOfB.GetField(fieldOfA.Name);
        fieldOfB.SetValue(b, fieldOfA.GetValue(a));
    }
    // copy properties
    foreach (var propertyOfA in typeOfA.GetProperties())
    {
        var propertyOfB = typeOfB.GetProperty(propertyOfA.Name);
        propertyOfB.SetValue(b, propertyOfA.GetValue(a));
    }

    return b;
}

该函数可以像这样使用:

The function can be used like this:

var a = new A
{
    Name = "a",
    Count = 1
};

var b = CopyAToB(a);
Console.Out.WriteLine(string.Format("{0} - {1}", b.Name, b.Count));

输出为:

a - 1

请注意,反射的使用需要付出一定的代价-它会影响性能.使用反射,您可以访问私有和公共对象成员.例如,从Visual Studio中使用它来创建测试访问器对象,以便访问所有测试对象成员.

Please note, that the usage of reflection comes with a price - it costs performance. Using reflection you can access both private and public object members. This is for example used from Visual Studio to create test accessor objects in order to access all test object members.

请查看现有的自动映射器(请参阅其他答案以获取链接),并使用它们代替您自己重新发明轮子-现有的库已针对速度进行了优化,经过全面测试并且非常使用舒适.这样,您可以将代码中的错误最小化.

Please have a look at the existing automappers (see the other answers for links) and use them instead of reinventing the wheel by yourself - the existing libraries are optimized for speed, thoroughly tested and are very comfortable to use. This way you will minimize errors in your code.

这篇关于如果两个属性具有相同的属性,如何在两个不同类的实例之间自动映射值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆