CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Copy property values from one instance to another where the class inherits from TypedPageData

437 Views
Copy Code Show/Hide Line Numbers
   1:  /// <summary>
   2:  /// Return all properties from a type up to a specified base type in the inheritance hierarchy
   3:  /// </summary>
   4:  /// <param name="type">Type that will be examined</param>
   5:  /// <param name="baseType">Where to stop in the inheritance hierarchy. Must be a type that first parameter inherits from
   6:  /// </param>
   7:  /// <returns>A list of all found properties</returns>
   8:  public static List<PropertyInfo> GetAllProperties(Type type, Type baseType)
   9:  {
  10:     List<PropertyInfo> properties = new List<PropertyInfo>();
  11:     while (type != baseType && baseType != typeof(System.Object))
  12:     {
  13:        properties.AddRange(type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).ToList());
  14:        type = type.BaseType;
  15:     }
  16:   
  17:     return properties;
  18:  }
  19:   
  20:  /// <summary>
  21:  /// Copy all property values from a page object to another using reflection to find all own declared properties
  22:  /// </summary>
  23:  /// <param name="sourcePage"></param>
  24:  /// <param name="targetPage"></param>
  25:  /// <returns></returns>
  26:  public static T CopyPageDataProperties<T>(T sourcePage, T targetPage) where T : TypedPageData 
  27:  {
  28:     return (T)CopyPageDataProperties<T>(sourcePage, targetPage, GetAllProperties(typeof(T), typeof(TypedPageData)));
  29:  }
  30:   
  31:  /// <summary>
  32:  /// Copy all specified properties from one page instance to another
  33:  /// </summary>
  34:  /// <param name="targetPage"></param>
  35:  /// <param name="sourcePage"></param>
  36:  /// <param name="properties"></param>
  37:  /// <returns></returns>
  38:  public static T CopyPageDataProperties<T>(T sourcePage, T targetPage, List<PropertyInfo> properties) where T : TypedPageData 
  39:  {
  40:     foreach (PropertyInfo prop in properties)
  41:     {
  42:        if (prop.CanWrite)
  43:        {
  44:           try
  45:           {
  46:              object propVal = GetProperty(sourcePage, prop.Name);
  47:              SetProperty(targetPage, prop.Name, propVal);
  48:           }
  49:           catch
  50:           {
  51:              throw new Exception(); // TODO: Change to a proper exception with a  nice clean message ;)
  52:           }
  53:        }
  54:     }
  55:     return (T)targetPage;
  56:  }
  57:   
  58:  /// <summary>
  59:  /// Get a property value from an object
  60:  /// </summary>
  61:  /// <param name="containingObject"></param>
  62:  /// <param name="propertyName"></param>
  63:  /// <returns></returns>
  64:  private static object GetProperty(object containingObject, string propertyName)
  65:  {
  66:     return containingObject.GetType().InvokeMember(propertyName, BindingFlags.GetProperty, null, containingObject, null);
  67:  }
  68:   
  69:  /// <summary>
  70:  /// Set a property value in an object
  71:  /// </summary>
  72:  /// <param name="containingObject"></param>
  73:  /// <param name="propertyName"></param>
  74:  /// <param name="newValue"></param>
  75:  private static void SetProperty(object containingObject, string propertyName, object newValue)
  76:  {
  77:     containingObject.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, containingObject, new object[] { newValue });
  78:  }
by rawbert
  March 11, 2010 @ 12:37pm
Tags:
Description:
I wanted to copy property values from one instance to another, but only do this with properties that I declared myself, up to a certain base type in the inheritance hierarchy.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate