FxCop custom rule to check namespace

217 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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