developer82
developer82

Reputation: 13713

Class library as embedded resource - can it be done?

I have a .NET solution that I'm writing that the final result is a class library (I'm supposed to give the client a DLL file that he could reference and use in his project).

My solution includes:

When I publish, as expected, I get to DLL files - one for MyClassLib and one for Tools.

I only want to give the client one DLL file (MyClassLib) and that DLL will know how to use Tools even if it's not copied with it.

I was wondering if there's any way to embed the Tools reference inside MyClassLib. If so, how? if not, what would you have done in such a case?

Thanks

Upvotes: 1

Views: 3635

Answers (3)

PauLEffect
PauLEffect

Reputation: 423

Old, but will post for future seekers.

Build a class like so:

class Extractor
{
    public static void ExtractResourceToFile(string resourceName, string filename)
    {
        if (!System.IO.File.Exists(filename))
            using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
            {
                byte[] b = new byte[s.Length];
                s.Read(b, 0, b.Length);
                fs.Write(b, 0, b.Length);
            }
    }
}

Then, add

Extractor.ExtractResourceToFile("NAMESPACE.YOURDLL.dll", "YOURDLL.dll");

to your main file.

In order for this to work, simply Add as existing file , with the build action Embedded resource.

Works with any third party dlls, and you can of course use both dll import or referencing.

Upvotes: 3

Nir
Nir

Reputation: 29594

This post by Jeffrey Richter shows you how to do this.

Basically you use the AppDomain.CurrentDomain.AssemblyResolve event to intercept the assembly load and return the assembly from a resource.

Alternatively you can use ILMerge, a utility that can combine multiple assemblies into a single file.

Upvotes: 3

LueTm
LueTm

Reputation: 2380

I had amazing results with LibZ. You can just merge everything together. I used it to merge everything into an installer .exe, but it works for .dlls just as well.

Upvotes: 0

Related Questions