.NET 3.5 - 对象未实现 IComparable?

发布于 2024-08-09 11:20:55 字数 707 浏览 5 评论 0原文

当将项目(其中多次使用 IComparable 的模板方法)从 VS 2005 转换为 VS 2008 时,我遇到了一些错误:

Error 12 Type argument 'Object' does not inherit from or implement 
the constraint type 'System.IComparable'.

这是 System.Object 不再实现该接口的实际事实,还是出了问题在转换期间?我能以某种方式解决这个问题吗?

问题在于以下方法:

Public Function ValueIn(Of T As IComparable)(ByVal pValue As T, ByVal ParamArray pArgs() As T) As Boolean
    For Each MyArg As T In pArgs
        If pValue.CompareTo(MyArg) = 0 Then
            Return True
        End If
    Next
    Return False
End Function

甚至像以下这样简单的方法:

Dim a as Object = 1
ValueIn(a,1,2)

也会导致上述错误。它在 VS 2005 中运行得很好,那么现在有什么问题呢?

When converting a project (in which a template method of IComparable was used a few times) from VS 2005 to VS 2008 I've got some errors:

Error 12 Type argument 'Object' does not inherit from or implement 
the constraint type 'System.IComparable'.

Is this an actual fact that System.Object no longer implements that interface, or something went wrong during the convertion? Can I fix this somehow?

The problem is with the following method:

Public Function ValueIn(Of T As IComparable)(ByVal pValue As T, ByVal ParamArray pArgs() As T) As Boolean
    For Each MyArg As T In pArgs
        If pValue.CompareTo(MyArg) = 0 Then
            Return True
        End If
    Next
    Return False
End Function

and even something simple like:

Dim a as Object = 1
ValueIn(a,1,2)

causes the error mentioned above. It worked perfectly in VS 2005, so what can be the problem now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

爱,才寂寞 2024-08-16 11:20:55

编辑:我刚刚在 VS 2005 和 2008 中尝试了您的代码。

您在项目或源代码文件中配置了Option Strict Off。你的代码从一开始就不起作用,如果你在 VS 2005 中设置 Option Strict On,你会看到错误的真正原因,即“类型参数‘T’的类型参数推断失败” ”。我建议在所有 VB.NET 代码中使用Option Strict On

您在 VS 2008 中会看到不同的错误,因为它使用的是较新版本的语言,具有非常不同的重载和类型推断规则。在 VB.NET 2008 中,无论 Option Strict 打开还是关闭,编译器都无法解析方法调用。

System.Object 类型没有也从未实现过任何接口。

VS 2008 中Option Infer 的设置与您的代码无关,因为它不使用任何推断类型。

修复两个 IDE 中的错误的最简单方法是更改​​调用代码:

    Dim a As Integer = 1
    ValueIn(a, 1, 2)

EDIT: I have just tried your code in both VS 2005 and 2008.

You have Option Strict Off configured in your project or source code file. Your code never worked in the first place, and if you set Option Strict On in VS 2005, you will see the real cause of the error, which is "Type argument inference failed for type parameter 'T'". I recommend that Option Strict On be used in all VB.NET code.

You see a different error in VS 2008 because it is using a newer version of the language, with very different overloading and type inference rules. In VB.NET 2008, the compiler cannot resolve the method call regardless of whether Option Strict is on or off.

The System.Object type does not and has never implemented any interface.

The setting of Option Infer in VS 2008 is not relevant to your code because it does not make use of any inferred types.

The simplest way to fix the error in both IDEs is to change the calling code thus:

    Dim a As Integer = 1
    ValueIn(a, 1, 2)
掀纱窥君容 2024-08-16 11:20:55

如果您在 Visual Studio 2005 中运行调试代码,您将看到

Dim a as Object = 1

中的 a是一个整数,但如果您使用 2008,它将声明它是一个对象。

Integer 实现了 IComparable 接口,但 Object 没有。
那么该怎么办呢?
答案:获得项目属性(右键单击项目名称并选择属性),进入“编译”下,现在除了 2005 年的显式、严格和比较之外,还有一个名为 Infer 的新字段。更改该值。

现在我们祈祷这会奏效。

If you run the debug the code in Visual Studio 2005 you will see that the a from

Dim a as Object = 1

is an Integer but if you use 2008 it will state that it's an Object.

Integer have the interface IComparable impelemented but not Object.
So what to do?
Answer: Got the the projects properties (right click the project name and select properties), Go in under 'Compile' and there you now have except the explicit, strict and compare, that you had in 2005, an new field named Infer. Change that value.

And now we cross our fingers that this will work.

め可乐爱微笑 2024-08-16 11:20:55

System.Object 是 IComparable 吗?那是如何运作的? IEquatable 我可以理解,但 IComparable 没有意义。

你能扩展一下“模板方法”吗?也许这就是一个线索。

System.Object was IComparable? How did that work? IEquatable I can understand but IComparable doesn't make sense.

Can you expand on "template method"? Maybe that's a clue.

风启觞 2024-08-16 11:20:55

我 99% 确信这是由 .Net 3.5 中模板类 IComparable 的更改引起的。我见过几个早期的 .NET 示例,它们工作正常,但在 3.5 中生成错误。

I'm 99% sure this is caused by a change in .Net 3.5 with the template class IComparable. I've seen a couple of earlier .NET examples that work fine but generate errors in 3.5.

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