Advertisement
pushrbx

MergeWith function

Aug 17th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.60 KB | None | 0 0
  1. public static void MergeWith<T>(this T primary, T secondary)
  2. {
  3.     foreach (var pi in typeof(T).GetProperties())
  4.     {
  5.         var priValue = pi.GetGetMethod().Invoke(primary, null);
  6.         var secValue = pi.GetGetMethod().Invoke(secondary, null);
  7.  
  8.         bool bnullable = false;
  9.  
  10.         if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Nullable<>))
  11.             bnullable = true;
  12.  
  13.         if (((bnullable && secValue != null) == false) && !priValue.Equals(secValue))
  14.         {
  15.             pi.GetSetMethod().Invoke(primary, new object[] { secValue });
  16.         }
  17.     }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement