将 .dll 合并到 C# 程序集中?
使用 #C.net 3.5
我知道 ILMerge 和类似技术,但我实际上想利用 杰弗里·里希特的建议。
将此代码添加到构造函数后,会出现命名空间问题。
Jeffrey Richter 的代码:
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);
}
};
我将这两个 DLL 设置为“嵌入资源”并将它们添加为引用,但是当我构建程序时,这两个 DLL 仍然是“未嵌入”,然后我将它们作为引用删除,但是当我构建程序时出现错误:“找不到类型或命名空间...您是否缺少 using 指令或程序集引用?”我将两个命名空间添加为 using 指令...,然后尝试删除两个引用和两个 using 指令,但仍然出现错误。
这是我的代码:
using System.Reflection;
namespace WindowsFormsApplication2
{
public partial class Ndice : Form
{
public Ndice()
{
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);
}
};
InitializeComponent();
}
}
}
Using #C.net 3.5
I'm aware of the ILMerge and alike techniques, but I would actually like to make use of Jeffrey Richter's suggestions.
After adding this code to the constructor there are namespace troubles.
Jeffrey Richter's code:
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);
}
};
I set the two DLL's as "Embedded Resource" and added them as a Reference, but when i build the program, the two dll are still "Un-embbeded", I then removed them as a Reference but then when i build the program there are errors saying: "the type or namespace cannot be found...are you missing a using directive or an assembly reference?" I have the two namespaces added as a using directive..., I then tried to remove the two references and the two using directives and still errors.
here is my code:
using System.Reflection;
namespace WindowsFormsApplication2
{
public partial class Ndice : Form
{
public Ndice()
{
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);
}
};
InitializeComponent();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您问题的描述来看,有些事情尚不清楚,可能需要这些事情才能回答:
基本上,据我了解,您的代码尝试使用两个程序集中的类。此代码是静态,这意味着这些类必须在编译时已知。或者换句话说,您必须引用这两个程序集才能编译使用其中定义的类型的代码。
我真的不明白当您添加这些程序集作为参考时出了什么问题。如果让您烦恼的是您看到它们被复制到 bin/debug 目录中,那仍然不应该阻止它们嵌入到您的主程序集中。因此,为了进行测试,您可以尝试从 bin/debug 中手动删除它们,或者将它们设置为“copy local = false”。
另一件事 - 您遇到的提到“使用”和“命名空间”的错误实际上与命名空间无关。这个错误意味着编译器没有找到它需要的类型。这可能是因为您删除了对这两个程序集的引用。
From the description in your question, some things are not clear that may be needed in order to answer:
Basically, from what I understand, you have code that tries to use classes from the 2 assemblies. This code is static, meaning that the classes have to be known at compile time. Or in other words, you must have a reference to those 2 assemblies in order to compile code that uses types that are defined in them.
I don't really understand what went wrong when you added those assemblies as a reference. If what bothered you is that you saw them being copied into the bin/debug directory, that still should not have prevented them from also being embedded in your main assembly. So to test, you can try manually deleting them from bin/debug, or maybe setting them to "copy local = false".
One more thing - the error you encountered that mentioned "using" and "namespaces" wasn't really about namespaces. This error means that the compiler didn't find the types it needed. And that was probably because you removed the references to the 2 assemblies.
将其放在构造函数中,而不是 InitializeComponent() 中。将 using 指令添加到主类源代码文件中不是问题。
Put it in the constructor, not InitializeComponent(). Adding using directives to your main class source code file isn't a problem.
应改为:
should be changed to: