Skip generated types when performing analysis in NDepend

Code created in more recent versions of C# has a lot of generated types, even if you don’t use code generation explicitly. Interators (the yield keyword), and anonymous delegates both use generated types underneath. Also those neat anonymous types introduced in C# 3.0 are nothing more than a compiler magic.

All this may clutter your NDepend window of choice, when looking at your projects. You can however get rid of generated stuff, pretty easily, with this simple CQL query:

// <Name>Types not generated by the compiler</Name>
SELECT TYPES FROM ASSEMBLIES "YourAssembly" 
WHERE !(HasAttribute "System.Runtime.CompilerServices.CompilerGeneratedAttribute")

Simple, but useful.

If you want to get rid of just some types of generated classes, for example you want to get rid of iterators, but want to leave anonymous delegate classes, you may use regular expressions for that. This is my clumsy attempt to that:

// <Name>Types not generated by the compiler</Name>
SELECT TYPES FROM ASSEMBLIES "Castle.Facilities.WcfIntegration" WHERE 
!NameLike ".+<>c__.+" /* Anonymous delegates, like private sealed class <>c__DisplayClass1
*/ AND 
!NameLike ".+\+<.*>d__.+" /* Iterators, like private sealed class <FindDependencies>d__0<T> :
IEnumerable<T>, IEnumerable, IEnumerator<T>, IEnumerator, IDisposable */

Technorati Tags: , ,