.Net 中的 Version 类没有像我期望的那样实现 CompareTo 接口,它似乎以字母数字方式处理比较,而不是比较四个数字。也许不是错误,而是“功能”。
谁能解释一下为什么比较(以及标准的 <、= 和 > 运算符)不能像我在下面所期望的那样工作?
Dim MainVersion As New Version("1.1.3251.4029")
Dim Ver_Low As New Version("1.1")
Dim Ver_Same As New Version("1.1.3251.4029")
Dim Ver_High As New Version("1.1.5.0")
' CompareTo here yields 1 which is expected as MainVersion is greater than Ver_Low.
MessageBox.Show(String.Format("{0}.CompareTo({1}) = {2}", MainVersion.ToString(), Ver_Low.ToString(), MainVersion.CompareTo(Ver_Low).ToString()))
' CompareTo here yields 0 which is expected as MainVersion and Ver_Same are the same.
MessageBox.Show(String.Format("{0}.CompareTo({1}) = {2}", MainVersion.ToString(), Ver_Same.ToString(), MainVersion.CompareTo(Ver_Same).ToString()))
' **** Issue here **** CompareTo here yields 1 which is NOT expected as MainVersion is less than Ver_High.
MessageBox.Show(String.Format("{0}.CompareTo({1}) = {2}", MainVersion.ToString(), Ver_High.ToString(), MainVersion.CompareTo(Ver_High).ToString()))
我知道人们已经为此做了自己的手动解决方法,我想知道这是设计使然还是应该起作用,我可能会做一些愚蠢的事情。
提前致谢
瑞安
更新:我做了一些微妙的愚蠢的事情,并将它们视为 IP 地址。例如; 1.1.3023.5364 大于 1.1.5 但 1.1.3023.5364 小于 1.1.5000。