VB.NET 中的无效类型

发布于 2024-11-05 08:23:27 字数 1068 浏览 2 评论 0原文

我有一个返回产品列表和随后显示的页面的函数,但是当我尝试在页面上加载列表时,我收到一个神秘的错误:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作死小能手 2024-11-12 08:23:27

我怀疑问题在于您有 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.

揪着可爱 2024-11-12 08:23:27

虽然您可能不会在代码中设置命名空间,但 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) =?

一杯敬自由 2024-11-12 08:23:27

我也遇到过类似的问题;经过很多挫折后,它在重新启动 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文