Question

I'd like to have the nightly build check for how many NotImplementedExeptions there are in my .NET code so hopefully we can remove them all before releasing. My first thought is that FxCop might be a good tool to do this. Does anyone have a custom FxCop rule for this? How would I go about creating one myself?

Was it helpful?

Solution

I've actually implemented one and shown the code in this answer.

OTHER TIPS

Unit test like this will fail if more than 10 methods create NotImplementedException. On failing it will report all methods that create this exception.

var throwingMethods = codebase.Methods
  .Where(m => m
         .GetInstructions()
         .Exists(i => i.Creates<NotImplementedException>()))
  .ToArray();

if (throwingMethods.Length > 10)
  CollectionAssert.IsEmpty(throwingMethods);

Where codebase is created like this:

var codebase = new Codebase("Assembly1.dll","Assembly2.dll");

Snippet uses Lokad.Quality.dll from the Lokad Shared Libraries.

There's tons of resources on the net about how to make your own FxCop rule, but since 'NotImplementedException' is such a unique name, you could probably just do a count of its occurence in the text of the files. Create an ItemGroup for all the .cs files and then use an appropriate DOS command (such as "find /c") and get the count as the output, via the EXEC task.

There are times when a NotImplementedExeption should be thrown -- one example would be if you've a partial implementation of IBindingList.

There are plenty of resources out there for creating custom fxCop rules. However, a "find in solution" might be a suitable approach (on the assumption that you're not needing this in an automated build)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top