gpt4 book ai didi

c# - 使用 Fody 的 Mono 支持将 dll 嵌入到 exe 中

转载 作者:行者123 更新时间:2023-11-30 12:55:15 24 4
gpt4 key购买 nike

好吧,我的问题几乎与 Embedding DLLs in a compiled executable 相似 但提供了非常好的答案 here失去了与单声道运行时的兼容性,尽管它适用于 Windows。

那么我如何使用 Fody (Costura) 并保持单声道兼容性。他们的文档位于 https://github.com/Fody/Costura#contents阅读:

CosturaUtility is a class that gives you access to initialize the Costura system manually in your own code. This is mainly for scenarios where the module initializer doesn't work, such as libraries and Mono.

To use, call CosturaUtility.Initialize() somewhere in your code, as early as possible.

class Program {
static Program() {
CosturaUtility.Initialize();
}

static void Main(string[] args) { ... }
}

但即使在手动初始化 ConturaUtility 之后,它也不支持单声道运行时。

我不认为错误日志是相关的,但这里是:

Unhandled Exception:
System.IO.FileNotFoundException: Could not load file or assembly 'CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32' or one of its dependencies.
File name: 'CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32'
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly 'CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32' or one of its dependencies.
File name: 'CommandLine, Version=2.2.1.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32'

最佳答案

在程序中的 Main() 开头使用此代码:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
String resourceName = "AssemblyLoadingAndReflection." +
new AssemblyName(args.Name).Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};

引用:https://blogs.msdn.microsoft.com/microsoft_press/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition/

关于c# - 使用 Fody 的 Mono 支持将 dll 嵌入到 exe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50583640/

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