Invalid Types in VB.NET
I have a function which returns a list of products and a page which displays then, but when I try to load the list on the page, I get a cryptic error:
[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=b77a5c561934e0开发者_如何学编程89'
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'.
To me, the two types appear identical. The exception is thrown on the line:
Dim products As List(Of Product) = AppDAL.FindProducts(New ProductSearchEventArgs(SearchText, ProductSearchEventArgs.TextCriteria.Contains))
The signature of my method is:
Public Shared Function FindProducts(e As ProductSearchEventArgs) As List(Of Product)
Neither Product, the page which is calling the FindProducts method or the AppDAL class have namespaces, I can't figure out why this doesn't work
I suspect the problem is that you have 2 separate definitions of Product
, perhaps by copying the class file between projects; that is not sufficient, as types are defined by their assembly. You need a reference between the projects, so they both use the same Type.
While you might not of set a namespace in code, VB always has a default namespace name in the project properties. Have you tried Dim products As List(Of AppDAL.Product) =
?
I have run into a similar problem; after a lot of frustration it disappeared after restarting VS, forcing a full get (if you are using source control) and doing a full-rebuild. I have no idea if this will help in your situation.
精彩评论