将.dll合并到C#程序集中? [英] Merge .dll into C# Assembly?

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

问题描述

使用#C.net 3.5

Using #C.net 3.5

我知道ILMerge和相似的技术,但我实际上想使用 Jeffrey Richter的建议

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的代码:

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

我将两个DLL设置为嵌入式资源,并将其添加为参考,但是当我构建程序中,这两个DLL仍然是Un-embbeded,然后我将它们删除为参考,但是当我构建程序时会出现错误:找不到类型或命名空间...你缺少一个using指令或装配参考?我有两个命名空间作为using指令添加...,然后我尝试删除两个引用,两个使用指令,仍然是错误。

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.

这是我的代码:

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();
               }
           }
        }


推荐答案

从您的问题的描述中,有些事情不清楚可能需要回答:

From the description in your question, some things are not clear that may be needed in order to answer:


  1. 究竟是什么不工作您是否在编译时或运行时失败?

  2. 您可以显示不编译的代码,还是在运行时不起作用?

  3. 当你说汇编是未嵌入的时候,你的意思是什么?

基本上,从我所理解的,你有代码它尝试使用2个程序集中的类。这个代码是 static ,这意味着这些类必须在编译时才知道。或者换句话说,您必须参考这2个程序集才能编译使用其中定义的类型的代码。

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.

我真的不明白什么当您添加这些程序集作为参考时出错。如果您感到困扰,那么您看到它们被复制到 bin / debug 目录中,这仍然不应该阻止它们也被嵌入到主程序集中。所以要测试,您可以尝试从 bin / debug 手动删除它们,或者将其设置为copy local = false。

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".

更多的东西 - 你遇到的提到使用和命名空间的错误并不是命名空间。这个错误意味着编译器没有找到它需要的类型。这可能是因为您删除了对2个程序集的引用。

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.

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

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