Debugging Editlevels using Hierarchy

Debugging Editlevels using Hierarchy

Old forum URL: forums.lhotka.net/forums/t/4613.aspx


dlabar posted on Thursday, April 03, 2008

I seem to have frequent issues with edit levels not being kept in sync.  VS can show you the edit level on an object, but it gets annoying having to search through and get the protected property for each object that I'm concerened about in my BO hierachy.

As a helpful solution, I have created these Extension methods to get the edit level of an object, and the edit level of all objects within this object.   Now in VS, I can just call BO.GetEditLevelHierachy, and see the edit level of all objects in the collection.

Please use at your own risk, and please submit anything I've done wrong.  This is my first experiance with using Reflection and I didn't put much effort into cleaning it up.  The Output should look something like:

<Namespace>.<Class Name>:<edit level>

BusinessObjects.CustomerWorkflowSteps:1
   BusinessObjects.CustomerWorkflowStep:2
      BusinessObjects.CustomerWorkflowStepEvaluations:1
      BusinessObjects.CustomerWorkflowStepServiceActions:1
      BusinessObjects.CustomerWorkflowStepEscalations:1
      BusinessObjects.CustomerWorkflowStepRoles:1

Enjoy!

Smile [:)]

 

public static string GetEditLevelHierarchy(this Csla.Core.IBusinessObject root){
    string message = "";

    message = root.GetType().FullName + ":" + root.GetEditLevel();
    message += GetChildEditLevelHierarchy(root, 0);

    return message;
}

public static string GetEditLevel(this Csla.Core.IBusinessObject obj) {
    System.Reflection.MethodInfo theMethod = obj.GetType().GetProperty("EditLevel", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetGetMethod(true);
    return ((int)theMethod.Invoke(obj, null)).ToString();
}

private static string GetChildEditLevelHierarchy(this Csla.Core.IBusinessObject parent, int tabLevel) {
    string message = "";
    string tab = "\n" + GetTab(tabLevel);
    System.Reflection.PropertyInfo[] properties = parent.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

    foreach (System.Reflection.PropertyInfo property in properties) {
        switch (property.Name) {

            case "Item":
                //Handle each Child in my collection
                int count = (int)parent.GetType().GetProperty("Count", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).GetGetMethod(false).Invoke(parent, null);
                for (int i = 0; i < count; i++) {
                    Csla.Core.IBusinessObject child = (Csla.Core.IBusinessObject)parent.GetType().GetProperty("Item", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).GetGetMethod(false).Invoke(parent, new object[1] { i++ });
                   
                    //Get Child Edit Level
                    message += tab + child.GetType().FullName + ":" + ((Csla.Core.IBusinessObject)child).GetEditLevel();

                    //Get Child Hierarchy
                    message += GetChildEditLevelHierarchy(child, tabLevel + 1);
                }
                break;

            case "BrokenRulesCollection":
                //Skip
                break;

            default:
                object value = property.GetGetMethod(true).Invoke(parent, null);
                if (value != null) {
                    Type propertyType = value.GetType();
                    if (propertyType.GetInterface(typeof(Csla.Core.IBusinessObject).FullName) != null) {
                        //Get Edit Level
                        message += tab + value.GetType().FullName + ":" + ((Csla.Core.IBusinessObject)value).GetEditLevel();
                        if (propertyType.GetInterface(typeof(Csla.Core.IParent).FullName) != null) {
                            message += GetChildEditLevelHierarchy((Csla.Core.IBusinessObject)value, tabLevel + 1);
                        }
                    }
                }
                break;
        }
    }

    return message;
}

private static string GetTab(int tabLevel) {
    string tab = "";
    for (int i = 0; i <= tabLevel; i++) {
        tab += "\t";
    }
    return tab;
}

DavidP replied on Wednesday, November 18, 2009

Thanks for this! I used this while debugging my own EditLevel woes.

Copyright (c) Marimer LLC