我可以检测(使用roslyn)lambda体中的x
引用是对外部变量x
的闭包,而不是lambda本身的一些变量吗?
var x = "foo";
var a = string[0];
a.Any(i => i == x);
答案 0 :(得分:4)
烨。您可以使用DataFlowAnalysis
API。
var tree = CSharpSyntaxTree.ParseText(
@"
class C{
void M(){
var x = ""foo"";
var a = new string[0];
var testing = a.Any(i => i == x);
}
}
");
var Mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);
var lambda = tree.GetRoot().DescendantNodes().OfType<LocalDeclarationStatementSyntax>().Last();
var dataFlowAnalysis = model.AnalyzeDataFlow(lambda);
var capturedVariables = dataFlowAnalysis.Captured;
foreach(var variable in capturedVariables)
{
//Do something
}
答案 1 :(得分:1)
(1)获取语义模型
(2)检查 AnonymousMethodExpressionSyntax / SimpleLambdaExpressionSyntax / ParenthesizedLambdaExpressionSyntax
(3)将该节点传递给SemanticModel上的AnalyzeDataFlow扩展方法
(4)迭代.Captured属性