2012-12-11 66 views
0

我在內存中爲單元測試目的創建了一個工作空間,但我無法訪問某個InvocationExpression的符號信息(這是我測試的CodeIssue所需的)。這是一個失敗測試的簡單例子。請幫忙?在內存中創建的工作空間 - 無法訪問symbolinfo

 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); 
+0

您正在使用哪種ctp? –

回答

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); 
+0

謝謝你清理我的狡猾的代碼!它看起來現在正在工作。 :) FWIW,我正在使用2012年9月份的CTP。 – lee