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)
Product、调用 FindProducts 方法的页面或 AppDAL 类都没有命名空间,我不明白为什么这不起作用
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=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'.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑问题在于您有 2 个单独的
Product
定义,可能是通过在项目之间复制类文件来实现的;这还不够,因为类型是由它们的程序集定义的。您需要项目之间的引用,因此它们都使用相同的类型。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.虽然您可能不会在代码中设置命名空间,但 VB 在项目属性中始终具有默认的命名空间名称。您是否尝试过
Dim products As List(Of AppDAL.Product) =
?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) =
?我也遇到过类似的问题;经过很多挫折后,它在重新启动 VS 后消失,强制完全获取(如果您使用源代码控制)并进行完全重建。我不知道这对你的情况是否有帮助。
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.