Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive routine to obtain PropertyInfo
    primarykey
    data
    text
    <p>I'm attempting to create a recursive routine that will retrieve PropertyInfos for all members under a specified object (in .NET 3.5). Everything for immediate members is working, but it needs to parse nested classes as well (and their nested classes, etc).</p> <p>I do not understand how to handle the section that parses nested classes. How would you write this part of the code?</p> <pre><code>public class ObjectWalkerEntity { public object Value { get; set; } public PropertyInfo PropertyInfo { get; set; } } public static class ObjectWalker { // This will be the returned object static List&lt;ObjectWalkerEntity&gt; objectList = new List&lt;ObjectWalkerEntity&gt;(); public static List&lt;ObjectWalkerEntity&gt; Walk(object o) { objectList.Clear(); processObject(o); return objectList; } private static void processObject(object o) { if (o == null) { return; } Type t = o.GetType(); foreach (PropertyInfo pi in t.GetProperties()) { if (isGeneric(pi.PropertyType)) { // Add generic object ObjectWalkerEntity obj = new ObjectWalkerEntity(); obj.PropertyInfo = pi; obj.Value = pi.GetValue(o, null); objectList.Add(obj); } else { ////// TODO: Find a way to parse the members of the subclass... // Parse each member of the non-generic object foreach (Object item in pi.PropertyType) { processObject(item); } } } return; } private static bool isGeneric(Type type) { return Extensions.IsSubclassOfRawGeneric(type, typeof(bool)) || Extensions.IsSubclassOfRawGeneric(type, typeof(string)) || Extensions.IsSubclassOfRawGeneric(type, typeof(int)) || Extensions.IsSubclassOfRawGeneric(type, typeof(UInt16)) || Extensions.IsSubclassOfRawGeneric(type, typeof(UInt32)) || Extensions.IsSubclassOfRawGeneric(type, typeof(UInt64)) || Extensions.IsSubclassOfRawGeneric(type, typeof(DateTime)); } </code></pre> <p><strong>Edit:</strong> I've used some of Harlam's suggestions, and came up with a working solution. This handles both nested classes and lists.</p> <p>I've replaced my previous loop through the propertyinfo with the following</p> <pre><code>foreach (PropertyInfo pi in t.GetProperties()) { if (isGeneric(pi.PropertyType)) { // Add generic object ObjectWalkerEntity obj = new ObjectWalkerEntity(); obj.PropertyInfo = pi; obj.Value = pi.GetValue(o, null); objectList.Add(obj); } else if (isList(pi.PropertyType)) { // Parse the list var list = (IList)pi.GetValue(o, null); foreach (object item in list) { processObject(item); } } else { // Parse each member of the non-generic object object value = pi.GetValue(o, null); processObject(value); } } </code></pre> <p>I've also added a new check to see if something is a list.</p> <pre><code>private static bool isList(Type type) { return IsSubclassOfRawGeneric(type, typeof(List&lt;&gt;)); } </code></pre> <p>Thanks for the help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload