C#: Custom assembly directory

前端 未结 4 931
再見小時候
再見小時候 2020-11-27 17:33

Say we have an application which consists of one executable and 5 libraries. Regularly all of these will be contained in one directory and the libraries will be loaded from

4条回答
  •  借酒劲吻你
    2020-11-27 17:48

    If you want to manually manage where to get assembles from, you have two possibilities:

    1. Handle the AppDomain.AssemblyResolve event (as it is described by SLaks). Here is the code snippet:

      static void Main(string[] args)
      {
         ...
         AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
         ...
      }
      
      static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
      {
         string assembliesDir = "your_directory";
         Assembly asm = Assembly.LoadFrom(Path.Combine(assembliesDir, args.Name + ".dll"));
         return asm;
      }
      
    2. Create a domain with your own settings (by setting ApplicationBase or PrivateBinPath properties of the AppDomainSetup object).

    So if you want to continue working in current domain you have to use method 1.

提交回复
热议问题