mscorlib(4.0.0.0) API with additions
Version.cs
3 using System.Text;
4 
5 namespace System
6 {
9  [ComVisible(true)]
10  [__DynamicallyInvokable]
11  public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version>
12  {
13  internal enum ParseFailureKind
14  {
19  }
20 
21  internal struct VersionResult
22  {
23  internal Version m_parsedVersion;
24 
25  internal ParseFailureKind m_failure;
26 
27  internal string m_exceptionArgument;
28 
29  internal string m_argumentName;
30 
31  internal bool m_canThrow;
32 
33  internal void Init(string argumentName, bool canThrow)
34  {
35  m_canThrow = canThrow;
36  m_argumentName = argumentName;
37  }
38 
39  internal void SetFailure(ParseFailureKind failure)
40  {
41  SetFailure(failure, string.Empty);
42  }
43 
44  internal void SetFailure(ParseFailureKind failure, string argument)
45  {
46  m_failure = failure;
47  m_exceptionArgument = argument;
48  if (m_canThrow)
49  {
50  throw GetVersionParseException();
51  }
52  }
53 
54  internal Exception GetVersionParseException()
55  {
56  switch (m_failure)
57  {
58  case ParseFailureKind.ArgumentNullException:
59  return new ArgumentNullException(m_argumentName);
60  case ParseFailureKind.ArgumentException:
61  return new ArgumentException(Environment.GetResourceString("Arg_VersionString"));
62  case ParseFailureKind.ArgumentOutOfRangeException:
63  return new ArgumentOutOfRangeException(m_exceptionArgument, Environment.GetResourceString("ArgumentOutOfRange_Version"));
64  case ParseFailureKind.FormatException:
65  try
66  {
67  int.Parse(m_exceptionArgument, CultureInfo.InvariantCulture);
68  }
69  catch (FormatException result)
70  {
71  return result;
72  }
73  catch (OverflowException result2)
74  {
75  return result2;
76  }
77  return new FormatException(Environment.GetResourceString("Format_InvalidString"));
78  default:
79  return new ArgumentException(Environment.GetResourceString("Arg_VersionString"));
80  }
81  }
82  }
83 
84  private int _Major;
85 
86  private int _Minor;
87 
88  private int _Build = -1;
89 
90  private int _Revision = -1;
91 
92  private static readonly char[] SeparatorsArray = new char[1]
93  {
94  '.'
95  };
96 
97  private const int ZERO_CHAR_VALUE = 48;
98 
101  [__DynamicallyInvokable]
102  public int Major
103  {
104  [__DynamicallyInvokable]
105  get
106  {
107  return _Major;
108  }
109  }
110 
113  [__DynamicallyInvokable]
114  public int Minor
115  {
116  [__DynamicallyInvokable]
117  get
118  {
119  return _Minor;
120  }
121  }
122 
125  [__DynamicallyInvokable]
126  public int Build
127  {
128  [__DynamicallyInvokable]
129  get
130  {
131  return _Build;
132  }
133  }
134 
137  [__DynamicallyInvokable]
138  public int Revision
139  {
140  [__DynamicallyInvokable]
141  get
142  {
143  return _Revision;
144  }
145  }
146 
149  [__DynamicallyInvokable]
150  public short MajorRevision
151  {
152  [__DynamicallyInvokable]
153  get
154  {
155  return (short)(_Revision >> 16);
156  }
157  }
158 
161  [__DynamicallyInvokable]
162  public short MinorRevision
163  {
164  [__DynamicallyInvokable]
165  get
166  {
167  return (short)(_Revision & 0xFFFF);
168  }
169  }
170 
178  [__DynamicallyInvokable]
179  public Version(int major, int minor, int build, int revision)
180  {
181  if (major < 0)
182  {
183  throw new ArgumentOutOfRangeException("major", Environment.GetResourceString("ArgumentOutOfRange_Version"));
184  }
185  if (minor < 0)
186  {
187  throw new ArgumentOutOfRangeException("minor", Environment.GetResourceString("ArgumentOutOfRange_Version"));
188  }
189  if (build < 0)
190  {
191  throw new ArgumentOutOfRangeException("build", Environment.GetResourceString("ArgumentOutOfRange_Version"));
192  }
193  if (revision < 0)
194  {
195  throw new ArgumentOutOfRangeException("revision", Environment.GetResourceString("ArgumentOutOfRange_Version"));
196  }
197  _Major = major;
198  _Minor = minor;
199  _Build = build;
200  _Revision = revision;
201  }
202 
209  [__DynamicallyInvokable]
210  public Version(int major, int minor, int build)
211  {
212  if (major < 0)
213  {
214  throw new ArgumentOutOfRangeException("major", Environment.GetResourceString("ArgumentOutOfRange_Version"));
215  }
216  if (minor < 0)
217  {
218  throw new ArgumentOutOfRangeException("minor", Environment.GetResourceString("ArgumentOutOfRange_Version"));
219  }
220  if (build < 0)
221  {
222  throw new ArgumentOutOfRangeException("build", Environment.GetResourceString("ArgumentOutOfRange_Version"));
223  }
224  _Major = major;
225  _Minor = minor;
226  _Build = build;
227  }
228 
234  [__DynamicallyInvokable]
235  public Version(int major, int minor)
236  {
237  if (major < 0)
238  {
239  throw new ArgumentOutOfRangeException("major", Environment.GetResourceString("ArgumentOutOfRange_Version"));
240  }
241  if (minor < 0)
242  {
243  throw new ArgumentOutOfRangeException("minor", Environment.GetResourceString("ArgumentOutOfRange_Version"));
244  }
245  _Major = major;
246  _Minor = minor;
247  }
248 
258  [__DynamicallyInvokable]
259  public Version(string version)
260  {
261  Version version2 = Parse(version);
262  _Major = version2.Major;
263  _Minor = version2.Minor;
264  _Build = version2.Build;
265  _Revision = version2.Revision;
266  }
267 
269  public Version()
270  {
271  _Major = 0;
272  _Minor = 0;
273  }
274 
277  public object Clone()
278  {
279  Version version = new Version();
280  version._Major = _Major;
281  version._Minor = _Minor;
282  version._Build = _Build;
283  version._Revision = _Revision;
284  return version;
285  }
286 
293  public int CompareTo(object version)
294  {
295  if (version == null)
296  {
297  return 1;
298  }
299  Version version2 = version as Version;
300  if (version2 == null)
301  {
302  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeVersion"));
303  }
304  if (_Major != version2._Major)
305  {
306  if (_Major > version2._Major)
307  {
308  return 1;
309  }
310  return -1;
311  }
312  if (_Minor != version2._Minor)
313  {
314  if (_Minor > version2._Minor)
315  {
316  return 1;
317  }
318  return -1;
319  }
320  if (_Build != version2._Build)
321  {
322  if (_Build > version2._Build)
323  {
324  return 1;
325  }
326  return -1;
327  }
328  if (_Revision != version2._Revision)
329  {
330  if (_Revision > version2._Revision)
331  {
332  return 1;
333  }
334  return -1;
335  }
336  return 0;
337  }
338 
343  [__DynamicallyInvokable]
344  public int CompareTo(Version value)
345  {
346  if (value == null)
347  {
348  return 1;
349  }
350  if (_Major != value._Major)
351  {
352  if (_Major > value._Major)
353  {
354  return 1;
355  }
356  return -1;
357  }
358  if (_Minor != value._Minor)
359  {
360  if (_Minor > value._Minor)
361  {
362  return 1;
363  }
364  return -1;
365  }
366  if (_Build != value._Build)
367  {
368  if (_Build > value._Build)
369  {
370  return 1;
371  }
372  return -1;
373  }
374  if (_Revision != value._Revision)
375  {
376  if (_Revision > value._Revision)
377  {
378  return 1;
379  }
380  return -1;
381  }
382  return 0;
383  }
384 
389  [__DynamicallyInvokable]
390  public override bool Equals(object obj)
391  {
392  Version version = obj as Version;
393  if (version == null)
394  {
395  return false;
396  }
397  if (_Major != version._Major || _Minor != version._Minor || _Build != version._Build || _Revision != version._Revision)
398  {
399  return false;
400  }
401  return true;
402  }
403 
408  [__DynamicallyInvokable]
409  public bool Equals(Version obj)
410  {
411  if (obj == null)
412  {
413  return false;
414  }
415  if (_Major != obj._Major || _Minor != obj._Minor || _Build != obj._Build || _Revision != obj._Revision)
416  {
417  return false;
418  }
419  return true;
420  }
421 
424  [__DynamicallyInvokable]
425  public override int GetHashCode()
426  {
427  int num = 0;
428  num |= (_Major & 0xF) << 28;
429  num |= (_Minor & 0xFF) << 20;
430  num |= (_Build & 0xFF) << 12;
431  return num | (_Revision & 0xFFF);
432  }
433 
436  [__DynamicallyInvokable]
437  public override string ToString()
438  {
439  if (_Build == -1)
440  {
441  return ToString(2);
442  }
443  if (_Revision == -1)
444  {
445  return ToString(3);
446  }
447  return ToString(4);
448  }
449 
456  [__DynamicallyInvokable]
457  public string ToString(int fieldCount)
458  {
459  switch (fieldCount)
460  {
461  case 0:
462  return string.Empty;
463  case 1:
464  return _Major.ToString();
465  case 2:
466  {
467  StringBuilder stringBuilder = StringBuilderCache.Acquire();
468  AppendPositiveNumber(_Major, stringBuilder);
469  stringBuilder.Append('.');
470  AppendPositiveNumber(_Minor, stringBuilder);
471  return StringBuilderCache.GetStringAndRelease(stringBuilder);
472  }
473  default:
474  if (_Build == -1)
475  {
476  throw new ArgumentException(Environment.GetResourceString("ArgumentOutOfRange_Bounds_Lower_Upper", "0", "2"), "fieldCount");
477  }
478  if (fieldCount == 3)
479  {
480  StringBuilder stringBuilder = StringBuilderCache.Acquire();
481  AppendPositiveNumber(_Major, stringBuilder);
482  stringBuilder.Append('.');
483  AppendPositiveNumber(_Minor, stringBuilder);
484  stringBuilder.Append('.');
485  AppendPositiveNumber(_Build, stringBuilder);
486  return StringBuilderCache.GetStringAndRelease(stringBuilder);
487  }
488  if (_Revision == -1)
489  {
490  throw new ArgumentException(Environment.GetResourceString("ArgumentOutOfRange_Bounds_Lower_Upper", "0", "3"), "fieldCount");
491  }
492  if (fieldCount == 4)
493  {
494  StringBuilder stringBuilder = StringBuilderCache.Acquire();
495  AppendPositiveNumber(_Major, stringBuilder);
496  stringBuilder.Append('.');
497  AppendPositiveNumber(_Minor, stringBuilder);
498  stringBuilder.Append('.');
499  AppendPositiveNumber(_Build, stringBuilder);
500  stringBuilder.Append('.');
501  AppendPositiveNumber(_Revision, stringBuilder);
502  return StringBuilderCache.GetStringAndRelease(stringBuilder);
503  }
504  throw new ArgumentException(Environment.GetResourceString("ArgumentOutOfRange_Bounds_Lower_Upper", "0", "4"), "fieldCount");
505  }
506  }
507 
508  private static void AppendPositiveNumber(int num, StringBuilder sb)
509  {
510  int length = sb.Length;
511  do
512  {
513  int num2 = num % 10;
514  num /= 10;
515  sb.Insert(length, (char)(48 + num2));
516  }
517  while (num > 0);
518  }
519 
530  [__DynamicallyInvokable]
531  public static Version Parse(string input)
532  {
533  if (input == null)
534  {
535  throw new ArgumentNullException("input");
536  }
537  VersionResult result = default(VersionResult);
538  result.Init("input", canThrow: true);
539  if (!TryParseVersion(input, ref result))
540  {
541  throw result.GetVersionParseException();
542  }
543  return result.m_parsedVersion;
544  }
545 
551  [__DynamicallyInvokable]
552  public static bool TryParse(string input, out Version result)
553  {
554  VersionResult result2 = default(VersionResult);
555  result2.Init("input", canThrow: false);
556  bool result3 = TryParseVersion(input, ref result2);
557  result = result2.m_parsedVersion;
558  return result3;
559  }
560 
561  private static bool TryParseVersion(string version, ref VersionResult result)
562  {
563  if (version == null)
564  {
565  result.SetFailure(ParseFailureKind.ArgumentNullException);
566  return false;
567  }
568  string[] array = version.Split(SeparatorsArray);
569  int num = array.Length;
570  if (num < 2 || num > 4)
571  {
572  result.SetFailure(ParseFailureKind.ArgumentException);
573  return false;
574  }
575  if (!TryParseComponent(array[0], "version", ref result, out int parsedComponent))
576  {
577  return false;
578  }
579  if (!TryParseComponent(array[1], "version", ref result, out int parsedComponent2))
580  {
581  return false;
582  }
583  num -= 2;
584  if (num > 0)
585  {
586  if (!TryParseComponent(array[2], "build", ref result, out int parsedComponent3))
587  {
588  return false;
589  }
590  num--;
591  if (num > 0)
592  {
593  if (!TryParseComponent(array[3], "revision", ref result, out int parsedComponent4))
594  {
595  return false;
596  }
597  result.m_parsedVersion = new Version(parsedComponent, parsedComponent2, parsedComponent3, parsedComponent4);
598  }
599  else
600  {
601  result.m_parsedVersion = new Version(parsedComponent, parsedComponent2, parsedComponent3);
602  }
603  }
604  else
605  {
606  result.m_parsedVersion = new Version(parsedComponent, parsedComponent2);
607  }
608  return true;
609  }
610 
611  private static bool TryParseComponent(string component, string componentName, ref VersionResult result, out int parsedComponent)
612  {
613  if (!int.TryParse(component, NumberStyles.Integer, CultureInfo.InvariantCulture, out parsedComponent))
614  {
615  result.SetFailure(ParseFailureKind.FormatException, component);
616  return false;
617  }
618  if (parsedComponent < 0)
619  {
620  result.SetFailure(ParseFailureKind.ArgumentOutOfRangeException, componentName);
621  return false;
622  }
623  return true;
624  }
625 
631  [__DynamicallyInvokable]
632  public static bool operator ==(Version v1, Version v2)
633  {
634  return v1?.Equals(v2) ?? ((object)v2 == null);
635  }
636 
642  [__DynamicallyInvokable]
643  public static bool operator !=(Version v1, Version v2)
644  {
645  return !(v1 == v2);
646  }
647 
655  [__DynamicallyInvokable]
656  public static bool operator <(Version v1, Version v2)
657  {
658  if ((object)v1 == null)
659  {
660  throw new ArgumentNullException("v1");
661  }
662  return v1.CompareTo(v2) < 0;
663  }
664 
672  [__DynamicallyInvokable]
673  public static bool operator <=(Version v1, Version v2)
674  {
675  if ((object)v1 == null)
676  {
677  throw new ArgumentNullException("v1");
678  }
679  return v1.CompareTo(v2) <= 0;
680  }
681 
687  [__DynamicallyInvokable]
688  public static bool operator >(Version v1, Version v2)
689  {
690  return v2 < v1;
691  }
692 
698  [__DynamicallyInvokable]
699  public static bool operator >=(Version v1, Version v2)
700  {
701  return v2 <= v1;
702  }
703  }
704 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
Version(int major, int minor)
Initializes a new instance of the T:System.Version class using the specified major and minor values.
Definition: Version.cs:235
override int GetHashCode()
Returns a hash code for the current T:System.Version object.
Definition: Version.cs:425
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static bool operator >=(Version v1, Version v2)
Determines whether the first specified T:System.Version object is greater than or equal to the second...
Definition: Version.cs:699
bool Equals(Version obj)
Returns a value indicating whether the current T:System.Version object and a specified T:System....
Definition: Version.cs:409
int CompareTo(object version)
Compares the current T:System.Version object to a specified object and returns an indication of their...
Definition: Version.cs:293
unsafe StringBuilder Insert(int index, string value, int count)
Inserts one or more copies of a specified string into this instance at the specified character positi...
int CompareTo(Version value)
Compares the current T:System.Version object to a specified T:System.Version object and returns an in...
Definition: Version.cs:344
static bool operator >(Version v1, Version v2)
Determines whether the first specified T:System.Version object is greater than the second specified T...
Definition: Version.cs:688
Definition: __Canon.cs:3
override bool Equals(object obj)
Returns a value indicating whether the current T:System.Version object is equal to a specified object...
Definition: Version.cs:390
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static bool operator !=(Version v1, Version v2)
Determines whether two specified T:System.Version objects are not equal.
Definition: Version.cs:643
Defines a generalized type-specific comparison method that a value type or class implements to order ...
Definition: IComparable.cs:8
The exception that is thrown when an arithmetic, casting, or conversion operation in a checked contex...
NumberStyles
Determines the styles permitted in numeric string arguments that are passed to the Parse and TryParse...
Definition: NumberStyles.cs:10
Version(string version)
Initializes a new instance of the T:System.Version class using the specified string.
Definition: Version.cs:259
static bool operator<(Version v1, Version v2)
Determines whether the first specified T:System.Version object is less than the second specified T:Sy...
Definition: Version.cs:656
override string ToString()
Converts the value of the current T:System.Version object to its equivalent T:System....
Definition: Version.cs:437
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
int Major
Gets the value of the major component of the version number for the current T:System....
Definition: Version.cs:103
The exception that is thrown when the format of an argument is invalid, or when a composite format st...
Version(int major, int minor, int build, int revision)
Initializes a new instance of the T:System.Version class with the specified major,...
Definition: Version.cs:179
int Length
Gets or sets the length of the current T:System.Text.StringBuilder object.
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition: ICloneable.cs:7
static bool operator==(Version v1, Version v2)
Determines whether two specified T:System.Version objects are equal.
Definition: Version.cs:632
string ToString(int fieldCount)
Converts the value of the current T:System.Version object to its equivalent T:System....
Definition: Version.cs:457
Represents the version number of an assembly, operating system, or the common language runtime....
Definition: Version.cs:11
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
static Version Parse(string input)
Converts the string representation of a version number to an equivalent T:System.Version object.
Definition: Version.cs:531
The exception that is thrown when one of the arguments provided to a method is not valid.
int Build
Gets the value of the build component of the version number for the current T:System....
Definition: Version.cs:127
short MajorRevision
Gets the high 16 bits of the revision number.
Definition: Version.cs:151
static bool operator<=(Version v1, Version v2)
Determines whether the first specified T:System.Version object is less than or equal to the second T:...
Definition: Version.cs:673
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
int Minor
Gets the value of the minor component of the version number for the current T:System....
Definition: Version.cs:115
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: IEquatable.cs:6
Specifies that the class can be serialized.
object Clone()
Returns a new T:System.Version object whose value is the same as the current T:System....
Definition: Version.cs:277
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
static bool TryParse(string input, out Version result)
Tries to convert the string representation of a version number to an equivalent T:System....
Definition: Version.cs:552
Version(int major, int minor, int build)
Initializes a new instance of the T:System.Version class using the specified major,...
Definition: Version.cs:210
Version()
Initializes a new instance of the T:System.Version class.
Definition: Version.cs:269
int Revision
Gets the value of the revision component of the version number for the current T:System....
Definition: Version.cs:139
short MinorRevision
Gets the low 16 bits of the revision number.
Definition: Version.cs:163