Reference a DLL from another DLL

后端 未结 3 820
我在风中等你
我在风中等你 2020-12-28 20:00

I have a C# application program, let\'s call it App.exe. It references a DLL named A.dll which in turn references another DLL, namely, B.dll. However the way in which they a

3条回答
  •  一向
    一向 (楼主)
    2020-12-28 20:45

    You can manually resolve the B.DLL assembly by providing an event handler to the AppDomain.AssemblyResolve event of the current AppDomain.

    currentDomain.AssemblyResolve += ResolveLostAssemblies;
    

    Then provide an implementation of ResolveEventHandler:

    private Assembly ResolveLostAssemblies(object sender, ResolveEventArgs args)
    {
        // Find the assembly referenced by args.Name, load it dynamically, and return it.
    }
    

    I've found that this provides the most control over which assemblies get loaded and when. It especially works well in the situation where you're application is pluggable and each plugin lives in their own subdirectory of a "plugins" folder (which isn't necessarily the application directory). Technically the assembly doesn't even have to be a physical file using this method. Though D Stanley's method is generally considered more standard.

提交回复
热议问题