Find code using [Obsolete] marked code of a list of assemblies using NDepend

41 Views Asked by At

I am currently trying to use NDepend to identify (our) code that uses [Obsolete] marked members of a particular group of referenced assemblies (in my example starting with "Microsoft.TeamFoundation.*") and by "our code" I mean members of ours calling / using [Obsolete] members in said list of reference assemblies. Ideally I would get a precise list of our Type and their members but I would also be fine with just our types if I had the actual count of those [Obsolete] member usages inside those types

The idea is to do a one time analysis of the quantity of [Obsolete] member calls/usages we have, then integrate that into a CI pipeline going forward to track the reduction of that over time.

Any guidance or sample code doing something like that would be highly appreciated.

1

There are 1 best solutions below

0
Patrick from NDepend team On BEST ANSWER

You can try this code query:

let obsoleteTMF = TypesAndMembers.Where(x => x.IsObsolete).ToHashSetEx()
from c in Application.Types.Concat<IUser>(Application.Methods).UsingAny(obsoleteTMF)
select new {
typeOrMethod = (ICodeElement)c,
obsoleteUsed = c.IsType? 
   c.AsType.TypesUsed.Intersect(obsoleteTMF) :

   c.AsMethod.MethodsCalled
   .Concat<IMember>(c.AsMethod.FieldsUsed)
   .Intersect(obsoleteTMF)
}

Thanks to calls to Concat() this query handles both types and methods at the same time. .ToHashSetEx() is used to speed up calls to Intersect().

It works this way:

NDepend code query to match obsolete typoe, methods and fields usage