2015-04-02 12 views
5

Sto tentando di aprire RoslynLight.sln con OpenSolutionAsync per poi scorrere tutti i progetti. Per i miei scopi ho bisogno di un modello semantico e di riferimenti risolti. Attraverso una combinazione di questo issue e this question, sono arrivato a questa soluzione parziale:Come posso risolvere tutti i riferimenti con OpenSolutionAsync di 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(); 
    } 
    } 
} 

Il log del programma è troppo grande per me per spedire, ma il lavoro funziona in tutto solo per alcuni progetti. Ad esempio, Rosyln \ src \ compilatori \ Core \ Desktop \ CodeAnalysis.Desktop.csproj non ha errori, ma Roslyn \ src \ compilatori \ CSharp \ portatile \ CSharpCodeAnalysis.csproj ha:

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' 

... e molti altri. Mi sembra che qualsiasi modo per farlo funzionare per un progetto/soluzione arbitraria sarebbe molto hacky. Mi sto perdendo qualcosa?

risposta

3

È necessario aggiungere solo i riferimenti di facciata se il progetto è destinato al framework .NET "completo". Quindi, se prima guardi i riferimenti esistenti, se ci sono riferimenti che provengono da Reference Assembly \ Microsoft.NETPortable \ v4.5 \ Profile \ ... allora non dovresti aver bisogno di aggiungerli.

+0

Grazie! Il controllo dei riferimenti mancanti ha funzionato quasi, ma c'è ancora (almeno) un progetto con riferimenti mancanti (credo). Quando apro BasicCodeAnalysis.Desktop.vbproj ottengo un sacco di errori di tipo: [gist] (https://gist.github.com/scottcarr/cedcf98060159cdac418#file-gistfile1-txt) Qualche idea? –

+0

Sembra che ci siano molti più errori per VB che C#. Se provo ad aprire solo tutti i progetti C# in RoslynLight.sln, ottengo un numero gestibile di errori: https://gist.github.com/scottcarr/fdc07fad670be7006852#file-gistfile1-txt. Il codice è qui: https: //github.com/scottcarr/roslyn_repros –

+0

Gli errori VB sembrerebbero che ci sia qualcosa che ha glitchato con riferimenti di progetto VB-to-C#. Sembra che il lato VB non riesca a trovare le cose nel nostro codice "condiviso" scritto in C#. –

4

Sto cercando di sistemarlo negli obiettivi di MSBuild, ma nel frattempo, la seguente soluzione alternativa dovrebbe risolvere il problema. Invece di usare:

MSBuildWorkspace.Create(); 

uso:

MSBuildWorkspace.Create(new Dictionary<string, string> { { "CheckForSystemRuntimeDependency", "true" } }); 
Problemi correlati