I am trying to use NDepend and its Linq based query language to generate some reports about the code's current state. I want to label some of my methods and classes with predefined "tags", for example the methods labeled with tag "Database" contains database specific code, the ones labeled with tag "Algorithm_X" contains specific logic related to an algorithm "X". We think that such a tagging procedure will lead to a more straightforward documentation generation process.
I wonder whether NDepend provides a mechanism which facilitates such a process. I am thinking of using C# Attributes to generate such custom tags and then labeling methods with appropriate attributes which corresponds to "tagging" them. I am well aware of the ".HasAttribute" method of CQLinq and actively using it. But this tagging procedure needs a way to list or enumerate all Attributes attached to a method and I failed to realize it using NDepend until now.
My question is; is there a way to implement that (listing all attributes of a method) in NDepend? If not, is there another way in NDepend which would facilitate such a tagging procedure? I may implement this using Reflections by writing custom C# code, but I want to be sure that I am out of options with NDepend at this state.
You can actually lists attributes tagging a method with NDepend LINQ code querying (CQLINQ) but it is not straightforward nor fast. We plan to improve attributes support in the NDepend code model, it has been asked on the NDepend User Voice.
So the following query works but it can take a few second on a large code base (which is slow for NDepend where typically queries are executed in a few milliseconds):
let typesAttributes = Types.Where(t => t.IsAttributeClass)
from m in Methods
let mAttributes = typesAttributes.Where(t => m.HasAttribute(t)).ToArray()
where mAttributes .Length > 0
select new { m, mAttributes }
The optimization below will make it run twice faster in general.
let typesAttributes = Types.Where(t => t.IsAttributeClass)
from m in Types.UsingAny(typesAttributes).ChildMethods()
let mAttributes = typesAttributes.Where(t => m.HasAttribute(t)).ToArray()
where mAttributes .Length > 0
select new { m, mAttributes }