2015-04-02 14 views
5

Tôi đang cố mở RoslynLight.sln bằng OpenSolutionAsync rồi lặp qua tất cả các dự án. Vì mục đích của tôi, tôi cần một mô hình ngữ nghĩa và các tham chiếu được giải quyết. Thông qua sự kết hợp của issue này và this question, tôi đã đến giải pháp phần này:Làm cách nào để tôi có thể giải quyết tất cả các tham chiếu với OpenSolutionAsync của Roslyn?

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(); 
    } 
    } 
} 

Nhật ký cho chương trình này là quá lớn đối với tôi để gửi, nhưng công việc xung quanh chỉ làm việc cho các dự án cụ thể. Ví dụ, rosyln \ src \ trình biên dịch \ Lõi \ Desktop \ CodeAnalysis.Desktop.csproj không có lỗi, nhưng roslyn \ src \ Trình biên dịch \ CSharp \ xách tay \ CSharpCodeAnalysis.csproj có:

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' 

... và nhiều khác. Dường như với tôi bất kỳ cách nào tôi có thể làm cho công việc này cho một dự án tùy ý/giải pháp sẽ rất hacky. Tui bỏ lỡ điều gì vậy?

Trả lời

3

Bạn chỉ cần thêm tham chiếu façade nếu dự án đang nhắm mục tiêu khung .NET "đầy đủ". Vì vậy, nếu bạn nhìn vào các tài liệu tham khảo hiện có đầu tiên, nếu có bất kỳ tài liệu tham khảo đến từ tham chiếu Assemblies \ Microsoft.NETPortable \ v4.5 \ Profile \ ... thì bạn không cần phải thêm chúng.

+0

Cảm ơn bạn! Kiểm tra các tài liệu tham khảo bị thiếu gần như đã làm việc, nhưng vẫn còn (ít nhất) một dự án có thiếu tham chiếu (tôi nghĩ). Khi tôi mở BasicCodeAnalysis.Desktop.vbproj tôi nhận được một loạt các lỗi loại: [gist] (https://gist.github.com/scottcarr/cedcf98060159cdac418#file-gistfile1-txt) Bất kỳ ý tưởng nào? –

+0

Dường như tôi nhận được nhiều lỗi hơn cho VB hơn C#. Nếu tôi cố gắng chỉ mở tất cả các dự án C# trong RoslynLight.sln, tôi nhận được một số lỗi có thể quản lý được: https://gist.github.com/scottcarr/fdc07fad670be7006852#file-gistfile1-txt. Mã này là ở đây: https: //github.com/scottcarr/roslyn_repros –

+0

Lỗi VB của bạn sẽ giống như có một cái gì đó mà glitched với VB-to-C# tài liệu tham khảo dự án. Có vẻ như bên VB không thể tìm thấy nội dung trong mã "được chia sẻ" của chúng tôi được viết bằng C#. –

4

Tôi đang tìm cách sửa lỗi này trong mục tiêu MSBuild, nhưng trong thời gian chờ đợi, giải pháp sau đây sẽ giải quyết vấn đề. Thay vì sử dụng:

MSBuildWorkspace.Create(); 

sử dụng:

MSBuildWorkspace.Create(new Dictionary<string, string> { { "CheckForSystemRuntimeDependency", "true" } }); 
Các vấn đề liên quan