在内存中创建的工作空间 - 无法访问symbolinfo

时间:2012-12-11 22:23:55

标签: c# roslyn

我在内存中创建一个工作区用于单元测试,但我无法访问某个InvocationExpression的符号信息(这是我正在测试的CodeIssue所必需的)。这是一个失败的测试的简化示例。请帮帮忙?

        var solution =
            Solution.Create(SolutionId.CreateNewId())
                    .AddCSharpProject(Title, Title, out projectId)
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("mscorlib"))
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("Microsoft.CSharp"))
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("System"))
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("System.Core"))
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("System.Web"))
                    .AddDocument(projectId, "MyFile.cs",
                        @"using System.Web;
                        public partial class _Default : Page
                        {
                            protected void Page_Load(object sender, EventArgs e, string s)
                            {
                                Response.Write(""hello"");
                            }
                        }"
                    , out documentId);



        var mydocument = solution.GetDocument(documentId);
        var tree = mydocument.GetSyntaxTree();
        var model = mydocument.GetSemanticModel();
        var responseWriteLine = tree.GetRoot().DescendantNodesAndSelf().First(x => x.GetLocation().GetLineSpan(false).StartLinePosition.Line == 5 && x is InvocationExpressionSyntax);

        Assert.IsNotNull(model.GetSymbolInfo(responseWriteLine.Expression).Symbol);

1 个答案:

答案 0 :(得分:3)

我在代码片段中添加了一些缺失的用法。

 var solution =
            Solution.Create(SolutionId.CreateNewId())
                    .AddCSharpProject(Title, Title, out projectId)
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("mscorlib"))
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("Microsoft.CSharp"))
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("System"))
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("System.Core"))
                    .AddMetadataReference(projectId, MetadataReference.CreateAssemblyReference("System.Web"))
                    .AddDocument(projectId, "MyFile.cs",
                        @"using System;
                        using System.Web;
                        using System.Web.UI;

                        public partial class _Default : Page
                        {
                            protected void Page_Load(object sender, EventArgs e, string s)
                            {
                                Response.Write(""hello"");
                            }
                        }"
                    , out documentId);



            var mydocument = solution.GetDocument(documentId);
            var tree = mydocument.GetSyntaxTree();
            SemanticModel model = (SemanticModel)mydocument.GetSemanticModel();

            var responseWriteLine = tree.GetRoot().DescendantNodes().OfType<InvocationExpressionSyntax>().First();
            Assert.IsNotNull(model.GetSymbolInfo(responseWriteLine.Expression).Symbol);