开发者

Merge .dll into C# Assembly?

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


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

  1. What exactly isn't working? Do you fail at compile time or at run time?
  2. Can you show the code that does not compile, or does not work at run time?
  3. What do you mean when you say the assemblies are "un-embedded"?

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.


Put it in the constructor, not InitializeComponent(). Adding using directives to your main class source code file isn't a problem.


String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll";

should be changed to:

String resourceName = Application.Current.GetType().Namespace + "." + new AssemblyName(args.Name).Name + ".dll";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜