C#4.0与.NET合并.dll程序集 [英] C# 4.0 Merge .dll assembly with .NET

查看:202
本文介绍了C#4.0与.NET合并.dll程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定离开我的其他问题死亡,因为我想到了一个新的想法,使用Jeffrey Richter的方法写在此页面将.dll库合并到我的应用程序。所以我添加了.dll文件作为嵌入式资源,并将其添加为参考。然后在Program.cs(我不知道他发布的代码应该去哪里),我补充说:

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. So I added my .dll file as an embedded resource and also added it as a reference. Then in Program.cs (I have no idea where the code he posted is supposed to go), I added this:

    ...
    [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);
    }
}
        ;

我应该将resourceName更改为别的吗?我是否正确添加了(在正确的地方)?

Am I supposed to change resourceName to something else? 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.

推荐答案

在AppDomain尝试解析引用之前挂钩到AssemblyResolve事件,即在入口点和第一行。

Hook to the AssemblyResolve event before the AppDomain tries to resolve references i.e in the entry point and the first line.

嵌入式资源名称以应用程序名称开头,后跟资源名称。
ex:ConsoleApplication.Test.dll。

The embedded resource name starts with the application name followed by the resource name. ex: ConsoleApplication.Test.dll.

这篇关于C#4.0与.NET合并.dll程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆