Tim
Tim

Reputation: 2917

fxcop custom rules - Inspecting source code to look for new keyword

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

Upvotes: 2

Views: 746

Answers (2)

Anand Patel
Anand Patel

Reputation: 6431

This guy has explained well

http://www.guysmithferrier.com/downloads/FxCop.pdf

Upvotes: 1

Nicole Calinoiu
Nicole Calinoiu

Reputation: 21002

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

Upvotes: 1

Related Questions