namespace Test { using System; using Microshaoft; public class Class1 { static void Main(string[] args) { Console.WriteLine("第一个\".\"和第二个\".\"之间的数字按小数比较,其他按整数比较"); StringVersion v1 = new StringVersion("12.0390.001"); StringVersion v2 = new StringVersion("21.039000.1"); Console.WriteLine("版本: [{0}][{1}] ", v1.ToString(), v2.ToString()); Console.WriteLine("GetHashCode: [{0}][{1}] ", v1.GetHashCode(), v2.GetHashCode()); Console.WriteLine("Equals , {0} ", v1.Equals(v2)); Console.WriteLine("== , {0} ", v1 == v2); Console.WriteLine("!= , {0} ", v1 != v2); Console.WriteLine("> , {0} ", v1 > v2); Console.WriteLine("< , {0} ", v1 < v2); Console.WriteLine(">= , {0} ", v1 >= v2); Console.WriteLine("<= , {0} ", v1 <= v2); Console.WriteLine(Environment.Version.ToString()); } } } namespace Microshaoft { using System; public class StringVersion { private string[] _versions; private string _version; protected string[] Versions { get { return _versions; } } public StringVersion(string version) { _version = version; char[] c = new char[]{'.'}; _versions = version.Split(c); } public override string ToString() { return _version; } public override bool Equals(object o) { StringVersion x = this; StringVersion y = o as StringVersion; int xl = x.Versions.Length; int yl = y.Versions.Length; for (int i = 0; i < xl || i < yl; i ++) { uint xi = 0; uint yi = 0; double xd = 0.0d; double yd = 0.0d; if (i < xl) { if (i != 1) { uint.TryParse(x.Versions[i], out xi); } else { double.TryParse ( string.Format("0.{0}", x.Versions[i]) , out xd ); } } if (i < yl) { if (i != 1) { uint.TryParse(y.Versions[i], out yi); } else { double.TryParse ( string.Format("0.{0}", y.Versions[i]) , out yd ); } } if (i == 1) { if (xd != yd) { return false; } } else { if (xi != yi) { return false; } } } return true; } public override int GetHashCode() { string[] a = new string[_versions.Length]; Array.Copy(_versions, 0, a, 0, a.Length); for(int i = 0; i < _versions.Length; i ++) { if (i == 1) { a[i] = _versions[i].TrimEnd('0'); } else { a[i] = _versions[i].TrimStart('0'); } } string s = string.Join(".", a); return s.GetHashCode(); } public static bool operator == (StringVersion x, StringVersion y) { return x.Equals(y); } public static bool operator != (StringVersion x, StringVersion y) { int xl = x.Versions.Length; int yl = y.Versions.Length; for (int i = 0; i < xl || i < yl; i ++) { uint xi = 0; uint yi = 0; double xd = 0.0d; double yd = 0.0d; if (i < xl) { if (i != 1) { uint.TryParse(x.Versions[i], out xi); } else { double.TryParse ( string.Format("0.{0}", x.Versions[i]) , out xd ); } } if (i < yl) { if (i != 1) { uint.TryParse(y.Versions[i], out yi); } else { double.TryParse ( string.Format("0.{0}", y.Versions[i]) , out yd ); } } if (i == 1) { if (xd != yd) { return true; } } else { if (xi != yi) { return true; } } } return false; } public static bool operator > (StringVersion x, StringVersion y) { int xl = x.Versions.Length; int yl = y.Versions.Length; for (int i = 0; i < xl || i < yl; i ++) { uint xi = 0; uint yi = 0; double xd = 0.0d; double yd = 0.0d; if (i < xl) { if (i != 1) { uint.TryParse(x.Versions[i], out xi); } else { double.TryParse ( string.Format("0.{0}", x.Versions[i]) , out xd ); } } if (i < yl) { if (i != 1) { uint.TryParse(y.Versions[i], out yi); } else { double.TryParse ( string.Format("0.{0}", y.Versions[i]) , out yd ); } } if (i == 1) { if (xd > yd) { return true; } else if (xd < yd) { return false; } } else { if (xi > yi) { return true; } else if (xi < yi) { return false; } } } return false; } public static bool operator < (StringVersion x, StringVersion y) { int xl = x.Versions.Length; int yl = y.Versions.Length; for (int i = 0; i < xl || i < yl; i ++) { uint xi = 0; uint yi = 0; double xd = 0.0d; double yd = 0.0d; if (i < xl) { if (i != 1) { uint.TryParse(x.Versions[i], out xi); } else { double.TryParse ( string.Format("0.{0}", x.Versions[i]) , out xd ); } } if (i < yl) { if (i != 1) { uint.TryParse(y.Versions[i], out yi); } else { double.TryParse ( string.Format("0.{0}", y.Versions[i]) , out yd ); } } if (i == 1) { if (xd < yd) { return true; } else if (xd > yd) { return false; } } else { if (xi < yi) { return true; } else if (xi > yi) { return false; } } } return false; } public static bool operator >= (StringVersion x, StringVersion y) { int xl = x.Versions.Length; int yl = y.Versions.Length; bool equal = true; for (int i = 0; i < xl || i < yl; i ++) { uint xi = 0; uint yi = 0; double xd = 0.0d; double yd = 0.0d; if (i < xl) { if (i != 1) { uint.TryParse(x.Versions[i], out xi); } else { double.TryParse ( string.Format("0.{0}", x.Versions[i]) , out xd ); } } if (i < yl) { if (i != 1) { uint.TryParse(y.Versions[i], out yi); } else { double.TryParse ( string.Format("0.{0}", y.Versions[i]) , out yd ); } } if (i == 1) { if (xd > yd) { return true; } else if (xd < yd) { return false; } equal = equal && (xd == yd); } else { if (xi > yi) { return true; } else if (xi < yi) { return false; } equal = equal && (xi == yi); } } return equal; } public static bool operator <= (StringVersion x, StringVersion y) { int xl = x.Versions.Length; int yl = y.Versions.Length; bool equal = true; for (int i = 0; i < xl || i < yl; i ++) { uint xi = 0; uint yi = 0; double xd = 0.0d; double yd = 0.0d; if (i < xl) { if (i != 1) { uint.TryParse(x.Versions[i], out xi); } else { double.TryParse ( string.Format("0.{0}", x.Versions[i]) , out xd ); } } if (i < yl) { if (i != 1) { uint.TryParse(y.Versions[i], out yi); } else { double.TryParse ( string.Format("0.{0}", y.Versions[i]) , out yd ); } } if (i == 1) { if (xd < yd) { return true; } else if (xd > yd) { return false; } equal = equal && (xd == yd); } else { if (xi < yi) { return true; } else if (xi > yi) { return false; } equal = equal && (xi == yi); } } return equal; } } }