Hi, Guys,

I'm having a problem, maybe someone can help me out.

I want to calculate the inheritance depth for a given type. Here is
the method I'm using:

int depth = getDepth(typedef)

private int getDepth(TypeDefinition typeDef)
{
    int depth = 1;
    TypeDefinition basetype = typeDef.BaseType as TypeDefinition;

    if (basetype != null && basetype.Name != "Object")
        depth += getDepth(basetype);

    return depth;
}

I works perfectly for a test-case like this:

class Dad {}
class Kid : Dad {}
class GrandKid : Kid { }

The inheritance depths are: GrandKid = 3, Kid = 2, Dad = 1

But for, e.g. WindowsFormsApplication1.Form1 type, the BaseType
(System.Windows.Forms.Form) is not casteable to TypeDefinition, which
results in premature recursion break and inheritance depth = 1.

What could be the problem? Is there another way to traverse through
the inheritance tree with Cecil?

Thanks,
Lev

PS. I can do the same thing with System.Reflection, but there is a lot
of stuff that I gather with Cecil, so I wouldn't want to load an
assembly just for calculating the inheritance depth.
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---

Reply via email to