Language: C#
Get all properties from a type up to a specified base type in the inheritance hierarchy
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: }
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.
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

