ndepend

NDepend: Find fields that are either a given type or use a given type in their generic parameters


How would I go about using NDepend to not only identify JustMyCode.Fields that are exactly a given type, but also indirectly, i.e. fields like IList<MyType>, IDictionary<int, MyType>, Lazy<T> and all those "nice" generic variants/usages?

Is there any helper method similar to .UsedBy(...) available by any chance that provides such a functionality?


Solution

  • Here is a query to get field typed with String or Int32:

    let types = Types.WithFullNameIn(
       "System.String", 
       "System.Int32").ToArray()
    from f in Application.Fields
    where !f.ParentType.IsEnumeration &&
           f.FieldType != null &&
           types.Contains(f.FieldType)
    select new { f, type =f.FieldType }
    

    For now you cannot detect when a type is used in a generic parameter.

    Querying with NDepend fields of a given type