首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >端口NDepend CQLINQ查询到c# LINQ

端口NDepend CQLINQ查询到c# LINQ
EN

Stack Overflow用户
提问于 2013-10-05 20:24:37
回答 1查看 255关注 0票数 1

是否可以将CQLinq查询移植到简单的C# LINQ查询?

我正在使用NDepend API创建一个代码分析器工具,我想使用CQLinq查询。

有些很容易移植。例如,

代码语言:javascript
复制
from m in Methods
where m.ILCyclomaticComplexity > 10 
orderby m.ILCyclomaticComplexity descending
select new { m }

很容易移植到

代码语言:javascript
复制
using NDepend.CodeModel;
using NDepend.CodeQuery;

public List<IMethod> GetUnitTestFromType(ICodeBase codeBase)
{
    var complexMethods = (from m in codeBase.Application.Methods
                      where m.ILCyclomaticComplexity > 10
                      orderby m.ILCyclomaticComplexity descending
                      select m).ToList();
    return complexMethods;
} 

但是我想使用更强大的CQLinq方法,即AllowNoMatch()

代码语言:javascript
复制
from t in codeBase.Application.Types
where t.Implement("System.IDisposable".AllowNoMatch())
select t;

实际上,直接使用CQLinq查询是很好的。多么?

我可以看到有一个NDepend.CodeQuery命名空间,它包含CreateQuery、编译和执行等方法。有人能给我看一下用法吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-07 08:14:03

实际上,CQLinq提供了许多在命名空间NDepend.Reserved.CQLinq中定义的方便的扩展方法。这些扩展方法在CQLinq编译后得到特殊处理,在C#中无法使用。

在CQLinq查询中编写时:t.Implement("System.IDisposable".AllowNoMatch())

解决了...the特殊的ExtensionMethodsCQLinqDependency.Implement()扩展方法。CQLinq后C#编译/执行前步骤尝试在执行之前解析指定为字符串的类型( "System.IDisposable".AllowNoMatch() ),并在IType上推断谓词。

  • 如果没有找到完整命名的"System.IDisposable"类型,则始终返回false
  • 如果找到一个名为"System.IDisposable"的类型,则返回true。用于实现它的类型。

ExtensionMethodsCQLinqDependency.Implement()的文档中,指出只能在ICQLinqExecutionContext__中调用该方法,否则必须调用方法NDepend.CodeModel.IType.NDepend.CodeModel.IType.Implement

因此,通过使用NDepend.API,您必须自己完成CQLinq后编译工作,但这是非常直接的:

代码语言:javascript
复制
var iDisposable = codebase.Types.SingleOrDefault(t.FullName == "System.IDisposable");
if(iDisposable == null) {
  return new IType[0];
}
return from t in codeBase.Application.Types
       where t.Implement(iDisposable)
       select t;

我可以看到有一个NDepend.CodeQuery命名空间,它包含CreateQuery、编译和执行等方法。有人能给我看一下用法吗?

实际上,使用NDepend.API,您可以编译一个CQLinq查询字符串,执行它并使用结果。示例用法可在带有$NDependRedistributable$\NDepend.PowerTools\CodeQueryConsole\CodeQueryConsolePowerTool.cs CQLinq的OSS 查询代码中使用

代码语言:javascript
复制
     var codeBase = analysisResult.CodeBase;
     Func<string, IQueryCompiled> compileQueryProc = queryString => queryString.Compile( codeBase);

     // ... but if we can get a compareContext, then compile and execute the query against the compareContext
     ICompareContext compareContext;
     if (ProjectAnalysisUtils.TryGetCompareContextDefinedByBaseline(analysisResult, out compareContext)) {
        Debug.Assert(compareContext != null);
        compileQueryProc = queryString => queryString.Compile(compareContext);
     }

...
       IQueryCompiled queryCompiled;
       using (var queryEditSession = new QueryEditSession(queriesPreviouslyEdited)) {
           var queryString = queryEditSession.GetQueryString();
COMPILE_QUERY:
           Console.BackgroundColor = ConsoleColor.Black;
           Console.ForegroundColor = ConsoleColor.White;
           if (queryString == null) { break; }

           // Try compile query
           queryCompiled = compileQueryProc(queryString);
           var queryCompiledError = queryCompiled.QueryCompiledError;
           if (queryCompiledError != null) {
              queryString = queryEditSession.ShowCompilatioErrorsAndThenGetQueryString(queryCompiledError);
              goto COMPILE_QUERY;
           }
        }

        // Execute query compiled
        var queryCompiledSuccess = queryCompiled.QueryCompiledSuccess;
        Debug.Assert(queryCompiledSuccess != null);
        var result = queryCompiledSuccess.Execute();
        if (result.Status != QueryExecutionStatus.Success) {
           var exception = result.Exception;
           // The error must be an Exception thrown by the query, since we don't use the Execute(...) overload with time-out!
           Debug.Assert(exception != null);
           DisplayQueryThrowAnException(exception);
           continue;
        }

        QueryExecutionResultDisplayer.Go(result.SuccessResult);
        Console.WriteLine();
     }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19202349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档