Click to See Complete Forum and Search --> : OnLoad Event for Assemblies


monalin
September 2nd, 2009, 06:54 PM
Does such a thing exist? Or can I do something similar. Let me explain what i'm doing.

I'm attempting to embed a 3rd party dll inside my dll so when I ship it I don't have 3 or 4 different DLL's I only want one packaged up. Is there a way to do this without the use of an external application to merge the assemblies into one?

Right now I have the 3rd party dll as an embedded resource in a resx file. I've added the dll as a project reference and set it to not copy to the output directory.

I found an event that I can attach to that fires when the AppDomain fails to find an assembly in that event all the code works to load the assembly from the resource file embedded in the dll.

The problem I'm having is registering the event as soon as the parent dll is loaded. Right now I have to do something like this to register to that event so my function runs when it looks for a dll.


Resources.AssemblyLoader.Init();


This is what my function looks like so you get an idea


public class AssemblyLoader
{
static AssemblyLoader()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
ResourceManager resourceManager = new ResourceManager("Resources.ExternalAssembly", typeof(ExternalAssembly).Assembly);
Byte[] bytes = (Byte[])resourceManager.GetObject(args.Name.Split(',')[0].Replace('.','_'));

return bytes != null
? Assembly.Load(bytes)
: null;
};
}

public static void Init() { }
}


Any suggestions? Is there a better way to do this? Everything works I just don't like requiring the consuming application to call Resources.AssemblyLoader.Init() when it loads. This isn't intuitive for the programmer and will cause a lot of headscratching if the whole process isn't self contained inside the dll.

monalin
September 2nd, 2009, 07:17 PM
I managed to make a wrapper class around the class which I needed from the 3rd party assembly and I just Initalize the AssemblyLoader in the static constructor of that class. I'm not very happy with this solution but at least its better than my previous solution.

Still interested in what you guys have to say though. Let me know.