AssemblyResolveInMyAround.cs 1.0 KB

123456789101112131415161718192021222324252627282930
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. namespace NppChnConvPlugin
  5. {
  6. internal static class AssemblyResolveInMyAround
  7. {
  8. /// <summary>
  9. /// Patch assembly version range
  10. /// </summary>
  11. public static void Register()
  12. {
  13. var executingAssemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? throw new InvalidOperationException("Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) is null");
  14. AppDomain.CurrentDomain.AssemblyResolve += delegate (object sender, ResolveEventArgs args)
  15. {
  16. var pathWithoutExtension = Path.Combine(executingAssemblyDir, new AssemblyName(args.Name).Name);
  17. var asmPath = pathWithoutExtension + ".dll";
  18. if (File.Exists(asmPath)) return Assembly.LoadFrom(asmPath);
  19. asmPath = pathWithoutExtension + ".exe";
  20. if (File.Exists(asmPath)) return Assembly.LoadFrom(asmPath);
  21. return null;
  22. };
  23. }
  24. }
  25. }