繁体   English   中英

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

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

仅由于使用NDepend 规则/API的 typeof 组合,我如何识别这两种类型之间的依赖关系。

在下面的示例中,类型仅通过typeof(UsedDependendType)依赖于UsedDependendType ,没有其他成员参与耦合。

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

我能够确定这些类型之间的依赖关系中是否涉及任何成员。 但是为了确认我需要知道是否有对 typeof(UsedDependendType) 的调用。

使用 NDepend API/CQL 如何识别方法中的调用typeof(UsedDependendType)

问候巴桑特

目前还没有办法将typeof(TUsed)与其他用法分开。

您可以尝试并可能细化此查询,其中列出了依赖项中不涉及任何成员的类型对(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