2015-04-02 41 views
5

我试图用OpenSolutionAsync打开RoslynLight.sln,然后遍历所有项目。为了我的目的,我需要一个语义模型并解析引用。通过这种issuethis question组合,我已经到达此部分解决:如何解决与Roslyn的OpenSolutionAsync有关的所有引用?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Microsoft.CodeAnalysis; 
using Microsoft.CodeAnalysis.MSBuild; 
using System.IO; 

namespace OpenRoslyn 
{ 
    class Program 
    { 
    static void Main(string[] args) 
    { 

     var msbw = MSBuildWorkspace.Create(); 
     var sln = msbw.OpenSolutionAsync(@"C:\Users\carr27\Documents\GitHub\roslyn\src\RoslynLight.sln").Result; 
     //var proj = sln.Projects.First(x => x.Name == "CodeAnalysis.Desktop"); 
     var messages = new List<string>(); 
     foreach (var p in sln.Projects) 
     { 
     Console.WriteLine(p.FilePath); 
     messages.Add(p.FilePath); 
     var facadesDir = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\"; 
     var proj = p.AddMetadataReference(MetadataReference.CreateFromAssembly(typeof(object).Assembly)); 
     proj = proj.AddMetadataReference(MetadataReference.CreateFromFile(facadesDir + "System.Runtime.dll")); 
     proj = proj.AddMetadataReference(MetadataReference.CreateFromFile(facadesDir + "System.Runtime.Extensions.dll")); 
     proj = proj.AddMetadataReference(MetadataReference.CreateFromFile(facadesDir + "System.IO.dll")); 
     proj = proj.AddMetadataReference(MetadataReference.CreateFromFile(facadesDir + "System.Threading.Tasks.dll")); 
     proj = proj.AddMetadataReference(MetadataReference.CreateFromFile(facadesDir + "System.Text.Encoding.dll")); 
     proj = proj.AddMetadataReference(MetadataReference.CreateFromFile(facadesDir + "System.Reflection.dll")); 
     try 
     { 
      var cu = proj.GetCompilationAsync().Result; 
      // here I would do useful work, but for know I just get diagnostics 
      foreach (var e in cu.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error)) 
      { 
      Console.WriteLine("{0}: {1}", e.Location, e.GetMessage()); 
      messages.Add(String.Format("{0}: {1}", e.Location, e.GetMessage())); 
      } 
     } 
     catch (AggregateException e) 
     { 
      foreach(var ie in e.InnerExceptions) 
      { 
      Console.WriteLine(ie.Message); 
      messages.Add(ie.Message); 
      } 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
      messages.Add(e.Message); 
     } 
     } 
     File.WriteAllLines("log.txt", messages); 
     Console.WriteLine("done."); 
     Console.ReadKey(); 
    } 
    } 
} 

日志对这一计划是太大了,我张贴,但工作只有大约可用于特定项目。例如,rosyln的\ src \编译\核心\桌面\ CodeAnalysis.Desktop.csproj没有错误,但罗斯林的\ src \编译\ CSHARP \便携式\ CSharpCodeAnalysis.csproj有:

None: Multiple assemblies with equivalent identity have been imported: 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.Threading.Tasks.dll' and 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\System.Threading.Tasks.dll'. Remove one of the duplicate references. 
None: Multiple assemblies with equivalent identity have been imported: 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.Text.Encoding.dll' and 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\System.Text.Encoding.dll'. Remove one of the duplicate references. 
None: Multiple assemblies with equivalent identity have been imported: 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.Runtime.Extensions.dll' and 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\System.Runtime.Extensions.dll'. Remove one of the duplicate references. 
None: Multiple assemblies with equivalent identity have been imported: 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.Runtime.dll' and 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\System.Runtime.dll'. Remove one of the duplicate references. 
None: Multiple assemblies with equivalent identity have been imported: 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.Reflection.dll' and 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\System.Reflection.dll'. Remove one of the duplicate references. 
None: Multiple assemblies with equivalent identity have been imported: 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.IO.dll' and 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\System.IO.dll'. Remove one of the duplicate references. 
SourceFile(C:\Users\carr27\Documents\GitHub\roslyn\src\Compilers\CSharp\Portable\BoundTree\UnboundLambda.cs[9068..9109)): The type 'ConcurrentDictionary<TKey, TValue>' exists in both 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' and 'System.Collections.Concurrent, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 
SourceFile(C:\Users\carr27\Documents\GitHub\roslyn\src\Compilers\CSharp\Portable\BoundTree\UnboundLambda.cs[9203..9250)): The type 'ConcurrentDictionary<TKey, TValue>' exists in both 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' and 'System.Collections.Concurrent, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 

...等等其他。在我看来,任何可以让这项工作成为任意项目/解决方案的方法都会非常黑客。我错过了什么吗?

回答

3

如果项目的目标是“完整”.NET框架,则只需添加外观引用。因此,如果您首先查看现有的引用,如果有任何引用来自引用程序集\ Microsoft.NETPortable \ v4.5 \ Profile \ ...,那么您不需要添加它们。

+0

谢谢!检查缺失的引用几乎工作,但仍有(至少)一个项目缺少引用(我认为)。当我打开BasicCodeAnalysis.Desktop.vbproj时,我收到了一大堆类型错误:[gist](https://gist.github.com/scottcarr/cedcf98060159cdac418#file-gistfile1-txt)任何想法? – 2015-04-02 20:48:58

+0

似乎我得到了比C#更多的VB错误。如果我试图只打开RoslynLight.sln中的所有C#项目,我会遇到可管理的错误数量:https://gist.github.com/scottcarr/fdc07fad670be7006852#file-gistfile1-txt。代码在这里:https://github.com/scottcarr/roslyn_repros – 2015-04-02 21:28:10

+0

你的VB错误看起来像VB-to-C#项目引用有问题。它看起来像VB一边无法找到我们用C#编写的“共享”代码中的东西。 – 2015-04-03 05:29:31

4

我正在寻找解决这个问题的MSBuild目标,但与此同时,下面的解决方法应该解决这个问题。而不是使用:

MSBuildWorkspace.Create(); 

使用:

MSBuildWorkspace.Create(new Dictionary<string, string> { { "CheckForSystemRuntimeDependency", "true" } }); 
相关问题