gpt4 book ai didi

c# - 在 OSX : "No assembly containing System.Object was found" 上使用 .NET Core 从代码调用 Roslyn

转载 作者:太空宇宙 更新时间:2023-11-03 21:04:03 25 4
gpt4 key购买 nike

我正尝试在 OSX 上的 .NET Core 中从 C# 调用 Roslyn。

public class Program
{
public static void Main()
{
var cscArgs = CSharpCommandLineParser.Default.Parse(
new[] { "/target:Library" },
Directory.GetCurrentDirectory(),
"/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.1.0/"
);
var refs = cscArgs.MetadataReferences.Select(x => MetadataReference.CreateFromFile(x.Reference, x.Properties));

foreach (var r in refs)
{
Console.WriteLine(r.FilePath);
}

var trees = new[]
{
CSharpSyntaxTree.ParseText("class Foo {}", cscArgs.ParseOptions)
};

var compilation = CSharpCompilation.Create(cscArgs.CompilationName, trees, refs, cscArgs.CompilationOptions);

using (var peStream = File.OpenWrite("test.dll"))
{
var emitResult = compilation.Emit(peStream, null, null, null, cscArgs.ManifestResources, cscArgs.EmitOptions);

foreach (var d in emitResult.Diagnostics)
{
Console.WriteLine(d);
}
}
}
}

当我运行此代码时,(test.dll 保持为空并且)WriteLine 打印出一个 MetadataReference,然后是警告和两个编译错误:

/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.1.0/mscorlib.dll
warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
(2,31): error CS0518: Predefined type 'System.Object' is not defined or imported
(2,31): error CS1729: 'object' does not contain a constructor that takes 0 arguments

看起来 Compilation 没有获取 mscorlib 引用。 DLL 确实存在于引用的位置。

$ ls /usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.1.0/mscorlib.dll
/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.1.0/mscorlib.dll

我做错了什么?我怎样才能说服 Roslyn 选择 mscorlib DLL?

最佳答案

如果您尝试为 .NET Core 编译程序集,System.Object 是在 System.Runtime 中定义的,而不是 mscorlib.1 此外,在针对 .NET Core 进行编译时,编译器通常使用引用程序集,而不是实际的运行时二进制文件。

因此,简短回答:为 System.Runtime.dll 创建 MetadataReference。

var nuget = Environment.GetEnvironmentVariable("HOME") + "/.nuget/packages";
MetadataReference.CreateFromFile(nuget + "/system.runtime/4.3.0/ref/netstandard1.5/System.Runtime.dll")

更长的答案

不过,很有可能您需要的不仅仅是 System.Runtime。查看正在引用哪些库的更简单方法是检查您自己的应用程序的编译。要查看这一点,请添加对 Microsoft.Extensions.DependencyModel 的包引用并检查用于编译和应用的所有库。

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.0.0-*" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="1.1.0" />
</ItemGroup>

</Project>
var references = from l in DependencyContext.Default.CompileLibraries
from r in l.ResolveReferencePaths()
select MetadataReference.CreateFromFile(r);

foreach (var r in references)
{
Console.WriteLine(r.FilePath);
}

1参见https://learn.microsoft.com/en-us/dotnet/core/api/system.object ,在页面的最底部。

关于c# - 在 OSX : "No assembly containing System.Object was found" 上使用 .NET Core 从代码调用 Roslyn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42451218/

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