如果两个对象具有相同的特性,可以从一个值来autoassigned? [英] If two objects have same properties, can the values from one be autoassigned?

查看:87
本文介绍了如果两个对象具有相同的特性,可以从一个值来autoassigned?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个学术问题比什么都重要......我知道还有更实际的手段发展明智的处理...



假设你有下面的代码




公共MyObject1 AssignTest(MyObject1 OBJ1,MyObject2 OBJ2)
{

 } 




凡MyObject1和MyObject2拥有的确切的相同的属性可用。是否有可能将所有已在MyObject1指定,自动将MyObject2值?而不是要通过声明和分配值(即MyObject1.Property1 = MyObject2.Property1等)


解决方案

一种可能性,使这个(例如,用于创建自己的automapper或理解它是如何工作的基本的目的)是使用(如已经建议)的反射。代码可以是这样的:

  // TODO:错误处理
//测试类
公A类
{
公共字符串名称{;组; }
公众诠释计数;
}

公共类B
{
公共字符串名称{;组; }
公众诠释计数;
}
//复制常规
公众B CopyAToB(A一)
{
B B =新的B();
//复制领域
VAR typeOfA = a.GetType();
VAR typeOfB = b.GetType();
的foreach(在typeOfA.GetFields VAR fieldOfA())
{
VAR fieldOfB = typeOfB.GetField(fieldOfA.Name);
fieldOfB.SetValue(二,fieldOfA.GetValue(一));
}
//复制的属性
的foreach(VAR propertyOfA在typeOfA.GetProperties())
{
VAR propertyOfB = typeOfB.GetProperty(propertyOfA.Name);
propertyOfB.SetValue(二,propertyOfA.GetValue(一));
}

回复B;
}



该功能可用于这样的:

  VAR一个=新的A 
{
NAME =A,
计数= 1
};

变种B = CopyAToB(一);
Console.Out.WriteLine(的String.Format({0} - {1},b.Name,b.Count));



输出是:



  A  -  1 

请注意,反射的使用自带的价格 - 它的成本的表现。使用反射,你可以访问私人和公共对象成员。这例如从Visual Studio使用,以访问所有测试对象成员创建测试访问对象。



请看看现有的automappers(链接见其他答案),并用它们来代替的自己重新发明轮子的 - 的现有的库速度,彻底的测试和优化,非常舒适的使用。这样,您将减少代码中的错误。


This is an academic question more than anything...I know there are more practical means development-wise to handle...

Assuming you have the following code:

public MyObject1 AssignTest (MyObject1 obj1, MyObject2 obj2) {

    }

Where MyObject1 and MyObject2 have the exact same properties available. is it possible to assign all the values that have been specified in MyObject1 to MyObject2 automagically? Rather than going through and assigning the values declaratively (i.e. MyObject1.Property1 = MyObject2.Property1 etc.)

解决方案

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));

The output is:

a - 1

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.

这篇关于如果两个对象具有相同的特性,可以从一个值来autoassigned?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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