문제

I need to find all the classes derived from "System.Web.UI.Page" which reference another class derived from Page ... and everything I try in NDepend gives me either nothing, or every page.

We recently converted an Asp.Net "site" to a "web application," and in the process discovered that a lot of the pages had the same class name as classes in our database layer. They're in a different namespace, but since all the pages are in the same namespace (and now, are in the same assembly), we're having some problems.

Mostly, this caused compile errors when a page used a data layer class and accessed a member of it, but now that all the pages are in a Web Application (and thus, can see each other), the compiler decided they were referencing the web page class and not the data layer class. We're concerned that some classes might have slipped past the compile-time crash by perhaps implementing a member with the same name, so we want to find any place where .Net thinks that a web Page (a class which inherits (however distantly) from System.Web.UI.Page) has a reference to another web page.

Obviously this NDepend CQL just returns all the pages:

SELECT TYPES FROM ASSEMBLIES "UI" 
WHERE IsUsing "System.Web.UI.Page"
AND DeriveFrom "System.Web.UI.Page"

Does anyone know if this is possible, or if I can write a rule using FXCop or something else?

도움이 되었습니까?

해결책

I am unaware of any way to do this using CQL, although I'm not enough of a CQL expert to be sure that it's not possible. On the other hand, it is quite feasible to find such problems using FxCop. The following rule code should find your problems, although it's really only suitable for a one-time screening. If you were looking for something to permanently include in FxCop analyses, it would need to be fancied-up a bit.

private TypeNode PageType { get; set; }

private TypeNode FocalType { get; set; }

public override void BeforeAnalysis()
{
    base.BeforeAnalysis();    
    this.PageType = FrameworkTypes.Page;
}

public override ProblemCollection Check(TypeNode type)
{
    if (type.IsDerivedFrom(this.PageType))
    {
        this.FocalType = type;
        this.Visit(type);
    }

    return this.Problems;
}

public override void VisitTypeReference(TypeNode type)
{
    if ((type != null) && (type != this.FocalType) && type.IsDerivedFrom(this.PageType) && (!this.FocalType.IsDerivedFrom(type)))
    {
        this.Problems.Add(new Problem(this.GetResolution(type.FullName), type));
    }

    base.VisitTypeReference(type);
}

다른 팁

we want to find any place where .Net thinks that a web Page (a class which inherits (however distantly) from System.Web.UI.Page) has a reference to another web page.

Jaykul, this can be achiever through the following Code Query over LINQ (CQLinq):

let pageTypes = Application.Assemblies.WithNameLike("UI")
                .Where(t => t.DeriveFrom "System.Web.UI.Page")

from t in pageTypes 
let pageTypesUsed = t.TypesUsed.Intersect(pageTypes)
where pageTypesUsed.Count() > 0
select new { t, pageTypesUsed }

Additionally you wrote:

we discovered that a lot of the pages had the same class name as classes in our database layer

NDepend proposes more than 200 default code rules, one of them is Avoid having different types with same name

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top