3

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.

Ufuk Can Bicici
  • 3,589
  • 4
  • 28
  • 57

1 Answers1

4

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 } 
Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • Thanks for the quick answer. Going one step further, is it possible to obtain Attribute parameters as well? For example if I have a Attribute `[TagAttribute("Class Info")] class MyClass { }` defined on my class, is it possible to obtain the string "Class Info" somehow with using CqLinq? – Ufuk Can Bicici Jul 14 '15 at 11:18
  • Not yet possible, this is the NDepend User Voices linked in the response, don't hesitate to vote for it and comment. http://ndepend.uservoice.com/forums/226344-ndepend-user-voice/suggestions/5544407-being-able-to-read-attribute-property-values – Patrick from NDepend team Jul 14 '15 at 14:14