gpt4 book ai didi

c# - 引用 Newtonsoft.Json.dll 时,在运行时在单声道下编译 C# 代码失败

转载 作者:行者123 更新时间:2023-12-04 12:25:02 29 4
gpt4 key购买 nike

我们的软件是用 C# 编写的运行 .Net-Framework 4.7.2 .我们的用户可以使用我们软件中内置的脚本编辑器,用 C# 编写他们自己的用户脚本。为了实现这一点,我们使用了 .Net 组件 CodeDomProvider .为了能够以一种简单的方式使用 JSON,我们想要制作 Newtonsoft.Json.dll在我们的用户脚本中引用。这是通过添加对 CodeDomProvider 的 CompilerParameters 的引用来完成的。 Newtonsoft.Json 通过使用最新的 Newtonsoft.Json NuGet 添加到我的应用程序中包 (12.0.3)。

这是此设置的最小示例:

using System;
using System.CodeDom.Compiler;
using System.IO;

namespace ScriptingUnix
{
class Program
{
static void Main(string[] args)
{
try
{
string output;
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
output = "/tmp/MyUnixTest.dll";
}
else
{
output = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyUnixTest.dll");
}

CompilerParameters parameters = new CompilerParameters
{
GenerateInMemory = false,
IncludeDebugInformation = true,
OutputAssembly = output
};

using (CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp"))
{
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("Newtonsoft.Json.dll");
CompilerResults results = codeProvider.CompileAssemblyFromSource
(
parameters,
"using System;\nusing Newtonsoft.Json.Serialization;\nusing Newtonsoft.Json.Linq;\n class Program\r\n{\r\n static void Main(string[] args)\r\n {\r\n \r\n }\r\n}"
);

foreach (CompilerError compilerError in results.Errors)
{
Console.WriteLine(compilerError);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
}

此设置在 Windows 上的 .Net Framework 下运行良好。问题是,我们还提供了在 (Arch)-Linux 上运行 Mono 的软件版本(Linux 4.19.80-1-ARCH armv7l,Mono JIT 编译器版本 6.0.0(makepkg/6256b82d62f Wed 25 Sep 2019 05 :04:08 AM UTC)),其中使用单声道运行相同的代码会导致 error CS0006: Metadata file Newtonsoft.Json.dll could not be found .

我已经尝试将以下内容添加到我的应用程序的 app.conf 中,但这没有帮助。还要重新安装 NuGet包没有帮助。添加 Newtonsoft.Json.dll到 GAC 不起作用(失败 Failure adding assembly Newtonsoft.Json.dll to the cache: The file specified is not a valid assembly.)。在尝试使用 app.conf 时,我观察到错误消息更改为 error CS0009: Metadata file /opt/Newtonsoft.Json.dll does not contain valid metadata: Metadata file /opt/Newtonsoft.Json.dll does not contain valid metadata .这就是为什么我猜他找到了 dll 但需要额外的、有效的 metadata。我想到了某种 dll.conf。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

有没有人有关于如何让它工作的提示?

最佳答案

你有两种可能:

  • 使用来自 github 的最新 Mono (6.6)。如果包不可用,则编译它。
  • 将 Newtonsoft.Json 降级到 11.0 版。如果需要,请使用绑定(bind)重定向。
  • 关于c# - 引用 Newtonsoft.Json.dll 时,在运行时在单声道下编译 C# 代码失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59209700/

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