We use this method in VB.NET in Visual Studio 2008 ...
First, the project must know in order to include the "other" dll as an embedded resource. In Solution Explorer, add the dll to the file in your project (and not as a link). Then open the “Properties for the file” and set the “Build actions” to “Embedded resource”. It is recommended that you create a local copy of the dll file in the structure of your project, and not contact any other location. After the project includes the DLL file, you can add a link to this copy of the dll so that you can use its contents during development.
This ensures that the "other" DLL is included in your compiled dll, but it does not load automatically. This is where the following code comes in:
Public Module Core Private _initialized As Boolean Public Sub EnsureInitialized() If Not _initialized Then AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf AssemblyResolve _initialized = True End If End Sub Private Function AssemblyResolve(ByVal sender As Object, ByVal e As ResolveEventArgs) As Assembly Dim resourceFullName As String = String.Format("[CONTAINER ASSEMBLY].{0}.dll", e.Name.Split(","c)(0)) Dim thisAssembly As Assembly = Assembly.GetExecutingAssembly() Using resource As Stream = thisAssembly.GetManifestResourceStream(resourceFullName) If resource IsNot Nothing Then Return Assembly.Load(ToBytes(resource)) Return Nothing End Using End Function Private Function ToBytes(ByVal instance As Stream) As Byte() Dim capacity As Integer = If(instance.CanSeek, Convert.ToInt32(instance.Length), 0) Using result As New MemoryStream(capacity) Dim readLength As Integer Dim buffer(4096) As Byte Do readLength = instance.Read(buffer, 0, buffer.Length) result.Write(buffer, 0, readLength) Loop While readLength > 0 Return result.ToArray() End Using End Function End Module
Put this module somewhere in your project, and be sure to call the EnsureInitialized method to bind the AssemblyResolve handler before calling any other code in your DLL.
NOTE. You need to replace [CONTAINER ASSEMBLY] with the name of your DLL.
Also note that the above code is a stripped-down version of what we actually use, because our system includes log4net logging of messages in strategic places. Log messages are not needed for true functionality, so I deleted them for brevity and clarity.
The main caveat to this approach is that the AssemblyResolve handler must be connected manually. Even if you cannot configure everything so that EnsureInitialized is called only once during the initialization of the consumption code, you can call EnsureInitialized in any of your own modules, which require a different dll to execute. This makes the code a bit more subtle because you have to remember to make this initialization call, but it allows you to sleep at night knowing that the DLL will be available when you need it.
In my experience, some "other" DLLs do not play very well when they are provided as embedded resources, so you may need to play a little to get it to work.
Final note: I have never used an ArcMap component, so your mileage may vary!