检测引用其他页面的 Asp.Net 页面(使用 NDepend?FXCop?)

发布于 2025-01-05 16:26:32 字数 637 浏览 5 评论 0原文

我需要找到从“System.Web.UI.Page”派生的所有类,这些类引用从 Page 派生的另一个类...而我在 NDepend 中尝试的所有内容要么什么也没有给我,要么给我每个页面。

我们最近将 Asp.Net“站点”转换为“Web 应用程序”,在此过程中发现许多页面与数据库层中的类具有相同的类名。它们位于不同的命名空间中,但由于所有页面都位于同一命名空间中(现在位于同一个程序集中),因此我们遇到了一些问题。

大多数情况下,当页面使用数据层类并访问其成员时,这会导致编译错误,但现在所有页面都在 Web 应用程序中(因此可以互相看到),编译器决定它们正在引用 Web页面类而不是数据层类。我们担心某些类可能通过实现具有相同名称的成员而跳过了编译时崩溃,因此我们希望找到 .Net 认为网页(继承(无论多么遥远)的类)的任何位置来自 System.Web.UI.Page)具有对另一个网页的引用。

显然这个 NDepend CQL 只是返回所有页面:

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

有谁知道这是否可能,或者我是否可以使用 FXCop 或其他东西编写规则?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2025-01-12 16:26:33

我不知道有什么方法可以使用 CQL 来做到这一点,尽管我还不够 CQL 专家来确定这是不可能的。另一方面,使用FxCop来发现此类问题是相当可行的。以下规则代码应该可以找到您的问题,尽管它实际上只适合一次性筛选。如果您正在寻找永久包含在 FxCop 分析中的内容,则需要对其进行一些修饰。

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);
}

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);
}
誰認得朕 2025-01-12 16:26:33

我们希望找到 .Net 认为网页(继承自 System.Web.UI.Page 的类(无论距离有多远))引用另一个网页的任何位置。

Jaykul,这可以通过以下通过 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 }

此外,您写道:

我们发现很多页面与数据库层中的类具有相同的类名

NDepend 提出的不仅仅是 200 个默认代码规则,其中之一是 避免不同类型具有相同的名称

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文