title: Object to Object Property mapping in collections
body: <p>Im creating a collection of (dynamically generated type) for display in a silverlight grid and one of the processes involves creating an import (dynamically generated type) type then mapping the properties on the import type to the collection of (dynamically generated type) both types share a Id property that identifies the item ( be it on the grid or in the import)</p>
  
  <p>ie
  type bound to grid</p>
  
  <pre><code> int Id {get; set}     
   string Foo {get;set;}
   string FooFoo {get;set;}
  </code></pre>
  
  <p>and import the type</p>
  
  <pre><code> int Id {get; set}
   string Foo {get;set}
  </code></pre>
  
  <p>where ids match i want to copy foos.</p>
  
  <p>What is a fast way to map properties from the one type to another in a collection?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Heres the final Typemapper implementation with thanks to Stephan, as a feature will only map the two types when the keymembers are equal, mappings defined via a dictionary string string representing the member names, <strong>works in silverlight</strong>.</p>
  
  <pre><code>public class TypeMapper
  { 
      private readonly DynamicMethod _mapper;
  
  
      public static DynamicMethod BuildMapper(Type fromType, 
                                              Type toType,
                                              KeyValuePair&lt;string, string&gt; keyMemberMap,
                                              Dictionary&lt;string, string&gt; memberMappings)
      {
  
          var method = new DynamicMethod("Map", typeof(bool), new[] { fromType, toType });
  
          // Preparing Reflection instances
          MethodInfo getFromKeyMethod = fromType.GetMethod(
              string.Format("get_{0}", keyMemberMap.Key),
              BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  
          MethodInfo getToKeyMethod = toType.GetMethod(
              string.Format("get_{0}", keyMemberMap.Value),
              BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  
          ILGenerator gen = method.GetILGenerator();
  
          // Preparing locals
          gen.DeclareLocal(typeof(Boolean));
          // Preparing labels
          Label labelNoMatch = gen.DefineLabel();
          // Writing body
          gen.Emit(OpCodes.Ldarg_0);
          gen.Emit(OpCodes.Callvirt, getFromKeyMethod);
          gen.Emit(OpCodes.Ldarg_1);
          gen.Emit(OpCodes.Callvirt, getToKeyMethod);
          gen.Emit(OpCodes.Ceq);
          gen.Emit(OpCodes.Stloc_0);
          gen.Emit(OpCodes.Ldloc_0);
          gen.Emit(OpCodes.Brfalse_S, labelNoMatch);
          gen.Emit(OpCodes.Ldarg_1);
          gen.Emit(OpCodes.Ldarg_0);
  
  
          foreach (var mapping in memberMappings)
          {
              var getFromValueMethod = fromType.GetMethod(
                 string.Format("get_{0}", mapping.Key),
                 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  
              var setToValueMethod = toType.GetMethod(
                  string.Format("set_{0}", mapping.Value),
                  BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  
              gen.Emit(OpCodes.Callvirt, getFromValueMethod);
              gen.Emit(OpCodes.Callvirt, setToValueMethod);
          }
  
          gen.MarkLabel(labelNoMatch);
          gen.Emit(OpCodes.Ldloc_0);
          gen.Emit(OpCodes.Ret);
  
  
          return method;
      }
  
      public void Map (object fromInstance, object toInstance)
      {
          _mapper.Invoke(null, new[] { fromInstance, toInstance });
      }
  
  
      public TypeMapper(Type fromType, Type toType, 
          KeyValuePair&lt;string, string&gt; keyMemberMap, 
          Dictionary&lt;string, string&gt; memberMappings)
      {
          _mapper = BuildMapper(fromType, toType, keyMemberMap, memberMappings); 
      }
  
  }
  </code></pre>