【问题标题】:Detecting direct instantiation with nDepend使用 nDepend 检测直接实例化
【发布时间】:2014-07-09 13:53:07
【问题描述】:

使用 nDepend API,是否可以实现以下操作?

我想留意我们的对象工厂被绕过并且直接实例化具体类的情况。

显然我需要能够过滤掉以下内容:

StringBuilder stringBuilder = new StringBuilder();

也许可以通过在 Where 子句中添加要排除的类型名称或要检查的命名空间,但我想确保我们看到:

IMyCustomType item = ObjectFactory.Get<IMyCustomType>();

不是这个:

MyCustomType item = new MyCustomType();

谢谢。

【问题讨论】:

    标签: ndepend


    【解决方案1】:

    也许下面这样的代码规则可以帮助你,希望它是可以理解的,不必评论它:

    warnif count > 0
    
    let ctors = Application.Namespaces.WithNameLike("Namespaces1*").ChildMethods().Where(m => m.IsConstructor)
    
    let codeThatMustNotCallCtors = Application.Namespaces.WithNameLike("Namespaces2*").ChildMethods()
    
    from m in codeThatMustNotCallCtors.UsingAny(ctors)
    select new { m, ctorsCalled = m.MethodsCalled.Intersect(ctors ) }
    

    【讨论】:

    • 这正是我所需要的;感谢您的示例代码。可悲的是,我还没有声望支持你的答案。不过我会的。
    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多