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

Get all properties from a type up to a specified base type in the inheritance hierarchy

292 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:  }
by rawbert
  March 11, 2010 @ 12:25pm
Tags:
Description:
I created this piece of code when I needed to copy property values from one class instance to another without knowing the exact properties. Needed it to be generic so I could use it for different classes.

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