是否可以将CQLinq查询移植到简单的C# LINQ查询?
我正在使用NDepend API创建一个代码分析器工具,我想使用CQLinq查询。
有些很容易移植。例如,
from m in Methods
where m.ILCyclomaticComplexity > 10
orderby m.ILCyclomaticComplexity descending
select new { m }很容易移植到
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()
from t in codeBase.Application.Types
where t.Implement("System.IDisposable".AllowNoMatch())
select t;实际上,直接使用CQLinq查询是很好的。多么?
我可以看到有一个NDepend.CodeQuery命名空间,它包含CreateQuery、编译和执行等方法。有人能给我看一下用法吗?
谢谢!
发布于 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后编译工作,但这是非常直接的:
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 查询代码中使用
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();
}https://stackoverflow.com/questions/19202349
复制相似问题