priore

Copy the values of a EntityObject ​​in another EntityObject

Mar 8th, 2014
136
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×