nDepend - 如何使用nDepend API修改“JustMyCode”查询?

时间:2014-11-13 10:57:13

标签: ndepend

我的目标是使用nDepend API修改“JustMyCode”查询。我使用的代码如下:

var justMyCodeGroup = prj.CodeQueries.CodeQueriesSet.ChildGroups.Single(x => x.Name.Contains("JustMyCode"));
var originalQuery = justMyCodeGroup.ChildQueries
            .Single(x => x.QueryString.Contains("Discard generated Types from JustMyCode"));
var changedQuery = originalQuery.Controller.CreateQuery(originalQuery.IsActive,
            query,
            originalQuery.
                DisplayStatInReport,
            originalQuery.DisplayListInReport,
            originalQuery.DisplaySelectionViewInReport,
            originalQuery.IsCriticalRule);

var justMyCodeGroupWithModifiedQuery = justMyCodeGroup.ReplaceQuery(originalQuery, changedQuery);
 prj.CodeQueries.CodeQueriesSet.ReplaceGroup(justMyCodeGroup, justMyCodeGroupWithModifiedQuery);

但是,当我运行上面的代码时,我得到带有消息的ArgumentException:

  

newGroup.Controller与此groupOfGroups.Controller不同

任何帮助?

更新1: 我也试过代码:

       var justMyCodeGroup = prj.CodeQueries.CodeQueriesSet.ChildGroups.Single(x => x.Name.Contains("JustMyCode"));
        var originalQuery = justMyCodeGroup.ChildQueries
            .Single(x => x.QueryString.Contains("Discard generated Types from JustMyCode"));
        var changedQuery = originalQuery.Controller.CreateQuery(originalQuery.IsActive,
            query,
            originalQuery.
                DisplayStatInReport,
            originalQuery.DisplayListInReport,
            originalQuery.DisplaySelectionViewInReport,
            originalQuery.IsCriticalRule);

        var justMyCodeGroupWithModifiedQuery = justMyCodeGroup.ReplaceQuery(originalQuery, changedQuery);

        var newQueries = new List<IQuery>();
        foreach (var q in justMyCodeGroup.ChildQueries)
        {
            if (q.QueryString.Contains("Discard generated Types from JustMyCode"))
            {
                continue;
            }

            newQueries.Add(prj.CodeQueries.CodeQueriesSet.Controller.CreateQuery(q.IsActive, q.QueryString,
                q.DisplayStatInReport, q.DisplayListInReport, q.DisplaySelectionViewInReport, q.IsCriticalRule));
        }
        newQueries.Add(prj.CodeQueries.CodeQueriesSet.Controller.CreateQuery(originalQuery.IsActive, query, originalQuery.DisplayStatInReport, originalQuery.DisplayListInReport, originalQuery.DisplaySelectionViewInReport, originalQuery.IsCriticalRule));

        var newGroup = prj.CodeQueries.CodeQueriesSet.Controller.CreateGroup(justMyCodeGroup.Name,
            justMyCodeGroup.IsActive, justMyCodeGroup.ShownInReport, newQueries, new List<IGroup>());

        prj.CodeQueries.CodeQueriesSet.RemoveGroup(justMyCodeGroup);
        prj.CodeQueries.CodeQueriesSet.AddGroup(newGroup);

现在,RemoveGroup抛出异常:

  

这组组不包含groupToRemove。

更新2: 而且我也想知道,为什么这段代码会返回false?

 var justMyCodeGroup = prj.CodeQueries.CodeQueriesSet.ChildGroups.Single(x => x.Name.Contains("JustMyCode"));
 prj.CodeQueries.CodeQueriesSet.ContainsGroup(justMyCodeGroup)

1 个答案:

答案 0 :(得分:0)

请参阅PowerTools源文件:

$NDependInstallDir$\NDepend.PowerTools.SourceCode\CQL2CQLinq\CQL2CQLinqPowerTool.cs

此PowerTools将使用旧CQL语法编写的代码查询转换为使用新CQLinq语法编写的代码查询,因此它加载项目中的查询集,更新CQL查询,然后保存项目中设置的新查询。

queriesController以这种方式聚集......

     var queriesSet = project.CodeQueries.CodeQueriesSet;
     var queriesController = queriesSet.Controller;

...然后用这种方式修改查询集:

queriesController.DoUpdateQueryObject(query, newQuery);