找不到应在哪里组装

时间:2019-05-12 23:29:37

标签: c# .net winforms dll .net-assembly

在我的.NET 4.6.2 winforms应用程序中,使用C#,我具有以下文件夹结构:

  • C:\ path \ AppFolder
  • C:\ path \ AppFolder \ Drivers

Main Exe在AppFolder中。在Drivers文件夹中,我有2个DLL文件,例如A.DLL和B.DLL。

在设计时,主Exe和A.DLL都引用了B.DLL。

A.DLL是在运行时由B.DLL加载的。

B.DLL中用于加载A.DLL的代码如下:

    public void LoadParsers()
    {
        string debug = "Uno\r\n";

        try
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Drivers");
            // Carga los drivers de los equipos
            string[] assemblies = System.IO.Directory.GetFiles(path, "*.bcx");

            debug += " Uno.5\r\n";

            foreach (string a in assemblies)
            {
                debug += " Dos " + a + "\r\n";
                //_eventLog.WriteEntry(System.IO.Path.GetFullPath(a));
                System.Reflection.Assembly assembly = null;
                try
                {
                    assembly = System.Reflection.Assembly.LoadFile(System.IO.Path.GetFullPath(a));
                    foreach (Type t in assembly.GetTypes())
                    {
                        debug += " Tres " + t.GetType().ToString() + "\r\n";
                        if (t.GetInterface("IParserPlugin") != null)
                        {
                            try
                            {
                                debug += " Cuatro " + t.GetInterface("IParserPlugin").ToString() + "\r\n";
                                IParserPlugin parser = Activator.CreateInstance(t) as IParserPlugin;
                                Parsers.Add(parser);
                            }
                            catch(Exception ex)
                            {
                                debug += " Cinco " + ex.Message + "\r\n";
                                OnError(this.FriendlyName, "-1", ex.Message);
                            }
                        }
                    }
                }
                catch(Exception ex)
                {
                    debug += " Seis " + ex.Message + "\r\n";
                    foreach (var excep in ((System.Reflection.ReflectionTypeLoadException)ex).LoaderExceptions)
                        debug += " Seis.5 " + excep.Message + "\r\n";
                    OnError(this.FriendlyName, "-1", ex.Message);
                }
            }
        }
        catch(Exception ex)
        {
            debug += " Siete " + ex.Message + "\r\n";
            OnError(this.FriendlyName, "-1", ex.Message);
        }

        System.IO.File.WriteAllText("debug.log", debug);
    }

    #endregion
}

您会发现* .bcx将A.DLL重命名为A.BCX,以便与其他DLL区别开来。

在运行时,它会在System.Reflection.Assembly.LoadFile指令中引发异常。

错误是它

  

无法加载程序集B或其依赖项之一。

通过使用Assembly Binding Log Viewer,我看到Assembly A试图从上一级文件夹而不是从Drivers文件夹中加载Assembly B。

那是为什么?更重要的是,如何解决?

致谢

Jaime

0 个答案:

没有答案
相关问题