How to identify coupling between types due to typeof composition using NDepend API's?

45 Views Asked by At

How can I identify the dependencies between the two types only due to typeof composition using NDepend rules/API.

In the below example the type is dependent on UsedDependendType only through typeof(UsedDependendType), no other members are involved in the coupling.

class OnlyTypeOfDependencyBetweenTypes
{
    public IEnumerable<Type> Initialize()
    {
        return new List<Type> {
            typeof(UsedDependendType)
        };
    }
}

I am able to identify whether there are any members involved in the dependency between these types. But for confirming I need to know whether there is a call to typeof(UsedDependendType) is there or not.

Using NDepend API/CQL how can I identify the call typeof(UsedDependendType) in a method?

Regards Basanth

1

There are 1 best solutions below

1
Patrick from NDepend team On

For now there is no way to segregate typeof(TUsed) over other usages.

You can try and maybe refine this query that list pairs of types (t,tUsed) where there is no member involved in the dependency.

from t in Application.Types
// No typeof() in interface
where !t.IsInterface

from tUsed in t.TypesUsed
// Don't count third-party
where !tUsed.IsThirdParty
// Don't count tUsed when it is an attribute
where !tUsed.IsAttributeClass
where !t.Implement(tUsed)

// Don't enumerate both (A,B) and (B,A)
where string.Compare(tUsed.Name, t.Name) < 0

// tUsed is not used as a return type nor a field type
where !t.Methods.WithReturnType(tUsed).Any()
where !t.Fields.WithFieldType(tUsed).Any()

// No member of tUsed is used by t
where !tUsed.Members.UsedByAny(t.Methods).Any()

select new { t, tUsed }