views:

27

answers:

1

Hello

I have base type "Base" in assembly "A" and derived type "Child" in assembly "B".

I'm using FxCop to find in assembly "B" types derived from "Base" like this:

var baseAssemblyNode = AssemblyNode.GetAssembly("A.dll"), true, true, false);
var baseType = baseAssemblyNode.Types.Single(t => t.Name.Name == "Base");

var assemblyNode = AssemblyNode.GetAssembly("B.dll"), true, true, false); 
var derivedTypes = assemblyNode.Types.Where(t => t.IsDerivedFrom(baseType));

But the result collection is empty.

I found type "Child" manually and saw that it's base type is not equal to found "Base" type.

It was expected cause these type nodes are different objects, but in depth in Name property they have equal UniqueKey and Name - I expected that FxCop performs comparison for types in IsDerivedMethod by these properties. But it doesn't work.

What are the options to solve this problem?

A: 

If the types are related by a reference this should do the trick:

public override ProblemCollection Check(TypeNode type)
    {
        var winFormsReference=type.DeclaringModule.ContainingAssembly.AssemblyReferences
            .SingleOrDefault(ar => ar.Assembly.Name.StartsWith("System.Windows.Forms"));
        if (winFormsReference==null)
            return null;
        var controlType=winFormsReference.Assembly.Types.Single(t => t.FullName=="System.Windows.Forms.Control");
        _currentTypeFullName=type.FullName;
        if (type.IsDerivedFrom(controlType)==false||_shouldCheckType(type)==false)
            return null;

        var initializer = type.Members.OfType<Method>( ).SingleOrDefault(x => x.Name.Name=="InitializeComponent");
        if (initializer==null)
            Problems.Add(new Problem(NotSetResolution( ), type));
        else
            VisitMethod(initializer);
        return Problems;
    }

This is what I am using to find all classes that inherit from System.Windows.Forms.Control and then ensure certain properties are being set to the team standards in the InitializeComponent().

Maslow