C# ActiveX 开发时反序列化化提示找不到DLL解决方法

问题现象分析:

出现上述问题的根本原因是在IE浏览器中调用ActiveX的控件的程序根目录是,IE的目录,当时AcitvX控件的文件一般放置在自定义的文件目录中,在进行反序列化时,程序默认在IE的目录中寻找所需要的DLL文件,但是IE 目录中根本就没有这个文件,所以反序列化失败。

问题解决方法:

我们在加载DLL失败的时候,需要再次指定正确的DLL文件所在目录。在C# 中恰好有这个事件。重新默认应用程序域的事件AssemblyResolve 

  AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

 /// <summary>
        /// 加载非默认位置的程序集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e)
        {
            //手动指定程序集的位置
            if (e.Name.Split(',')[0] == "ExinGraphics2")
            {
                string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                Environment.CurrentDirectory = dir;
                string path = System.IO.Path.Combine(Environment.CurrentDirectory, "ExinGraphics2.dll");
                return Assembly.LoadFrom(path);
            }
            else
            {
                return null;
               
            }
        }


猜你喜欢

转载自blog.csdn.net/lishiming0308/article/details/11483365