using System; using System.IO; using System.Reflection; using System.Windows.Forms; using Cbdx.Tests.CefRelated; namespace Cbdx.Tests { internal static class Program { /// /// 应用程序的主入口点。 /// [STAThread] private static void Main() { RegisterAssemblyResolvePolicy(); InitReferenceAssembly(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestForm()); QuitReferenceAssembly(); } private static void RegisterAssemblyResolvePolicy() { AppDomain.CurrentDomain.AssemblyResolve += OnCurrentDomainOnAssemblyResolve; } private static void InitReferenceAssembly() { CefInit.Init(); } private static void QuitReferenceAssembly() { CefInit.Quit(); } private static Assembly OnCurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args) { //1.search in reqAsm dir + arch //2.search in reqAsm dir //3.search in myAsm dir + arch //4.search in myAsm dir var asm = LoadFromNear(args1.RequestingAssembly, args1.Name); return null == asm ? LoadFromNear(Assembly.GetExecutingAssembly(), args1.Name) : null; } private static Assembly LoadFromNear(Assembly reqAsm, string name) { var arch = Environment.Is64BitProcess ? "x64" : "x86"; var fileName = new AssemblyName(name).Name; string reqAsmDirectory; if (false != reqAsm?.GlobalAssemblyCache || string.IsNullOrEmpty(reqAsmDirectory = Path.GetDirectoryName(reqAsm.Location))) return null; string pathToLoad; //1,3 if (File.Exists(pathToLoad = Path.Combine(reqAsmDirectory, arch, fileName) + ".dll") || File.Exists(pathToLoad = Path.Combine(reqAsmDirectory, arch, fileName) + ".exe")) { return Assembly.LoadFrom(pathToLoad); } //2,4 if (File.Exists(pathToLoad = Path.Combine(reqAsmDirectory, fileName) + ".dll") || File.Exists(pathToLoad = Path.Combine(reqAsmDirectory, fileName) + ".exe")) { return Assembly.LoadFrom(pathToLoad); } return null; } } }