gpt4 book ai didi

c# - Roslyn AdhocWorkspace 要求引用 System.Runtime

转载 作者:太空狗 更新时间:2023-10-29 21:59:57 27 4
gpt4 key购买 nike

我正在尝试对一些 C# 代码进行语义分析。在为我的测试设置 AdhocWorkspace 时,我发现了指向使用 Typeobject 上的程序集位置的示例。当我这样做时,我的诊断中出现以下错误:

[0]: TestIsNotInstanceOfTypeMessage.cs(13,20): error CS0012: The type 'Object' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

[1]: TestIsNotInstanceOfTypeMessage.cs(13,13): error CS0012: The type 'Type' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

当我添加以下行时,一切正常:

MetadataReference.CreateFromFile(@"C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\v4.6.1\\Facades\\System.Runtime.dll"),

我对测试项目中的硬编码路径不太满意,但不确定如何“获取”4.6.1 版本的 objectType any另一种方式。下面的代码显示了我的助手获取 AdhocWorkspace

的要点
private static readonly ImmutableArray<MetadataReference> _coreReferences = 
ImmutableArray.Create<MetadataReference>(
MetadataReference.CreateFromFile(typeof(Type).Assembly.Location),
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)


private static readonly Project _baseProject = new AdhocWorkspace()
.AddProject("Test", LanguageNames.CSharp)
.AddMetadataReferences(_coreReferences)
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

更多细节:

  • Visual Studio Community v15.4.0
  • 目标框架:.NET 4.6.1
  • Microsoft.CodeAnalysis.CSharp v2.4.0
  • 主工程是控制台应用,测试工程是普通库,PCL都不是

最佳答案

这是因为您使用的是 AdhocWorkspace。据我所知,此工作区旨在与 .Net 无关。意思是——它应该适用于.Net Standard 和.Net Core,交叉版本。

如果您想跳过与默认库相关的问题,您最好使用 MSBuildWorkspace

但是,此工作区依赖于计算机上安装的 MSBuild,以及其他一些 MSBuild 特定的东西,这可能会导致一些其他问题。喜欢:

Msbuild 在处理文件“C:\Path\Myfile.csproj”时失败,消息为:C:\Path\packages\somepackage\build\somepackage.targets: (75, 2): The "sometask"任务声明或使用不正确,或在构造过程中失败。检查任务名称和程序集名称的拼写。

如果您想坚持使用 AdHoc 工作区 - 您将必须手动定位引用,方法是使用注册表定位 .Net 程序集文件夹,或者可能求助于 GAC。换句话说 - 如果您想将解决方案中的 AdHoc 空间与 .Net 标准联系起来 - 您可以这样做,但您只能靠自己了。

关于c# - Roslyn AdhocWorkspace 要求引用 System.Runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47125443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com