我想避免用新的实例化某些类,而强迫使用工厂类。

但是我不知道该怎么做。

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

预先感谢您的帮助,
最好的祝福

最佳答案

这是应该让您入门的东西。您将需要添加自己的逻辑来确定是否应允许通过实例化任何给定类型的实例。

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

关于c# - fxcop自定义规则-检查源代码以查找新关键字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1750323/

10-11 07:29