Advertisement
sirjordan

ObjectExtender

Apr 30th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. public static class ObjectExtender
  2.     {
  3.         /// <summary>
  4.         /// Copy the equal property values from original object to receiver.
  5.         /// Copy only value type properties (and string) with same name and type.
  6.         /// You can use different object types.
  7.         /// </summary>
  8.         /// <param name="original">Source</param>
  9.         /// <param name="receiver">Destination</param>
  10.         public static void MapProperties(this object original, object receiver)
  11.         {
  12.             List<PropertyInfo> originalProps = original.GetType().GetProperties().ToList();
  13.             List<PropertyInfo> receiverProps = receiver.GetType().GetProperties().ToList();
  14.  
  15.             foreach (PropertyInfo prop in originalProps)
  16.             {
  17.                 if (receiverProps.Exists(p => p.Name == prop.Name
  18.                     && p.PropertyType.IsAssignableFrom(prop.PropertyType))
  19.                     && (prop.PropertyType.IsValueType || prop.PropertyType == typeof(string)))
  20.                 {
  21.                     string name = prop.Name;
  22.                     object value = original.GetType().GetProperty(name).GetValue(original, null);
  23.                     receiver.GetType().GetProperty(name).SetValue(receiver, value, null);
  24.                 }
  25.             }
  26.         }
  27.  
  28.  
  29.         public static void CopyPropertiesTo<T>(this T source, T destination)
  30.         {
  31.             PropertyInfo[] destinationProperties = destination.GetType().GetProperties();
  32.             foreach (PropertyInfo destinationProperty in destinationProperties)
  33.             {
  34.                 if (destinationProperty.CanWrite)
  35.                 {
  36.                     PropertyInfo sourceProperty = source.GetType().GetProperty(destinationProperty.Name);
  37.                     if (sourceProperty.PropertyType.IsValueType || sourceProperty.PropertyType == typeof(string))
  38.                     {
  39.                         destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.         public static string ToStringOrNull(this object o)
  45.         {
  46.             if (o == null)
  47.             {
  48.                 return null;
  49.             }
  50.             else
  51.             {
  52.                 return o.ToString();
  53.             }
  54.         }
  55.         public static int? ToIntOrNull(this object o)
  56.         {
  57.             if (o == null)
  58.             {
  59.                 return null;
  60.             }
  61.             else
  62.             {
  63.                 int value;
  64.                 if (int.TryParse(o.ToString(), out value))
  65.                 {
  66.                     return value;
  67.                 }
  68.                 else
  69.                 {
  70.                     return null;
  71.                 }
  72.             }
  73.         }
  74.         public static decimal? ToDecimalOrNull(this object o)
  75.         {
  76.             if (o == null)
  77.             {
  78.                 return null;
  79.             }
  80.             else
  81.             {
  82.                 decimal value;
  83.                 if (decimal.TryParse(o.ToString(), out value))
  84.                 {
  85.                     return value;
  86.                 }
  87.                 else
  88.                 {
  89.                     return null;
  90.                 }
  91.             }
  92.         }
  93.         public static DateTime? ToDateOrNull(this object o)
  94.         {
  95.             if (o == null)
  96.             {
  97.                 return null;
  98.             }
  99.             else
  100.             {
  101.                 DateTime value;
  102.                 if (DateTime.TryParse(o.ToString(), out value))
  103.                 {
  104.                     return value;
  105.                 }
  106.                 else
  107.                 {
  108.                     return null;
  109.                 }
  110.             }
  111.         }
  112.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement