如何使用Roslyn SemanticModel通过ScriptCompilation获取TypeInfo

时间:2017-01-11 14:32:56

标签: c# roslyn

我正在尝试提取TypeInfo以获取动态脚本环境中的可用成员列表。

如果我使用CSharpCompilation.Create(),我可以获取成员列表,但是当我从CSharpScript状态获取编译或使用CSharpCompilation.CreateScriptCompilation()时,我得到一个异常“序列包含多个元素”。

此功能正常

    public List<string> Test()
    {
        var code = @"
        using System;
        var i = 5; ";

        var code2 = "i. ";

        var parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Script);

        var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, parseOptions);
        var syntaxTree2 = SyntaxFactory.ParseSyntaxTree(code2, parseOptions);

        var systemReference = MetadataReference.CreateFromFile(typeof(Int32).Assembly.Location);
        var compilation = CSharpCompilation.Create("foo")
                .AddReferences(systemReference)
                .AddSyntaxTrees(syntaxTree)
                .AddSyntaxTrees(syntaxTree2);

        var semanticModel = compilation.GetSemanticModel(syntaxTree2);

        var dotTextSpan = new TextSpan(code2.IndexOf("i.") + 1, 1);
        var s = syntaxTree2.GetRoot().DescendantNodes(dotTextSpan).Last();
        var memberAccessNode = (MemberAccessExpressionSyntax) s;

        var lhsType = semanticModel.GetTypeInfo(memberAccessNode.Expression).Type;

        var r = new List<string>();

        foreach (var symbol in lhsType.GetMembers())
        {
            if (!symbol.CanBeReferencedByName
                || symbol.DeclaredAccessibility != Accessibility.Public
                || symbol.IsStatic)
                continue;

            r.Add(symbol.Name);
        }

        return r;
    }

但是当使用CSharpScript时,我得到了异常

    public List<string> Test2()
    {
        var code = @"
        using System;
        var i = 5; ";

        var scriptOptions = ScriptOptions.Default;
        scriptOptions.AddReferences(typeof(Int32).Assembly);

        var scriptState = CSharpScript.RunAsync(code, scriptOptions).Result;

        var code2 = "i. ";

        var parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Script);

        var syntaxTree2 = SyntaxFactory.ParseSyntaxTree(code2, parseOptions);

        var compilation = scriptState.Script.GetCompilation()
                .AddSyntaxTrees(syntaxTree2);

        var semanticModel = compilation.GetSemanticModel(syntaxTree2);

        var dotTextSpan = new TextSpan(code2.IndexOf("i.") + 1, 1);
        var s = syntaxTree2.GetRoot().DescendantNodes(dotTextSpan).Last();
        var memberAccessNode = (MemberAccessExpressionSyntax)s;

        var lhsType = semanticModel.GetTypeInfo(memberAccessNode.Expression).Type;

        var r = new List<string>();

        foreach (var symbol in lhsType.GetMembers())
        {
            if (!symbol.CanBeReferencedByName
                || symbol.DeclaredAccessibility != Accessibility.Public
                || symbol.IsStatic)
                continue;

            r.Add(symbol.Name);
        }

        return r;
    }

知道如何让它运行吗?

编辑:这是Exception的完整堆栈跟踪。

"   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.IsSubmissionSyntaxTree(SyntaxTree tree)\r\n   at Microsoft.CodeAnalysis.CSharp.BinderFactory.BinderFactoryVisitor.VisitCompilationUnit(CompilationUnitSyntax compilationUnit, Boolean inUsing, Boolean inScript)\r\n   at Microsoft.CodeAnalysis.CSharp.BinderFactory.GetBinder(CSharpSyntaxNode node, Int32 position, CSharpSyntaxNode memberDeclarationOpt, Symbol memberOpt)\r\n   at Microsoft.CodeAnalysis.CSharp.BinderFactory.GetBinder(CSharpSyntaxNode node, CSharpSyntaxNode memberDeclarationOpt, Symbol memberOpt)\r\n   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.CreateMemberModel(CSharpSyntaxNode node)\r\n   at System.Collections.Immutable.ImmutableInterlocked.GetOrAdd[TKey,TValue](ImmutableDictionary`2& location, TKey key, Func`2 valueFactory)\r\n   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetMemberModel(CSharpSyntaxNode node)\r\n   at Microsoft.CodeAnalysis.CSharp.SyntaxTreeSemanticModel.GetTypeInfoWorker(CSharpSyntaxNode node, CancellationToken cancellationToken)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetTypeInfo(ExpressionSyntax expression, CancellationToken cancellationToken)\r\n   at Microsoft.CodeAnalysis.CSharp.CSharpExtensions.GetTypeInfo(SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken)\r\n   at ScriptEngine.ScriptingHost.Test2() in D:\\Source\\ScriptEngine\\ScriptingHost.cs:line 205"

1 个答案:

答案 0 :(得分:2)

脚本编辑can only have a single SyntaxTree

您应该将节点添加到原始的SyntaxTree中。