Blame view

Vrh.Web.OneReport.Lib/ReportViewerForMVC/CopyPropertiesHelper.cs 1.47 KB
ab9f2fbe   Schwirg László   Add project files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  using System;
  using System.Reflection;
  
  namespace Vrh.Web.OneReport.ReportViewerForMvc
  {
      internal static class CopyPropertiesHelper
      {
          internal static void Copy<T>(ref T obj, T properties)
          {
              if (properties == null)
              {
                  throw new ArgumentNullException("properties", "Value cannot be null.");
              }
  
              Copy<T, T>(ref obj, properties);
          }
  
          internal static void Copy<T1, T2>(ref T1 obj, T2 properties)
          {
              Type objType = obj.GetType();
              Type propertiesType = properties.GetType();
              BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
  
              foreach (PropertyInfo propertyInfo in propertiesType.GetProperties(bindingFlags))
              {
                  try
                  {
                      if (propertyInfo.CanRead)
                      {
                          var valueToCopy = propertyInfo.GetValue(properties);
                          var objProperty = objType.GetProperty(propertyInfo.Name);
  
                          if (objProperty.CanWrite)
                          {
                              objProperty.SetValue(obj, valueToCopy);
                          }
                      }
                  }
                  catch (NullReferenceException ex)
                  {
                      throw ex;
                  }
                  catch (TargetInvocationException) { } //Do nothing, just like my boss.
              }
          }
      }
  }