0

我正在为单元测试目的在内存中创建一个工作区,但我无法访问某个 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);
4

1 回答 1

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);
于 2012-12-12T06:45:59.970 回答