Thanks, JB and Sebastien,
It works fine, though a little bit slow, when the inheritance is quite deep
:)
Do you mind if I borrow this piece of code for my open-source project?
With the credits to you of cource.
Regards,
Lev
PS.That's a code metrics related projet, as you've already guessed ;)
On 7/4/08, poupou <[EMAIL PROTECTED]> wrote:
>
>
> On Jul 4, 3:10 am, Lev <[EMAIL PROTECTED]> wrote:
> > 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?
>
>
> Just like JB said...
>
> Another source for both a resolver[1] (based on the one JB linked to)
> and code to compute depth[2] can be found in Gendarme.
>
> [1]
> http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/framework/Gendarme.Framework/AssemblyResolver.cs?view=markup
> [2]
>
> http://anonsvn.mono-project.com/viewcvs/trunk/mono-tools/gendarme/rules/Gendarme.Rules.Maintainability/AvoidDeepInheritanceTreeRule.cs?view=markup
>
>
> > 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.
>
>
> Well you'll probably end up loading (at least into memory) quite a few
> assemblies in the resolving process.
>
> Sebastien
> >
>
--
Best Regards,
Lev Danielyan
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---