Program.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Windows.Forms;
  5. using Cbdx.Tests.CefRelated;
  6. namespace Cbdx.Tests
  7. {
  8. internal static class Program
  9. {
  10. /// <summary>
  11. /// 应用程序的主入口点。
  12. /// </summary>
  13. [STAThread]
  14. private static void Main()
  15. {
  16. RegisterAssemblyResolvePolicy();
  17. InitReferenceAssembly();
  18. Application.EnableVisualStyles();
  19. Application.SetCompatibleTextRenderingDefault(false);
  20. Application.Run(new TestForm());
  21. QuitReferenceAssembly();
  22. }
  23. private static void RegisterAssemblyResolvePolicy()
  24. {
  25. AppDomain.CurrentDomain.AssemblyResolve += OnCurrentDomainOnAssemblyResolve;
  26. }
  27. private static void InitReferenceAssembly()
  28. {
  29. CefInit.Init();
  30. }
  31. private static void QuitReferenceAssembly()
  32. {
  33. CefInit.Quit();
  34. }
  35. private static Assembly OnCurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
  36. {
  37. //1.search in reqAsm dir + arch
  38. //2.search in reqAsm dir
  39. //3.search in myAsm dir + arch
  40. //4.search in myAsm dir
  41. var asm = LoadFromNear(args1.RequestingAssembly, args1.Name);
  42. return null == asm
  43. ? LoadFromNear(Assembly.GetExecutingAssembly(), args1.Name)
  44. : null;
  45. }
  46. private static Assembly LoadFromNear(Assembly reqAsm, string name)
  47. {
  48. var arch = Environment.Is64BitProcess ? "x64" : "x86";
  49. var fileName = new AssemblyName(name).Name;
  50. string reqAsmDirectory;
  51. if (false != reqAsm?.GlobalAssemblyCache ||
  52. string.IsNullOrEmpty(reqAsmDirectory = Path.GetDirectoryName(reqAsm.Location))) return null;
  53. string pathToLoad;
  54. //1,3
  55. if (File.Exists(pathToLoad = Path.Combine(reqAsmDirectory, arch, fileName) + ".dll")
  56. || File.Exists(pathToLoad = Path.Combine(reqAsmDirectory, arch, fileName) + ".exe"))
  57. {
  58. return Assembly.LoadFrom(pathToLoad);
  59. }
  60. //2,4
  61. if (File.Exists(pathToLoad = Path.Combine(reqAsmDirectory, fileName) + ".dll")
  62. || File.Exists(pathToLoad = Path.Combine(reqAsmDirectory, fileName) + ".exe"))
  63. {
  64. return Assembly.LoadFrom(pathToLoad);
  65. }
  66. return null;
  67. }
  68. }
  69. }