13

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

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

  2. 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)?

Example

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

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

For this simple sample - which method is faster?

Update

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);
}
Community
  • 1
  • 1
net_prog
  • 9,921
  • 16
  • 55
  • 70

5 Answers5

14

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.

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?

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

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.

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.

k.m
  • 30,794
  • 10
  • 62
  • 86
  • I have simple classes with primitive types, such as int, string, etc... I use serializer suggested here: http://stackoverflow.com/questions/78536/cloning-objects-in-c-sharp Types of the classes are supplied by external code. – net_prog Nov 18 '11 at 11:35
  • @net_prog: check my edit. I've run simple test for your class using both methods. – k.m Nov 18 '11 at 17:39
  • Thank you, this is what I was looking for. I actually wanted the class to be with properties, not fields, I have corrected the sample. But even in this case reflection is faster as I can see. – net_prog Nov 19 '11 at 06:18
  • If you specify `System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.NonPublic` as the search options for `GetFields()`, it includes backing fields for all properties. – Suncat2000 Aug 30 '23 at 14:33
8

Ultimately, general-purpose serializers (such as BinaryFormatter, via ObjectCopier) are using reflection. How well they use it depends on the specific serializer, but there is always extra overhead involved if you are serializing.

Since you only want a shallow copy, a tool like AutoMapper is the most appropriate tool here; again, it is using reflection (but I expect it is doing it "the right way", i.e. not via GetValue()/SetValue()), but it doesn't have the serialization costs.

In this scenario, serialization is overkill; AutoMapper is perfectly reasonable. If you wanted deep-clones, it gets trickier... serialization may start being tempting. I still probably wouldn't choose BinaryFormatter myself, but I'm very fussy about serialization ;p

It would of course be trivial to right some basic reflection that does the same via GetValue() etc, but that would be slow. Another interesting option here is that you can use the Expression API to create an object copier at runtime.... but... AutoMapper does everything you'd need here, so it seems redundant effort.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0
void Copy(object copyToObject, object copyFromObject)
{
    BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

    FieldInfo[] fields = copyFromObject.GetType().GetFields(flags);
    for (int i = 0; i < fields.Length; ++i)
    {
        BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
            | BindingFlags.Static;
        FieldInfo field = copyFromObject.GetType().GetField(fields[i].Name, bindFlags);
        FieldInfo toField = copyToObject.GetType().GetField(fields[i].Name, bindFlags);
        if(field != null)
        {
            toField.SetValue(copyToObject, field.GetValue(copyFromObject));
        }
    }
}
  • The code might answer the question. But you help the OP more if you explain what the code does and how it solves the question. – Markus Aug 10 '17 at 06:12
0

Binary serialization is very fast I use it a lot for this kind of problem

Deep cloning objects

Community
  • 1
  • 1
remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
  • For serialization I use the ObjectCopier from that thread, is it faster than reflection? – net_prog Nov 18 '11 at 11:29
  • 3
    @net_prog how do you think `BinaryFormatter` (the serializer under `ObjectCopier`) gets its data? Hint: it starts with "r" and sounds like "deflection". – Marc Gravell Nov 18 '11 at 11:47
0

If you are copying the properties during run time, reflection would be the answer. I would go for serialization if its not during run time. Serialization vs Reflection look at this once.

Community
  • 1
  • 1
nebula
  • 3,932
  • 13
  • 53
  • 82
  • If you know the properties of the object during run time, reflection would be waste of time, serialization would be the answer. But if you don't know the properties of the object during run time, reflection is the only solution i can think of. – nebula Nov 18 '11 at 11:31
  • If you know the properties, why would serialization suddenly be better than reflection? Not sure that makes sense... – Marc Gravell Nov 18 '11 at 11:45
  • because reflection is used when the type of object is not known during run time. If the type of object is already known, why to bother using reflection when serialization(binary serialization) is fast and easily achieved. – nebula Nov 18 '11 at 11:52
  • 1
    That makes no sense at all. Serialisation is going to involve reflection here; adding the actual encode/decode steps only adds work. For two reasonable implementations of a reflection-copier vs serialisation, and for a shallow copy, the reflection-copier is preferable in every way. Spoken as someone who knows as awful lot about both reflection and serialisation. – Marc Gravell Nov 19 '11 at 11:05