2016-11-05 29 views
0

我有一個案例,2個C#項目需要相互引用。因此,程序集A引用程序集B並且程序集B使用反射來加載A.在命令行應用程序中很好地工作。無法加載我正在執行的程序集

,但在我的Word COM加載項我收到以下錯誤:

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()是什麼引發異常。

+0

你可以發佈你的代碼嗎? – Aruna

+0

@阿魯納 - 對不起,我應該在一開始就這樣做。現在在那裏。 –

+0

你的程序集(A和B)是如何部署的 - 就像它們應該駐留在文件系統中的位置一樣?你是否傳遞LoadFile方法的完整路徑? – cynic

回答

0

代碼試圖從文件中加載DLL。

你可以從融合日誌中看到,它看起來在四個地方你的DLL,然後放棄:

  1. C:/ Program Files文件(86)/微軟辦公室/根/ Office16/WindwardReports。 DLL
  2. C:/ Program Files文件(86)/微軟辦公室/根/ Office16/WindwardReports/WindwardReports.DLL
  3. C:/ Program Files文件(86)/微軟辦公室/根/ Office16/WindwardReports.EXE
  4. C:/ Program Files(x86)/ Microsoft Office/Root/Office16/WindwardReports/WindwardReports.EXE

好像你的DLL位於別的地方。如果您確實需要從磁盤加載它,則需要正確的路徑。雖然如果它是正在執行的程序集,爲什麼可以'只使用Assembly.GetExecutingAssembly()

+0

我在WindwardReports.dll中運行,試圖加載OfficeOutputBuilder.dll。但爲什麼它搜索我正在運行的DLL? –

+0

你沒有顯示足夠的代碼來知道。調查'outputBuilder'的值。它似乎包含錯誤的DLL名稱。 –