Can I tell NDepend to ignore a single result?

2019-06-20 04:17发布

问题:

I'm running NDepend against my C# project in VS 2012. There is a particular "critical rule violation" that I have looked at and have decided that I want to leave as-is. I want to tell NDepend to ignore this one violation and NOT count it against my critical rule violation count, so that I can get the dot at the bottom to be no longer red.

If it matters: It's a "function with too many parameters". The purpose of function is to make a log entry into a database table, and I need to pass in all of the values (11 of them) to populate the table (after applying some logic to them). I suppose I could put all the values into a struct or class and pass that in as a single value, but there would be no other use for the struct/class than to call this one function, so that seems kind of silly to me.

So, I was looking for a way to just do something like right-clicking on the result and choosing "permanently ignore this result", like you might do with a spell checker, but I don't see any such option.

Is there a way to do this?

I don't want to exclude the entire rule or modify it to increase the max number of parameters, as there may be other instances where I DO agree that the function should be modified.

回答1:

You can modify the CQLinq rule body this way:

// <Name>Methods with too many parameters</Name>
warnif count > 0 from m in JustMyCode.Methods
.WithFullNameNotIn("MyNamespace.MyType.MyMethod1(String,Int32,Int32,Boolean,String,Int32,Int32,Boolean,String,Int32,Int32,Boolean)", 
                   "MyNamespace.MyType.MyMethod2(String,Int32,Int32,Boolean,String,Int32,Int32,Boolean,String,Int32,Int32,Boolean)",
                   ...)
where 
  m.NbParameters > 5 
  orderby m.NbParameters descending
select new { m, m.NbParameters }

Notice that the rule body contains the method(s) to exclude full names. To get the method full name, just right click the method in the result and choose the menu Copy name to clipboard.

Alternatively, if you wish to suppress a method from all rules results, you can use the JustMyCode/notmycode facility.