Checking Inheritance Heirarchy

Hi,

I am using the following code to check the inheritance heirarchy.

public override ProblemCollection Check(TypeNode type)
{
int baseCount = 0;
while ((type = type.BaseType) != null)
{
baseCount++;
}
}

I have the sample (Test Program for this rule) as a Windows Application. But some what I am not sure why, it enters the loop only for the Program.cs file and not for the other referenced files (Checked it by adding Event Log Statements). Is the logic mentioned above enough

Thanks and Regards

PrashG



Answer this question

Checking Inheritance Heirarchy

  • baaba

    Hi,

    Yes, I did try your suggestions. But it did not work. I tried running aginst an Application Block and also a custom sample. Will you please check this with some sample you may have and let me know

    Thanks and Regards

    Prash


  • Francois JEAN

    Hi,

    Thanks for your help in this. It is working absolutely fine. Now I have another small issue in this. We have a company policy that the inheritance heirarchy should not be more than 3. This works fine for normal classes. However when it comes to Win Forms, it is becoming a problem as Win Form itself has 4 levels of inheritance heirarchy. My question is, is there any way we can exclude the Framework classes from being counted.

    Thanks and Regards

    PrashG


  • gogoYang

    Hi PrashG,

    My guess is that your other files are internal types By default rules don't analyze internal code, add the following property to your rule to make your rule run on everything:



    public override TargetVisibilities TargetVisibility
    { get { return TargetVisibilities.All; }}

    Regards,

    Jeffrey


  • Sneha

    There is nothing wrong with your code, so you need to dig deeper. I suggest you step through the code carefully in the debugger. Or insert a breakpoint in source at the specific point you believe is not getting hit, in order to verify that point, eg:

    namespace Microsoft.FxCopRuleSamples.Test
    {
    [CLSCompliant(false)]
    sealed public class CountsObjectLevels: IntrospectionTestRule
    {
    public CountsObjectLevels() : base("CountsObjectLevels") { }

    public override TargetVisibilities TargetVisibility
    { get { return TargetVisibilities.All;}}


    public override ProblemCollection Check(TypeNode type)
    {
    int baseCount = 0;
    while ((type = type.BaseType) != null)
    {
    System.Diagnostics.Debugger.Break();
    baseCount++;
    }
    return null;
    }
    }
    }

    btw - the next version of FxCop contains a rule that flags deeply nested object hierarchies.



  • Brian Fink

    You could replace your while statement with something like this:

    bool search = true;
    while(search)
    {
    type = type.BaseType;
    if(null != type)
    {
    if(type.FullName.Name != "Microsoft.GoNoLowerThan.ThisTypeInHeirachy")
    {
    // Do stuff
    }
    else
    {
    search = false;
    }
    }
    else
    {
    search = false;
    }
    }

    Clearly you can add more cases to the FullName comparison. Note - I can't remember if FullName needs the Name property to be accessed, you might just go type.FullName.
    while ((type = type.BaseType) != null)

  • Guber

    The way we handle this internally is to compare the Type.DeclaringModule against the Type.BaseType.DeclaringModule. When we change an assembly boundary, we stop analyzing. In your case, you could compare the location of the comparing module and stop analyzing when that location is identical to the location of mscorlib.dll, which would indicate analysis has dipped into a redist binary. Mscorlib is exposed via SystemTypes.SystemAssembly.

  • Pon_teh_pony

    Hi PrashG,

    Did you try the suggestion I gave earlier If so, do you reach the method begin

    Regards,

    Jeffrey


  • James E Freedle II

    Hi,

    I have the following code to do the check the inheritance heirarchy and it is not entering the condition (while loop).

    public override ProblemCollection Check(TypeNode type)

    {

    int depthCount = 0;

    if (type == null)

    return null;

    while ((type = type.BaseType) != null)

    {

    EventLog.WriteEntry("Application", "Check " + type.BaseType.Name.Name.ToString());

    depthCount++;

    }

    if (depthCount > INHERITANCE_HIERARCHY)

    {

    Problems.Add(new Problem(GetResolution(RuleUtilities.Format(type), depthCount.ToString(), INHERITANCE_HIERARCHY.ToString())));

    }

    return Problems;

    }

    Will you please let me know the probable mistake in this code

    Thanks and Regards

    PrashG


  • Checking Inheritance Heirarchy