无法加载我在

时间:2016-11-05 22:48:55

标签: .net system.reflection

我有一个案例,其中2个C#项目需要互相引用。因此,程序集A引用程序集B.而程序集B使用反射来加载A.在命令行应用程序中运行良好。

但在我的Word COM AddIn中我收到以下错误:

Could not load file or assembly 'WindwardReports, Version=15.0.142.0, Culture=neutral,
PublicKeyToken=34ffe15f4bbb8e53' or one of its dependencies.
The system cannot find the file specified.

FusionLog

=== Pre-bind state information ===
LOG: DisplayName = WindwardReports, Version=15.0.142.0, Culture=neutral, PublicKeyToken=34ffe15f4bbb8e53 (Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Office/Root/Office16/
LOG: Initial PrivatePath = NULL Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\\Program Files (x86)\\Microsoft Office\\Root\\Office16\\WINWORD.EXE.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\config\\machine.config.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Office/Root/Office16/WindwardReports.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Office/Root/Office16/WindwardReports/WindwardReports.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Office/Root/Office16/WindwardReports.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Office/Root/Office16/WindwardReports/WindwardReports.EXE.

这个相同的代码在命令行应用程序中运行良好。

代码: 这是代码。 (语法有点奇怪,因为这是使用IKVM转向.NET的Java代码,但在运行时它是.NET代码。)

    cli.System.Reflection.Assembly assm;
    int indexSemi = outputBuilder.indexOf(';');
    if (indexSemi != -1) {
        String dllFilename = outputBuilder.substring (0, indexSemi);
        outputBuilder = outputBuilder.substring(indexSemi + 1);
        assm = cli.System.Reflection.Assembly.LoadFile(dllFilename);
    }
    else
        assm = cli.System.Reflection.Assembly.GetExecutingAssembly();

    cli.System.Runtime.Remoting.ObjectHandle hdl = cli.System.Activator.CreateInstance(assm.get_FullName(), outputBuilder);
    return (IOutputBuilderEngine) hdl.Unwrap();

System.Activator.CreateInstance()正在抛出异常。

1 个答案:

答案 0 :(得分:0)

代码正在尝试从文件中加载DLL。

从融合日志中可以看出,它在四个地方寻找你的DLL,然后放弃:

  1. C:/ Program Files(x86)/ Microsoft Office / Root / Office16 / WindwardReports.DLL
  2. C:/ Program Files(x86)/ Microsoft Office / Root / Office16 / WindwardReports / WindwardReports.DLL
  3. C:/ Program Files(x86)/ Microsoft Office / Root / Office16 / WindwardReports.EXE
  4. C:/ Program Files(x86)/ Microsoft Office / Root / Office16 / WindwardReports / WindwardReports.EXE
  5. 好像你的DLL位于其他地方。如果您确实需要从磁盘加载它,那么您将需要正确的路径。虽然如果它是执行组件,为什么可以'你只需使用Assembly.GetExecutingAssembly()