Advertisement
priore

Copy the values of a EntityObject ​​in another EntityObject

Mar 8th, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. /// <summary>
  2. /// Extension Method of the Entity Object This copies the values ​​in another EntityObject with common properties
  3. /// </summary>
  4. /// <typeparam name="T"></typeparam>
  5. /// <param name="source"></param>
  6. /// <param name="dest"></param>
  7.     public static void MigrateTo<T>(this T source, EntityObject dest) where T : EntityObject
  8.     {
  9.         List<PropertyInfo> sourcePropList = (from a in source.GetType().GetProperties()
  10.                                        where !a.PropertyType.Namespace.Contains("System.Data")
  11.                                        select a).ToList();
  12.  
  13.         List<PropertyInfo> destPropList = (from a in dest.GetType().GetProperties()
  14.                                            where !a.PropertyType.Namespace.Contains("System.Data")
  15.                                            select a).ToList();
  16.  
  17.         foreach (PropertyInfo source_prop in sourcePropList)
  18.         {
  19.             // exclude property named "id" (usually the id of the record)
  20.             if (!source_prop.Name.Equals("id"))
  21.             {
  22.                 if (source_prop.GetValue(source, null) != null)
  23.                 {
  24.                     foreach (PropertyInfo dest_prop in destPropList)
  25.                     {
  26.                         if (dest_prop.Name.Equals(source_prop.Name))
  27.                         {
  28.                             dest_prop.SetValue(dest, source_prop.GetValue(source, null), null);
  29.                         }
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement