我一直在將大型VS C#項目分解爲更小的項目,儘管當它是一個項目時一切正常,但我現在發現錯誤,因爲我已經將它分開。當我嘗試投射時,會拋出異常,但我沒有更改任何代碼。例外情況如下:導致Casting異常的DLL問題
InvalidCastException
[A]MyApplication.MyProject.MyNamespace.Class cannot be cast to [B]MyApplication.MyProject.MyNamespace.Class. Type A originates from 'MyProgram, Version=2.4.0.46, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\rc\78e25ad5\28e4b7d\assembly\dl3\877f6451\b808fef4_4e19cb01\MyProgram.DLL'. Type B originates from 'MyProgram, Version=2.4.0.46, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Source\view\Application\Version\core\bin\MyProgram.dll'.
正如您所看到的,演員中的兩種類型是相同的,唯一的區別是上下文和位置。稱爲GetHelperPage()的方法是問題的根源 - 它是外部應用程序中的外部方法。這通常不會是一個問題,除了外部應用程序然後回調此dll。該方法是這樣的:
[DllImport("EpsComHelper.dll", EntryPoint = "EPSCOMClientGetPage", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int HelperGetPage(uint pClient, String sPage, StringBuilder sBuff, int nBuffSize);
這種外部程序有DLL的正確路徑: 「C:\來源\圖\應用\版本\核心\ BIN \ MyProgram.dll」。但是,VS正在將DLL加載到它的臨時文件中並從那裏運行,就像您在上面的例外中的路徑中看到的一樣。這使得它看起來在兩個相同的dll中,這導致了錯誤。我看到在Reflection and casting,但我沒有ConfigurationManager.AppSettings [「DLL_File_Path」]或Assembly.LoadFrom(「path.dll」)在我的解決方案中的任何地方的響應。實際上,程序集或加載從或dll的位置永遠不會在解決方案中顯着提及。我能想到的是,這在某種程度上是由VS在幕後完成的,我無法找到任何可以改變它的地方。任何幫助將非常感激。
爲錯誤的調用堆棧:
RedCarpetCore.dll!RedCarpet.Core.EpsInProcess.EpsConnector.ChangeLogLevel(string[] args = {string[3]}) Line 3727 + 0x1d bytes
DotNetBridge.DLL!<Module>.RunMethodInProcess(sbyte* szAssembly = 0x035b7db4, sbyte* szFullyQualifiedName = 0x08ebfcf4, sbyte* szClientPtr = 0x0bccd26c, int nArgs = 2, sbyte** arrArgs = 0x0263190c) + 0x43f bytes
[Native to Managed Transition]
DotNetScriptPlugin.dll!034d9e5f()
ntdll.dll!7c827a29()
kernel32.dll!77e6570a()
MSVCR71.DLL!7c352d9b()
MSVCR71.DLL!7c3531c5()
MSVCR71.DLL!7c352e69()
MSVCR71.DLL!7c36a582()
MSVCR71.DLL!7c34f9a2()
MSVCR71.DLL!7c34f9a2()
MSVCR71.DLL!7c350135()
MSVCR71.DLL!7c36a582()
eprise.dll!01d15fa7()
eprevent.dll!01e218f0()
eprevent.dll!01e2ec73()
eprise.dll!01ccf427()
wcc200.dll!01c0a6c5()
oleaut32.dll!77d04141()
[Managed to Native Transition]
RedCarpetCore.DLL!RedCarpet.Core.EpsInProcess.EpsClient.GetPage(string s = "/sysinfo") Line 318 + 0x1a bytes
你會注意到DLL獲取所有混亂的地方切換。首字母大寫的dll位於臨時文件中,最後是小寫字母的dll位於文件夾中。