Saket Kumar
Saket Kumar

Reputation: 4845

FxCop custom rule to check namespace

I am trying to write a custom rule in FxCop to validate if my namespace starts with a particular word. I have tried something like below:

    public override ProblemCollection Check(string namespaceName, TypeNodeCollection types)
    {
        if (namespaceName == null)
        {
            return this.Problems;
        }

        if (!namespaceName.StartsWith("FujiXerox.ApeosWare.", StringComparison.Ordinal))
        {
            this.Problems.Add(new Problem(this.GetNamedResolution("NamespaceResolution", namespaceName)));
        }

        return this.Problems;
    }

But it is not working. Can anyone please suggest how to write this custom rule correctly.

Upvotes: 2

Views: 207

Answers (1)

Patrick from NDepend team
Patrick from NDepend team

Reputation: 13842

I don't know with FxCop, but with NDepend (a .NET tool integrated in VS, that let's write custom code rules as C# LINQ queries) you just have to write:

// <Name>Namespace should start with XYZ</Name>
warnif count > 0 
from n in Application.Namespaces
where !n.Name.StartsWith("XYZ")
select n

The rule can be:

NDepend custom code rule Namespace should start with

Disclaimer: I work at NDepend

Upvotes: 1

Related Questions