简体   繁体   English

如何使用 NDepend API 识别由于 typeof 组合而导致的类型之间的耦合?

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

How can I identify the dependencies between the two types only due to typeof composition using NDepend rules/API .仅由于使用NDepend 规则/API的 typeof 组合,我如何识别这两种类型之间的依赖关系。

In the below example the type is dependent on UsedDependendType only through typeof(UsedDependendType) , no other members are involved in the coupling.在下面的示例中,类型仅通过typeof(UsedDependendType)依赖于UsedDependendType ,没有其他成员参与耦合。

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.但是为了确认我需要知道是否有对 typeof(UsedDependendType) 的调用。

Using NDepend API/CQL how can I identify the call typeof(UsedDependendType) in a method?使用 NDepend API/CQL 如何识别方法中的调用typeof(UsedDependendType)

Regards Basanth问候巴桑特

For now there is no way to segregate typeof(TUsed) over other usages.目前还没有办法将typeof(TUsed)与其他用法分开。

You can try and maybe refine this query that list pairs of types (t,tUsed) where there is no member involved in the dependency.您可以尝试并可能细化此查询,其中列出了依赖项中不涉及任何成员的类型对(t,tUsed)

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 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM