我该如何解决与罗斯林的OpenSolutionAsync所有引用? [英] How can I resolve all references with Roslyn's OpenSolutionAsync?

查看:380
本文介绍了我该如何解决与罗斯林的OpenSolutionAsync所有引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用OpenSolutionAsync打开RoslynLight.sln然后通过所有的项目迭代。对于我而言,我需要一个语义模型和解决的引用。通过这个问题的组合,的这个问题,我在这部分解决到达:

I'm trying to open RoslynLight.sln with OpenSolutionAsync then iterate through all the projects. For my purposes I need a semantic model and resolved references. Through a combination of this issue and this question, I've arrived at this partial solution:

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\compilers\Core\Desktop\CodeAnalysis.Desktop.csproj没有错误,但roslyn\src\Compilers\CSharp\Portable\CSharpCodeAnalysis.csproj具有:

The log for this program is too big for me to post, but the work around only works for certain projects. For example, rosyln\src\compilers\Core\Desktop\CodeAnalysis.Desktop.csproj has no errors, but roslyn\src\Compilers\CSharp\Portable\CSharpCodeAnalysis.csproj has:

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'

...等等。在我看来,任何方式,我可以做这项工作的一个任意的项目/解决方案将是非常哈克。我缺少的东西吗?

... and many others. It seems to me any way I could make this work for an arbitrary project/solution would be very hacky. Am I missing something?

推荐答案

您只需要添加外墙引用如果该项目的目标是全。NET框架。所以,如果你在现有引用先看看,如果有来自参考Assemblies\Microsoft.NETPortable\v4.5\Profile\任何引用......那么你不应该需要添加他们。

You only need to add the façade references if the project is targeting the "full" .NET framework. So if you look at the existing references first, if there are any references that come from Reference Assemblies\Microsoft.NETPortable\v4.5\Profile\... then you shouldn't need to add them.

这篇关于我该如何解决与罗斯林的OpenSolutionAsync所有引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆