复制对象属性:反射或序列化-哪个更快? [英] Copy object properties: reflection or serialization - which is faster?

查看:496
本文介绍了复制对象属性:反射或序列化-哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相同类型的对象,需要将属性值从一个对象复制到另一个.有两种选择:

I have two objects of the same type and need to copy property values from one object to another. There are two options:

  1. 使用反射,浏览第一个对象的属性并复制值.

  1. Use reflection, navigate through the properties of the first object and copy the values.

序列化第一个对象并反序列化副本.

Serialize the first object and deserialize a copy.

两项工作都符合我的要求,问题是在速度(成本)方面我最好使用哪一种?

Both work for my requirement, the question is which do I better use in the terms of speed (cost)?

示例

class Person
{
    public int ID { get; set; }
    public string Firsthand { get; set; } 
    public string LastName { get; set; } 
    public int Age { get; set; } 
    public decimal Weight { get; set; } 
}

需要将属性值从Person p1复制到Person p2.

Need to copy property values from Person p1 to Person p2.

对于这个简单的示例-哪种方法更快?

For this simple sample - which method is faster?

更新

对于序列化,我使用此处建议的ObjectCopier:深度克隆对象

For serialization I use the ObjectCopier suggested here: Deep cloning objects

为了进行反思,我使用以下代码:

For reflection I use this code:

foreach (PropertyInfo sourcePropertyInfo in copyFromObject.GetType().GetProperties())  
{
    PropertyInfo destPropertyInfo = copyToObject.GetType().GetProperty(sourcePropertyInfo.Name);

    destPropertyInfo.SetValue(
        copyToObject,
        sourcePropertyInfo.GetValue(copyFromObject, null),
        null);
}

推荐答案

这全部取决于您要复制的内容以及计划使用的序列化程序类型.关于序列化器的事情是,其中的一些实际上可能是使用反射作为构建对象的基础机制.

It all depends on what you want to copy, and what kind of serializer you plan to use. Thing with serializers is, some of them might be actually using reflection as underlying mechanism of building objects.

编辑#1:据我所知,您的班级使用的BinaryFormatter确实利用反射来完成其工作.所以问题是,您可以为您的类型编写比Microsoft在一般情况下更好的(更快的)自定义反射代码吗?

Edit #1: As far as I know, BinaryFormatter used by your class does utilize reflection to do its work. So question is, can you write better (faster?) custom reflection code for your types than Microsoft did for general scenario?

编辑#2:出于好奇,我进行了简单的测试.在执行浅拷贝方面,BinaryFormatter与反思.我使用的反射代码可以在这里看到:

Edit #2: Out of curiosity, I've run simple test. BinaryFormatter vs reflection in terms of performing shallow copy. Reflection code I used can be seen here:

var newPerson = Activator.CreateInstance<Person>();
var fields = newPerson.GetType().GetFields(BindingFlags.Public 
    | BindingFlags.Instance);
foreach (var field in fields)
{
    var value = field.GetValue(person);
    field.SetValue(newPerson, value);
}

与您使用的ObjectCopier类相比,结果是什么?反射似乎比序列化代码快 7倍.但是,这适用于具有公共字段Person类.对于属性,差异仍然很明显,但仅快2倍.

What are the results compared to ObjectCopier class you're using? Reflection seems to perform 7 times faster than serialization code. This however applies to Person class with public fields. For properties, difference is still noticable, but it's only 2 times faster.

我认为与众不同之处在于BinaryFormatter需要使用流,这会带来额外的开销.但这只是我的假设,可能与事实相去甚远.

I assume difference comes from the fact that BinaryFormatter needs to use streams, which introduce additional overhead. Yet this is just my assumption, which might be far from facts.

可以在此处找到我使用的测试程序的源代码.欢迎任何人指出它的缺陷和可能的问题:-)

Source code for testing program I used can be found here. Anybody is welcome to point flaws and possible problems with it :-)

侧注
与所有我在想..." 基准一样,我建议您带着一点盐.仅当其性能确实成为问题时才应进行此类优化.

Sidenote
As with all "I was wondering..." benchmarks, I suggest you take it with a grain of salt. Such optimizations should be only made when their performance actually becomes an issue.

这篇关于复制对象属性:反射或序列化-哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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