I know there are tools to count the number of classes i use in my code base, and this is a reasonable software engineering metric (e.g. average lines of code per class).
However i was idly wondering whether there are any tools to count the number of anonymous classes in the code base?
If so would this make an interesting software quality metric? e.g. i would probably be worried if i had more anonymous classes than normal classes though I’m not sure if i should be.
I don’t think the number of anonymous types is a useful metric, whether by itself or when compared with the number of named types. I also don’t know about any tools that measure this (that doesn’t mean they don’t exist, though).
To find out the number of anonymous types in a compiled assembly, you could use these facts about compiled anonymous types:
[CompilerGenerated]attribute<>f__AnonymousType(an unspeakable name in C#)[DebuggerDisplay]attribute withType="<Anonymous Type>"#1 by itself is not enough to identify an anonymous type. And I think you can’t rely on #2 or #3, since MS can decide to change them at any time and other compilers, like Mono C#, can use another style. But I think it’s the best you can do. Using #1 and #2, you could write a method that does what you want like this:
If you wanted to find out the number of anonymous files in a bunch of source code files, you would need C# parser. If you are willing to use pre-release software, I think this would be a good job for Roslyn, except it doesn’t support anonymous types yet.