简体   繁体   English

C#4.0使用.NET合并.dll程序集

[英]C# 4.0 Merge .dll assembly with .NET

I decided to leave my other question to die, since I thought of a new idea using Jeffrey Richter's method written on this page to merge a .dll library to my application. 我决定将我的另一个问题留下来,因为我想到了一个新的想法,使用在此页面上编写的Jeffrey Richter的方法将.dll库合并到我的应用程序中。 So I added my .dll file as an embedded resource and also added it as a reference. 所以我将.dll文件添加为嵌入式资源,并将其添加为参考。 Then in Program.cs (I have no idea where the code he posted is supposed to go), I added this: 然后在Program.cs中(我不知道他发布的代码应该去哪里),我添加了这个:

    ...
    [STAThread]
    static void Main()
    {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length > 1)
                _in = args[1];
            SingleInstanceController controller = new SingleInstanceController();
            controller.Run(args);

            AppDomain.CurrentDomain.AssemblyResolve += (sender, argsx) =>
{
String resourceName = "AssemblyLoadingAndReflection." +
   new AssemblyName(argsx.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);
    }
}
        ;

Am I supposed to change resourceName to something else? 我应该将resourceName更改为其他内容吗? Have I added it correctly (in the right place)? 我是否正确添加(在正确的位置)?

Now the problem is, it still fails to find and load the assembly and I'm not sure what I've done wrong. 现在问题是,它仍然无法找到并加载程序集,我不确定我做错了什么。 Any help would be appreciated. 任何帮助,将不胜感激。

Your problem is very similar to this one: C#: How to embed DLL into resourcefile (no dll copy in program directory) 您的问题与此非常相似: C#:如何将DLL嵌入资源文件中(程序目录中没有dll副本)

Basically, your AppDomain.AssemblyResolve event handler did not get called because Main failed to compile. 基本上,您的AppDomain.AssemblyResolve事件处理程序未被调用,因为Main无法编译。 Even if it did compile, attaching the event handler should be the first thing you do in main. 即使它已经编译,附加事件处理程序应该是你在main中做的第一件事。

My answer to the question above has a working code sample and explanation on why your code does not work. 我对上述问题的回答有一个工作代码示例和解释为什么您的代码不起作用。

Use the debugger. 使用调试器。 Set breakpoints on the AssemblyResolve assignment and the lambda body. 在AssemblyResolve赋值和lambda主体上设置断点。 Single step the code. 单步代码。

Yes, that's too late. 是的,现在为时已晚。 Move the assignment. 移动作业。 If SingleInstanceController is in such a DLL then the Main() method never even gets started. 如果SingleInstanceController在这样的DLL中,那么Main()方法甚至都不会启动。 Move that code into a separate helper method and give it the [MethodImpl(MethodImplOptions.Noinlining)] attribute. 将该代码移动到单独的帮助器方法中,并为其提供[MethodImpl(MethodImplOptions.Noinlining)]属性。

Distributing your program in a single file is already very well supported, it doesn't require any code nor merging DLLs. 已经很好地支持在单个文件中分发程序,它不需要任何代码也不需要合并DLL。 Also takes care of the desktop shortcut, the file associations and getting .NET installed on old machines. 还要处理桌面快捷方式,文件关联以及在旧计算机上安装.NET。 It's called setup.exe 它叫做setup.exe

Hook to the AssemblyResolve event before the AppDomain tries to resolve references ie in the entry point and the first line. 在AppDomain尝试解析引用(即在入口点和第一行)之前挂钩到AssemblyResolve事件。

The embedded resource name starts with the application name followed by the resource name. 嵌入的资源名称以应用程序名称开头,后跟资源名称。 ex: ConsoleApplication.Test.dll. 例如:ConsoleApplication.Test.dll。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM