2015-04-02 9 views
5

Próbuję otworzyć RoslynLight.sln za pomocą OpenSolutionAsync, a następnie przejrzeć wszystkie projekty. Do moich celów potrzebuję modelu semantycznego i rozwiązanych odniesień. Dzięki połączeniu tej issue i this question, ja przyjechałem w tym częściowe rozwiązanie:Jak mogę rozwiązać wszystkie odwołania do OpenSolutionAsync 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(); 
    } 
    } 
} 

Dziennik dla tego programu jest zbyt duży dla mnie pisać, ale praca prace wokół tylko dla niektórych projektów. Na przykład, rosyln \ src \ kompilatory \ Rdzeń \ Pulpit \ CodeAnalysis.Desktop.csproj nie ma błędów, ale Roslyn \ src \ kompilatory \ CSharp \ Portable \ CSharpCodeAnalysis.csproj posiada:

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' 

... i wiele inni. Wydaje mi się, że w żaden sposób nie mógłbym wykonać tej pracy, ponieważ arbitralny projekt/rozwiązanie byłby bardzo hacky. Czy czegoś brakuje?

Odpowiedz

3

Wystarczy dodać odniesienia do elewacji, jeśli projekt jest ukierunkowany na "pełne" środowisko .NET. Więc jeśli najpierw spojrzysz na istniejące odniesienia, jeśli istnieją odniesienia, które pochodzą z Zespołów Odniesienia \ Microsoft.NETPortable \ v4.5 \ Profile \ ..., nie powinieneś ich dodawać.

+0

dziękuję! Sprawdzanie brakujących odniesień prawie zadziałało, ale wciąż jest (przynajmniej) jeden projekt z brakującymi odniesieniami (myślę). Kiedy otwieram BasicCodeAnalysis.Desktop.vbproj, otrzymuję całą masę błędów: [gist] (https://gist.github.com/scottcarr/cedcf98060159cdac418#file-gistfile1-txt) Jakieś pomysły? –

+0

Wydaje się, że dostaję o wiele więcej błędów dla VB niż C#. Jeśli spróbuję otworzyć tylko wszystkie projekty C# w RoslynLight.sln, otrzymuję możliwą do zarządzania liczbę błędów: https://gist.github.com/scottcarr/fdc07fad670be7006852#file-gistfile1-txt. Kod znajduje się tutaj: https: //github.com/scottcarr/roslyn_repros –

+0

Twoje błędy VB będą wyglądać tak, jakby coś się stało z odwołaniami do projektów VB-to-C#. Wygląda na to, że strona VB nie może znaleźć rzeczy w naszym "udostępnionym" kodzie, który jest napisany w języku C#. –

4

Czekam na naprawienie tego w celach MSBuild, ale w międzyczasie, następujące obejście powinno rozwiązać problem. Zamiast używać:

MSBuildWorkspace.Create(); 

użytku:

MSBuildWorkspace.Create(new Dictionary<string, string> { { "CheckForSystemRuntimeDependency", "true" } }); 
Powiązane problemy