What’s the best practice for bundling one assembly in another? I have an assembly I’m distributing, but I have a couple third-party assemblies that I use in it, and I don’t want to have to distribute more than one.
So, I’d like to compile a couple assemblies into the one I distribute so that they’re just built-in.
What’s the best practice for this? I know I can put the other assemblies in my project and set them to ’embedded resource,’ but how do you get them back out again, meaning how do you get it to a place where I can instantiate classes defined in that embedded assembly?
I’ve seen the Assembly.Load method, but it seems to want a file path. Is there another way to load embedded assemblies? How do you tell the Load method where the assembly is?
Once you load the embedded assembly, is it just magically in scope and can I freely instantiate classes from it?
There’s an Assembly.Load(byte[]) overload that you can use to load assemblies from an in memory array. If I remember correctly, that’s the way LINQPad works for instance. Here’s a way to do it, given assembly being an instance of System.Reflection.Assembly, containing other assemblies as managed resources:
The issue with this one is that the runtime have to pin the byte array, so I’d suggest writing the assembly to a temporary file and use Assembly.LoadFile.
Both methods return a System.Reflection.Assembly object, that you can use to get types and then do whatever you want with them.