Click here to Skip to main content
Licence Ms-PL
First Posted 9 Apr 2010
Views 4,152
Bookmarked 6 times

Simple properties Mapper by reflection: Stop copying manually each property of your objects !

By | 9 Apr 2010 | Technical Blog
There are times when you have to copy each property of an object to one another. This is called mapping and it's very fastidious to do it by hand. In this post, we'll see how to create a method extension which do it for you in one line of code!
A Technical Blog article. View original blog here.[^]

There are times when you have to copy each property of an object to one another. This is called mapping and it's very fastidious to do it by hand.

In this post, we'll see how to create a method extension which do it for you in one line of code!

When can it be useful

Here is a non exhaustive list of usage you can find to this snippet:

  • You want to reload the previous state of an object without changing its instance: just clone it as a snapshot and copy back the properties when you want.
  • You get some Data Transfer Object coming from WCF/Web services and you want to fill a specific object with this data.
  • etc.

I surely know this is not the most efficient way to solve these problems, but this is fast to use/understand and easy to implement.

Let's jump to the code !

What's inside? Quite a few things actually! There are two objects, the source in which we take the data and the target in which we fill in the data.

I use reflection to obtain every property of the source and then I check on the target if a property exists with the same name. If yes, I fill it with the value of the property in the source object.

I also added a filter which is a list of Property names which will be ignored by the "copy process".

With a little more time, you can also add a dictionary which may map a property name in the source to another property name in the target if the names are noted to be exactly the same...

public static class PropetiesMapper{
    /// <summary>
    /// Copies all the properties of the "from" object to this object if they exist.
    /// </summary>
    /// <param name="to">The object in which the properties are copied</param>
    /// <param name="from">The object which is used as a source</param>
    /// <param name="excludedProperties">Exclude these properties from the copy</param>
    public static void copyPropertiesFrom
	(this object to, object from, string[] excludedProperties)
    {
      Type targetType = to.GetType();
      Type sourceType = from.GetType();
 
      PropertyInfo[] sourceProps = sourceType.GetProperties();
      foreach (var propInfo in sourceProps)
      {
        //filter the properties
        if (excludedProperties != null
          && excludedProperties.Contains(propInfo.Name))
          continue;
 
        //Get the matching property from the target
        PropertyInfo toProp =
          (targetType == sourceType) ? propInfo : targetType.GetProperty(propInfo.Name);
 
        //If it exists and it's writeable
        if (toProp != null && toProp.CanWrite)
        {
          //Copy the value from the source to the target
          Object value = propInfo.GetValue(from, null);
          toProp.SetValue(to,value , null);
        }
      }
    }
 
    /// <summary>
    /// Copies all the properties of the "from" object to this object if they exist.
    /// </summary>
    /// <param name="to">The object in which the properties are copied</param>
    /// <param name="from">The object which is used as a source</param>
    public static void copyPropertiesFrom(this object to, object from)
    {
      to.copyPropertiesFrom(from, null);
    }
 
    /// <summary>
    /// Copies all the properties of this object to the "to" object
    /// </summary>
    /// <param name="to">The object in which the properties are copied</param>
    /// <param name="from">The object which is used as a source</param>
    public static void copyPropertiesTo(this object from, object to)
    {
      to.copyPropertiesFrom(from, null);
    }
 
    /// <summary>
    /// Copies all the properties of this object to the "to" object
    /// </summary>
    /// <param name="to">The object in which the properties are copied</param>
    /// <param name="from">The object which is used as a source</param>
    /// <param name="excludedProperties">Exclude these properties from the copy</param>
    public static void copyPropertiesTo
	(this object from, object to, string[] excludedProperties)
    {
      to.copyPropertiesFrom(from, excludedProperties);
    }
  }

So, do you still want to put a "Hand made" label on your object's mapping ? :-D

A more complex and complete tool

For those who want something more powerful and complete, take a look at the automapper library on CodePlex.

Shout it kick it on DotNetKicks.com

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

jmix90

Software Developer
http://wpf-france.fr
France (Metropolitan) France (Metropolitan)

Member

Follow on Twitter Follow on Twitter
Jonathan creates software, mostly with C#,WPF and XAML.

He really likes to works on every Natural User Interfaces(NUI : multitouch, touchless, etc...) issues.



He is awarded Microsoft MVP in the "Client Application Development" section since 2011.
 

You can check out his WPF/C#/NUI/3D blog http://www.jonathanantoine.com.

He is also the creator of the WPF French community web site : http://wpf-france.fr.

Here is some videos of the projects he has already work on :


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralLight duty only PinmemberArchAngel12313:56 12 Apr '10  
GeneralRe: Light duty only [modified] Pinmemberjmix9019:53 12 Apr '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 Apr 2010
Article Copyright 2010 by jmix90
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid