【问题标题】:Query that counts new-operators计算新运算符的查询
【发布时间】:2013-01-21 12:16:45
【问题描述】:

我正在尝试使用 NDepend 创建另一个自定义查询,但无法弄清楚。

这是我想查询的伪代码:

var list
foreach type t
    int newCount = 0
    foreach type u in t.TypesUsed
        if "new"-operator of u is called anywhere within t
            newCount++;
    end foreach
    list.Add( new Tuple<Type, int>(t, newCount) )
end foreach
return list    

我想知道“new”操作符在一个类型中的任何位置被调用了多少次。

我对 NDepend 查询的语法非常陌生。所以一些提示会很有帮助:)

谢谢!

【问题讨论】:

    标签: count new-operator ndepend


    【解决方案1】:

    你可以试试这个查询,它列出了t 的每个类型,所有在t 代码中实例化的类型(即,当通过new 运算符调用其构造函数之一时实例化一个类型)。

    from t in JustMyCode.Types
    let typesInstantiated = from tUsed in t.TypesUsed 
                            where tUsed.Constructors.Any(c => c.IsUsedBy(t))
                            select tUsed
    where typesInstantiated.Count() > 0
    select new { t, typesInstantiated }
    

    结果如下:

    【讨论】:

    • 我应该能够修改它,以便它检测任何构造函数调用(而不是仅默认构造函数)。非常感谢!
    • 不客气,顺便说一句,我只是使用 Any() 而不是 FirstOrDefault() 简化了查询!= null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多