Reflection class to get all properties of any object
c#, reflection
Solution
If you want all of the properties, try:
propertyInfos = thisObject.GetType().GetProperties(
BindingFlags.Public | BindingFlags.NonPublic // Get public and non-public
| BindingFlags.Static | BindingFlags.Instance // Get instance + static
| BindingFlags.FlattenHierarchy); // Search up the hierarchy
For details, see BindingFlags.
Problem
I need to make a function that get all the properies of an object (including an children objects) This is for my error logging feature. Right now my code always returns 0 properties. Please let me know what I'm doing wrong, thanks! ``` public static string GetAllProperiesOfObject(object thisObject) { string result = string.Empty; try { // get all public static properties of MyClass type PropertyInfo[] propertyInfos; propertyInfos = thisObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Static);//By default, it will return only public properties. // sort properties by name Array.Sort(propertyInfos, (propertyInfo1, propertyInfo2) => propertyInfo1.Name.CompareTo(propertyInfo2.Name)); // write property names StringBuilder sb = new StringBuilder(); sb.Append("<hr />"); foreach (PropertyInfo propertyInfo in propertyInfos) { sb.AppendFormat("Name: {0} | Value: {1} <br>", propertyInfo.Name, "Get Value"); } sb.Append("<hr />"); result = sb.ToString(); } catch (Exception exception) { // to do log it } return result; } ``` here's what the object looks like: