fxcop 自定义规则 - 检查源代码以查找新关键字

发布于 2024-08-11 16:44:09 字数 97 浏览 2 评论 0原文

我想避免用 new 实例化某些类,并强制使用工厂类。

但我不明白该怎么做。

有人可以给我看一些样品吗?

预先感谢您的任何帮助, 此致

I would like to avoid instanciating certain class with new, and force to use the factory class.

But I don't understand how to do that.

Can someone show me a little sample ?

Thanks in advance for any help,
Best regards

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

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

发布评论

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

评论(2

╰沐子 2024-08-18 16:44:09

这里有一些可以帮助您入门的内容。您将需要添加自己的逻辑来确定是否应允许通过新建来实例化任何给定类型的实例。

public override ProblemCollection Check(Member member)
{
    if (member is Method)
    {
        this.Visit(member);
    }

    return this.Problems;
}

public override void VisitConstruct(Construct construct)
{
    base.VisitConstruct(construct);

    if (!this.AllowTypeToBeNewed(construct.Type))
    {
        this.Problems.Add(new Problem(this.GetResolution(), construct));
    }
}

private bool AllowTypeToBeNewed(TypeNode type)
{
    throw new NotImplementedException();
}

Here's something that should get you started. You will need to add your own logic for determining whether instances any given type should be allowed to instantiated via newing.

public override ProblemCollection Check(Member member)
{
    if (member is Method)
    {
        this.Visit(member);
    }

    return this.Problems;
}

public override void VisitConstruct(Construct construct)
{
    base.VisitConstruct(construct);

    if (!this.AllowTypeToBeNewed(construct.Type))
    {
        this.Problems.Add(new Problem(this.GetResolution(), construct));
    }
}

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