2011-05-07 92 views
2

我有它返回的產品清單,並隨後顯示頁面的功能,但是當我嘗試加載頁面的列表中,我得到一個神祕的錯誤:無效的類型在VB.NET

[A]System.Collections.Generic.List`1[Product] cannot be cast to 
[B]System.Collections.Generic.List`1[Product]. 
Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 
in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. 
Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 
in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. 

對我來說,這兩種類型看起來完全相同。拋出異常就行了:

Dim products As List(Of Product) = AppDAL.FindProducts(New ProductSearchEventArgs(SearchText, ProductSearchEventArgs.TextCriteria.Contains)) 

我的方法的簽名是:

Public Shared Function FindProducts(e As ProductSearchEventArgs) As List(Of Product) 

無論是產品,這是調用FindProducts方法或AppDAL類的頁面有命名空間,我可以」不知道爲什麼這不起作用

+1

您是否嘗試過重建項目並重新啓動Visual Studio? – 2011-05-07 15:16:47

回答

0

雖然您可能無法在代碼中設置命名空間,但VB始終在項目屬性中具有默認命名空間名稱。你有沒有試過Dim products As List(Of AppDAL.Product) =

+0

有趣。添加一個名稱空間解決了問題。 – Echilon 2011-05-07 15:50:21

2

我懷疑問題是你有0123'2個單獨的定義,也許通過複製項目之間的類文件;這是不夠的,因爲類型是由它們的組合來定義的。您需要項目之間的參考,因此它們都使用相同的類型。

+0

或者,您可能會在應用程序中對DAL具有的不同版本的dll有一個引用,這也可能導致此錯誤。 – Pondidum 2011-05-07 15:34:47

+0

@Pondidum - 通常只有在使用GAC時才適用;它是*可能的*,但在我的經驗班複製更爲常見 – 2011-05-07 15:39:51

0

我遇到了類似的問題;經過很多挫折之後,它在重新啓動VS之後消失了,強制完全獲得(如果您使用源代碼控制)並進行完全重建。我不知道這是否有助於你的情況。

+0

但是它在生產中發生了嗎?然後怎樣呢? – 2012-08-15 21:36:01