简体   繁体   中英

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.

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 自定义代码规则命名空间应以

Disclaimer: I work at NDepend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM