mscorlib(4.0.0.0) API with additions
Guid.cs
1 using Microsoft.Win32;
5 using System.Security;
6 
7 namespace System
8 {
10  [Serializable]
11  [ComVisible(true)]
12  [NonVersionable]
13  [__DynamicallyInvokable]
14  public struct Guid : IFormattable, IComparable, IComparable<Guid>, IEquatable<Guid>
15  {
16  [Flags]
17  private enum GuidStyles
18  {
19  None = 0x0,
20  AllowParenthesis = 0x1,
21  AllowBraces = 0x2,
22  AllowDashes = 0x4,
23  AllowHexPrefix = 0x8,
24  RequireParenthesis = 0x10,
25  RequireBraces = 0x20,
26  RequireDashes = 0x40,
27  RequireHexPrefix = 0x80,
28  HexFormat = 0xA0,
29  NumberFormat = 0x0,
30  DigitFormat = 0x40,
31  BraceFormat = 0x60,
32  ParenthesisFormat = 0x50,
33  Any = 0xF
34  }
35 
36  private enum GuidParseThrowStyle
37  {
38  None,
39  All,
40  AllButOverflow
41  }
42 
43  private enum ParseFailureKind
44  {
45  None,
46  ArgumentNull,
47  Format,
48  FormatWithParameter,
49  NativeException,
50  FormatWithInnerException
51  }
52 
53  private struct GuidResult
54  {
55  internal Guid parsedGuid;
56 
57  internal GuidParseThrowStyle throwStyle;
58 
59  internal ParseFailureKind m_failure;
60 
61  internal string m_failureMessageID;
62 
63  internal object m_failureMessageFormatArgument;
64 
65  internal string m_failureArgumentName;
66 
67  internal Exception m_innerException;
68 
69  internal void Init(GuidParseThrowStyle canThrow)
70  {
71  parsedGuid = Empty;
72  throwStyle = canThrow;
73  }
74 
75  internal void SetFailure(Exception nativeException)
76  {
77  m_failure = ParseFailureKind.NativeException;
78  m_innerException = nativeException;
79  }
80 
81  internal void SetFailure(ParseFailureKind failure, string failureMessageID)
82  {
83  SetFailure(failure, failureMessageID, null, null, null);
84  }
85 
86  internal void SetFailure(ParseFailureKind failure, string failureMessageID, object failureMessageFormatArgument)
87  {
88  SetFailure(failure, failureMessageID, failureMessageFormatArgument, null, null);
89  }
90 
91  internal void SetFailure(ParseFailureKind failure, string failureMessageID, object failureMessageFormatArgument, string failureArgumentName, Exception innerException)
92  {
93  m_failure = failure;
94  m_failureMessageID = failureMessageID;
95  m_failureMessageFormatArgument = failureMessageFormatArgument;
96  m_failureArgumentName = failureArgumentName;
97  m_innerException = innerException;
98  if (throwStyle != 0)
99  {
100  throw GetGuidParseException();
101  }
102  }
103 
104  internal Exception GetGuidParseException()
105  {
106  switch (m_failure)
107  {
108  case ParseFailureKind.ArgumentNull:
109  return new ArgumentNullException(m_failureArgumentName, Environment.GetResourceString(m_failureMessageID));
110  case ParseFailureKind.FormatWithInnerException:
111  return new FormatException(Environment.GetResourceString(m_failureMessageID), m_innerException);
112  case ParseFailureKind.FormatWithParameter:
113  return new FormatException(Environment.GetResourceString(m_failureMessageID, m_failureMessageFormatArgument));
114  case ParseFailureKind.Format:
115  return new FormatException(Environment.GetResourceString(m_failureMessageID));
116  case ParseFailureKind.NativeException:
117  return m_innerException;
118  default:
119  return new FormatException(Environment.GetResourceString("Format_GuidUnrecognized"));
120  }
121  }
122  }
123 
125  [__DynamicallyInvokable]
126  public static readonly Guid Empty;
127 
128  private int _a;
129 
130  private short _b;
131 
132  private short _c;
133 
134  private byte _d;
135 
136  private byte _e;
137 
138  private byte _f;
139 
140  private byte _g;
141 
142  private byte _h;
143 
144  private byte _i;
145 
146  private byte _j;
147 
148  private byte _k;
149 
156  [__DynamicallyInvokable]
157  public Guid(byte[] b)
158  {
159  if (b == null)
160  {
161  throw new ArgumentNullException("b");
162  }
163  if (b.Length != 16)
164  {
165  throw new ArgumentException(Environment.GetResourceString("Arg_GuidArrayCtor", "16"));
166  }
167  _a = ((b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]);
168  _b = (short)((b[5] << 8) | b[4]);
169  _c = (short)((b[7] << 8) | b[6]);
170  _d = b[8];
171  _e = b[9];
172  _f = b[10];
173  _g = b[11];
174  _h = b[12];
175  _i = b[13];
176  _j = b[14];
177  _k = b[15];
178  }
179 
192  [CLSCompliant(false)]
193  [__DynamicallyInvokable]
194  public Guid(uint a, ushort b, ushort c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
195  {
196  _a = (int)a;
197  _b = (short)b;
198  _c = (short)c;
199  _d = d;
200  _e = e;
201  _f = f;
202  _g = g;
203  _h = h;
204  _i = i;
205  _j = j;
206  _k = k;
207  }
208 
218  [__DynamicallyInvokable]
219  public Guid(int a, short b, short c, byte[] d)
220  {
221  if (d == null)
222  {
223  throw new ArgumentNullException("d");
224  }
225  if (d.Length != 8)
226  {
227  throw new ArgumentException(Environment.GetResourceString("Arg_GuidArrayCtor", "8"));
228  }
229  _a = a;
230  _b = b;
231  _c = c;
232  _d = d[0];
233  _e = d[1];
234  _f = d[2];
235  _g = d[3];
236  _h = d[4];
237  _i = d[5];
238  _j = d[6];
239  _k = d[7];
240  }
241 
254  [__DynamicallyInvokable]
255  public Guid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
256  {
257  _a = a;
258  _b = b;
259  _c = c;
260  _d = d;
261  _e = e;
262  _f = f;
263  _g = g;
264  _h = h;
265  _i = i;
266  _j = j;
267  _k = k;
268  }
269 
276  [__DynamicallyInvokable]
277  public Guid(string g)
278  {
279  if (g == null)
280  {
281  throw new ArgumentNullException("g");
282  }
283  this = Empty;
284  GuidResult result = default(GuidResult);
285  result.Init(GuidParseThrowStyle.All);
286  if (TryParseGuid(g, GuidStyles.Any, ref result))
287  {
288  this = result.parsedGuid;
289  return;
290  }
291  throw result.GetGuidParseException();
292  }
293 
301  [__DynamicallyInvokable]
302  public static Guid Parse(string input)
303  {
304  if (input == null)
305  {
306  throw new ArgumentNullException("input");
307  }
308  GuidResult result = default(GuidResult);
309  result.Init(GuidParseThrowStyle.AllButOverflow);
310  if (TryParseGuid(input, GuidStyles.Any, ref result))
311  {
312  return result.parsedGuid;
313  }
314  throw result.GetGuidParseException();
315  }
316 
322  [__DynamicallyInvokable]
323  public static bool TryParse(string input, out Guid result)
324  {
325  GuidResult result2 = default(GuidResult);
326  result2.Init(GuidParseThrowStyle.None);
327  if (TryParseGuid(input, GuidStyles.Any, ref result2))
328  {
329  result = result2.parsedGuid;
330  return true;
331  }
332  result = Empty;
333  return false;
334  }
335 
344  [__DynamicallyInvokable]
345  public static Guid ParseExact(string input, string format)
346  {
347  if (input == null)
348  {
349  throw new ArgumentNullException("input");
350  }
351  if (format == null)
352  {
353  throw new ArgumentNullException("format");
354  }
355  if (format.Length != 1)
356  {
357  throw new FormatException(Environment.GetResourceString("Format_InvalidGuidFormatSpecification"));
358  }
359  GuidStyles flags;
360  switch (format[0])
361  {
362  case 'D':
363  case 'd':
364  flags = GuidStyles.RequireDashes;
365  break;
366  case 'N':
367  case 'n':
368  flags = GuidStyles.None;
369  break;
370  case 'B':
371  case 'b':
372  flags = GuidStyles.BraceFormat;
373  break;
374  case 'P':
375  case 'p':
376  flags = GuidStyles.ParenthesisFormat;
377  break;
378  case 'X':
379  case 'x':
380  flags = GuidStyles.HexFormat;
381  break;
382  default:
383  throw new FormatException(Environment.GetResourceString("Format_InvalidGuidFormatSpecification"));
384  }
385  GuidResult result = default(GuidResult);
386  result.Init(GuidParseThrowStyle.AllButOverflow);
387  if (TryParseGuid(input, flags, ref result))
388  {
389  return result.parsedGuid;
390  }
391  throw result.GetGuidParseException();
392  }
393 
400  [__DynamicallyInvokable]
401  public static bool TryParseExact(string input, string format, out Guid result)
402  {
403  if (format == null || format.Length != 1)
404  {
405  result = Empty;
406  return false;
407  }
408  GuidStyles flags;
409  switch (format[0])
410  {
411  case 'D':
412  case 'd':
413  flags = GuidStyles.RequireDashes;
414  break;
415  case 'N':
416  case 'n':
417  flags = GuidStyles.None;
418  break;
419  case 'B':
420  case 'b':
421  flags = GuidStyles.BraceFormat;
422  break;
423  case 'P':
424  case 'p':
425  flags = GuidStyles.ParenthesisFormat;
426  break;
427  case 'X':
428  case 'x':
429  flags = GuidStyles.HexFormat;
430  break;
431  default:
432  result = Empty;
433  return false;
434  }
435  GuidResult result2 = default(GuidResult);
436  result2.Init(GuidParseThrowStyle.None);
437  if (TryParseGuid(input, flags, ref result2))
438  {
439  result = result2.parsedGuid;
440  return true;
441  }
442  result = Empty;
443  return false;
444  }
445 
446  private static bool TryParseGuid(string g, GuidStyles flags, ref GuidResult result)
447  {
448  if (g == null)
449  {
450  result.SetFailure(ParseFailureKind.Format, "Format_GuidUnrecognized");
451  return false;
452  }
453  string text = g.Trim();
454  if (text.Length == 0)
455  {
456  result.SetFailure(ParseFailureKind.Format, "Format_GuidUnrecognized");
457  return false;
458  }
459  bool flag = text.IndexOf('-', 0) >= 0;
460  if (flag)
461  {
462  if ((flags & (GuidStyles.AllowDashes | GuidStyles.RequireDashes)) == GuidStyles.None)
463  {
464  result.SetFailure(ParseFailureKind.Format, "Format_GuidUnrecognized");
465  return false;
466  }
467  }
468  else if ((flags & GuidStyles.RequireDashes) != 0)
469  {
470  result.SetFailure(ParseFailureKind.Format, "Format_GuidUnrecognized");
471  return false;
472  }
473  bool flag2 = text.IndexOf('{', 0) >= 0;
474  if (flag2)
475  {
476  if ((flags & (GuidStyles.AllowBraces | GuidStyles.RequireBraces)) == GuidStyles.None)
477  {
478  result.SetFailure(ParseFailureKind.Format, "Format_GuidUnrecognized");
479  return false;
480  }
481  }
482  else if ((flags & GuidStyles.RequireBraces) != 0)
483  {
484  result.SetFailure(ParseFailureKind.Format, "Format_GuidUnrecognized");
485  return false;
486  }
487  if (text.IndexOf('(', 0) >= 0)
488  {
489  if ((flags & (GuidStyles.AllowParenthesis | GuidStyles.RequireParenthesis)) == GuidStyles.None)
490  {
491  result.SetFailure(ParseFailureKind.Format, "Format_GuidUnrecognized");
492  return false;
493  }
494  }
495  else if ((flags & GuidStyles.RequireParenthesis) != 0)
496  {
497  result.SetFailure(ParseFailureKind.Format, "Format_GuidUnrecognized");
498  return false;
499  }
500  try
501  {
502  if (flag)
503  {
504  return TryParseGuidWithDashes(text, ref result);
505  }
506  if (flag2)
507  {
508  return TryParseGuidWithHexPrefix(text, ref result);
509  }
510  return TryParseGuidWithNoStyle(text, ref result);
511  }
512  catch (IndexOutOfRangeException innerException)
513  {
514  result.SetFailure(ParseFailureKind.FormatWithInnerException, "Format_GuidUnrecognized", null, null, innerException);
515  return false;
516  }
517  catch (ArgumentException innerException2)
518  {
519  result.SetFailure(ParseFailureKind.FormatWithInnerException, "Format_GuidUnrecognized", null, null, innerException2);
520  return false;
521  }
522  }
523 
524  private static bool TryParseGuidWithHexPrefix(string guidString, ref GuidResult result)
525  {
526  int num = 0;
527  int num2 = 0;
528  guidString = EatAllWhitespace(guidString);
529  if (string.IsNullOrEmpty(guidString) || guidString[0] != '{')
530  {
531  result.SetFailure(ParseFailureKind.Format, "Format_GuidBrace");
532  return false;
533  }
534  if (!IsHexPrefix(guidString, 1))
535  {
536  result.SetFailure(ParseFailureKind.Format, "Format_GuidHexPrefix", "{0xdddddddd, etc}");
537  return false;
538  }
539  num = 3;
540  num2 = guidString.IndexOf(',', num) - num;
541  if (num2 <= 0)
542  {
543  result.SetFailure(ParseFailureKind.Format, "Format_GuidComma");
544  return false;
545  }
546  if (!StringToInt(guidString.Substring(num, num2), -1, 4096, out result.parsedGuid._a, ref result))
547  {
548  return false;
549  }
550  if (!IsHexPrefix(guidString, num + num2 + 1))
551  {
552  result.SetFailure(ParseFailureKind.Format, "Format_GuidHexPrefix", "{0xdddddddd, 0xdddd, etc}");
553  return false;
554  }
555  num = num + num2 + 3;
556  num2 = guidString.IndexOf(',', num) - num;
557  if (num2 <= 0)
558  {
559  result.SetFailure(ParseFailureKind.Format, "Format_GuidComma");
560  return false;
561  }
562  if (!StringToShort(guidString.Substring(num, num2), -1, 4096, out result.parsedGuid._b, ref result))
563  {
564  return false;
565  }
566  if (!IsHexPrefix(guidString, num + num2 + 1))
567  {
568  result.SetFailure(ParseFailureKind.Format, "Format_GuidHexPrefix", "{0xdddddddd, 0xdddd, 0xdddd, etc}");
569  return false;
570  }
571  num = num + num2 + 3;
572  num2 = guidString.IndexOf(',', num) - num;
573  if (num2 <= 0)
574  {
575  result.SetFailure(ParseFailureKind.Format, "Format_GuidComma");
576  return false;
577  }
578  if (!StringToShort(guidString.Substring(num, num2), -1, 4096, out result.parsedGuid._c, ref result))
579  {
580  return false;
581  }
582  if (guidString.Length <= num + num2 + 1 || guidString[num + num2 + 1] != '{')
583  {
584  result.SetFailure(ParseFailureKind.Format, "Format_GuidBrace");
585  return false;
586  }
587  num2++;
588  byte[] array = new byte[8];
589  for (int i = 0; i < 8; i++)
590  {
591  if (!IsHexPrefix(guidString, num + num2 + 1))
592  {
593  result.SetFailure(ParseFailureKind.Format, "Format_GuidHexPrefix", "{... { ... 0xdd, ...}}");
594  return false;
595  }
596  num = num + num2 + 3;
597  if (i < 7)
598  {
599  num2 = guidString.IndexOf(',', num) - num;
600  if (num2 <= 0)
601  {
602  result.SetFailure(ParseFailureKind.Format, "Format_GuidComma");
603  return false;
604  }
605  }
606  else
607  {
608  num2 = guidString.IndexOf('}', num) - num;
609  if (num2 <= 0)
610  {
611  result.SetFailure(ParseFailureKind.Format, "Format_GuidBraceAfterLastNumber");
612  return false;
613  }
614  }
615  uint num3 = (uint)Convert.ToInt32(guidString.Substring(num, num2), 16);
616  if (num3 > 255)
617  {
618  result.SetFailure(ParseFailureKind.Format, "Overflow_Byte");
619  return false;
620  }
621  array[i] = (byte)num3;
622  }
623  result.parsedGuid._d = array[0];
624  result.parsedGuid._e = array[1];
625  result.parsedGuid._f = array[2];
626  result.parsedGuid._g = array[3];
627  result.parsedGuid._h = array[4];
628  result.parsedGuid._i = array[5];
629  result.parsedGuid._j = array[6];
630  result.parsedGuid._k = array[7];
631  if (num + num2 + 1 >= guidString.Length || guidString[num + num2 + 1] != '}')
632  {
633  result.SetFailure(ParseFailureKind.Format, "Format_GuidEndBrace");
634  return false;
635  }
636  if (num + num2 + 1 != guidString.Length - 1)
637  {
638  result.SetFailure(ParseFailureKind.Format, "Format_ExtraJunkAtEnd");
639  return false;
640  }
641  return true;
642  }
643 
644  private static bool TryParseGuidWithNoStyle(string guidString, ref GuidResult result)
645  {
646  int num = 0;
647  int num2 = 0;
648  if (guidString.Length != 32)
649  {
650  result.SetFailure(ParseFailureKind.Format, "Format_GuidInvLen");
651  return false;
652  }
653  foreach (char c in guidString)
654  {
655  if (c < '0' || c > '9')
656  {
657  char c2 = char.ToUpper(c, CultureInfo.InvariantCulture);
658  if (c2 < 'A' || c2 > 'F')
659  {
660  result.SetFailure(ParseFailureKind.Format, "Format_GuidInvalidChar");
661  return false;
662  }
663  }
664  }
665  if (!StringToInt(guidString.Substring(num, 8), -1, 4096, out result.parsedGuid._a, ref result))
666  {
667  return false;
668  }
669  num += 8;
670  if (!StringToShort(guidString.Substring(num, 4), -1, 4096, out result.parsedGuid._b, ref result))
671  {
672  return false;
673  }
674  num += 4;
675  if (!StringToShort(guidString.Substring(num, 4), -1, 4096, out result.parsedGuid._c, ref result))
676  {
677  return false;
678  }
679  num += 4;
680  if (!StringToInt(guidString.Substring(num, 4), -1, 4096, out int result2, ref result))
681  {
682  return false;
683  }
684  num += 4;
685  num2 = num;
686  if (!StringToLong(guidString, ref num2, num, out long result3, ref result))
687  {
688  return false;
689  }
690  if (num2 - num != 12)
691  {
692  result.SetFailure(ParseFailureKind.Format, "Format_GuidInvLen");
693  return false;
694  }
695  result.parsedGuid._d = (byte)(result2 >> 8);
696  result.parsedGuid._e = (byte)result2;
697  result2 = (int)(result3 >> 32);
698  result.parsedGuid._f = (byte)(result2 >> 8);
699  result.parsedGuid._g = (byte)result2;
700  result2 = (int)result3;
701  result.parsedGuid._h = (byte)(result2 >> 24);
702  result.parsedGuid._i = (byte)(result2 >> 16);
703  result.parsedGuid._j = (byte)(result2 >> 8);
704  result.parsedGuid._k = (byte)result2;
705  return true;
706  }
707 
708  private static bool TryParseGuidWithDashes(string guidString, ref GuidResult result)
709  {
710  int num = 0;
711  int num2 = 0;
712  if (guidString[0] == '{')
713  {
714  if (guidString.Length != 38 || guidString[37] != '}')
715  {
716  result.SetFailure(ParseFailureKind.Format, "Format_GuidInvLen");
717  return false;
718  }
719  num = 1;
720  }
721  else if (guidString[0] == '(')
722  {
723  if (guidString.Length != 38 || guidString[37] != ')')
724  {
725  result.SetFailure(ParseFailureKind.Format, "Format_GuidInvLen");
726  return false;
727  }
728  num = 1;
729  }
730  else if (guidString.Length != 36)
731  {
732  result.SetFailure(ParseFailureKind.Format, "Format_GuidInvLen");
733  return false;
734  }
735  if (guidString[8 + num] != '-' || guidString[13 + num] != '-' || guidString[18 + num] != '-' || guidString[23 + num] != '-')
736  {
737  result.SetFailure(ParseFailureKind.Format, "Format_GuidDashes");
738  return false;
739  }
740  num2 = num;
741  if (!StringToInt(guidString, ref num2, 8, 8192, out int result2, ref result))
742  {
743  return false;
744  }
745  result.parsedGuid._a = result2;
746  num2++;
747  if (!StringToInt(guidString, ref num2, 4, 8192, out result2, ref result))
748  {
749  return false;
750  }
751  result.parsedGuid._b = (short)result2;
752  num2++;
753  if (!StringToInt(guidString, ref num2, 4, 8192, out result2, ref result))
754  {
755  return false;
756  }
757  result.parsedGuid._c = (short)result2;
758  num2++;
759  if (!StringToInt(guidString, ref num2, 4, 8192, out result2, ref result))
760  {
761  return false;
762  }
763  num2++;
764  num = num2;
765  if (!StringToLong(guidString, ref num2, 8192, out long result3, ref result))
766  {
767  return false;
768  }
769  if (num2 - num != 12)
770  {
771  result.SetFailure(ParseFailureKind.Format, "Format_GuidInvLen");
772  return false;
773  }
774  result.parsedGuid._d = (byte)(result2 >> 8);
775  result.parsedGuid._e = (byte)result2;
776  result2 = (int)(result3 >> 32);
777  result.parsedGuid._f = (byte)(result2 >> 8);
778  result.parsedGuid._g = (byte)result2;
779  result2 = (int)result3;
780  result.parsedGuid._h = (byte)(result2 >> 24);
781  result.parsedGuid._i = (byte)(result2 >> 16);
782  result.parsedGuid._j = (byte)(result2 >> 8);
783  result.parsedGuid._k = (byte)result2;
784  return true;
785  }
786 
787  [SecuritySafeCritical]
788  private static bool StringToShort(string str, int requiredLength, int flags, out short result, ref GuidResult parseResult)
789  {
790  return StringToShort(str, null, requiredLength, flags, out result, ref parseResult);
791  }
792 
793  [SecuritySafeCritical]
794  private unsafe static bool StringToShort(string str, ref int parsePos, int requiredLength, int flags, out short result, ref GuidResult parseResult)
795  {
796  fixed (int* parsePos2 = &parsePos)
797  {
798  return StringToShort(str, parsePos2, requiredLength, flags, out result, ref parseResult);
799  }
800  }
801 
802  [SecurityCritical]
803  private unsafe static bool StringToShort(string str, int* parsePos, int requiredLength, int flags, out short result, ref GuidResult parseResult)
804  {
805  result = 0;
806  int result2;
807  bool result3 = StringToInt(str, parsePos, requiredLength, flags, out result2, ref parseResult);
808  result = (short)result2;
809  return result3;
810  }
811 
812  [SecuritySafeCritical]
813  private static bool StringToInt(string str, int requiredLength, int flags, out int result, ref GuidResult parseResult)
814  {
815  return StringToInt(str, null, requiredLength, flags, out result, ref parseResult);
816  }
817 
818  [SecuritySafeCritical]
819  private unsafe static bool StringToInt(string str, ref int parsePos, int requiredLength, int flags, out int result, ref GuidResult parseResult)
820  {
821  fixed (int* parsePos2 = &parsePos)
822  {
823  return StringToInt(str, parsePos2, requiredLength, flags, out result, ref parseResult);
824  }
825  }
826 
827  [SecurityCritical]
828  private unsafe static bool StringToInt(string str, int* parsePos, int requiredLength, int flags, out int result, ref GuidResult parseResult)
829  {
830  result = 0;
831  int num = (parsePos != null) ? (*parsePos) : 0;
832  try
833  {
834  result = ParseNumbers.StringToInt(str, 16, flags, parsePos);
835  }
836  catch (OverflowException ex)
837  {
838  if (parseResult.throwStyle == GuidParseThrowStyle.All)
839  {
840  throw;
841  }
842  if (parseResult.throwStyle == GuidParseThrowStyle.AllButOverflow)
843  {
844  throw new FormatException(Environment.GetResourceString("Format_GuidUnrecognized"), ex);
845  }
846  parseResult.SetFailure(ex);
847  return false;
848  }
849  catch (Exception failure)
850  {
851  if (parseResult.throwStyle != 0)
852  {
853  throw;
854  }
855  parseResult.SetFailure(failure);
856  return false;
857  }
858  if (requiredLength != -1 && parsePos != null && *parsePos - num != requiredLength)
859  {
860  parseResult.SetFailure(ParseFailureKind.Format, "Format_GuidInvalidChar");
861  return false;
862  }
863  return true;
864  }
865 
866  [SecuritySafeCritical]
867  private static bool StringToLong(string str, int flags, out long result, ref GuidResult parseResult)
868  {
869  return StringToLong(str, null, flags, out result, ref parseResult);
870  }
871 
872  [SecuritySafeCritical]
873  private unsafe static bool StringToLong(string str, ref int parsePos, int flags, out long result, ref GuidResult parseResult)
874  {
875  fixed (int* parsePos2 = &parsePos)
876  {
877  return StringToLong(str, parsePos2, flags, out result, ref parseResult);
878  }
879  }
880 
881  [SecuritySafeCritical]
882  private unsafe static bool StringToLong(string str, int* parsePos, int flags, out long result, ref GuidResult parseResult)
883  {
884  result = 0L;
885  try
886  {
887  result = ParseNumbers.StringToLong(str, 16, flags, parsePos);
888  }
889  catch (OverflowException ex)
890  {
891  if (parseResult.throwStyle == GuidParseThrowStyle.All)
892  {
893  throw;
894  }
895  if (parseResult.throwStyle == GuidParseThrowStyle.AllButOverflow)
896  {
897  throw new FormatException(Environment.GetResourceString("Format_GuidUnrecognized"), ex);
898  }
899  parseResult.SetFailure(ex);
900  return false;
901  }
902  catch (Exception failure)
903  {
904  if (parseResult.throwStyle != 0)
905  {
906  throw;
907  }
908  parseResult.SetFailure(failure);
909  return false;
910  }
911  return true;
912  }
913 
914  private static string EatAllWhitespace(string str)
915  {
916  int num = 0;
917  char[] array = new char[str.Length];
918  foreach (char c in str)
919  {
920  if (!char.IsWhiteSpace(c))
921  {
922  array[num++] = c;
923  }
924  }
925  return new string(array, 0, num);
926  }
927 
928  private static bool IsHexPrefix(string str, int i)
929  {
930  if (str.Length > i + 1 && str[i] == '0' && char.ToLower(str[i + 1], CultureInfo.InvariantCulture) == 'x')
931  {
932  return true;
933  }
934  return false;
935  }
936 
939  [__DynamicallyInvokable]
940  public byte[] ToByteArray()
941  {
942  return new byte[16]
943  {
944  (byte)_a,
945  (byte)(_a >> 8),
946  (byte)(_a >> 16),
947  (byte)(_a >> 24),
948  (byte)_b,
949  (byte)(_b >> 8),
950  (byte)_c,
951  (byte)(_c >> 8),
952  _d,
953  _e,
954  _f,
955  _g,
956  _h,
957  _i,
958  _j,
959  _k
960  };
961  }
962 
967  [__DynamicallyInvokable]
968  public override string ToString()
969  {
970  return ToString("D", null);
971  }
972 
975  [__DynamicallyInvokable]
976  public override int GetHashCode()
977  {
978  return _a ^ ((_b << 16) | (ushort)_c) ^ ((_f << 24) | _k);
979  }
980 
985  [__DynamicallyInvokable]
986  public override bool Equals(object o)
987  {
988  if (o == null || !(o is Guid))
989  {
990  return false;
991  }
992  Guid guid = (Guid)o;
993  if (guid._a != _a)
994  {
995  return false;
996  }
997  if (guid._b != _b)
998  {
999  return false;
1000  }
1001  if (guid._c != _c)
1002  {
1003  return false;
1004  }
1005  if (guid._d != _d)
1006  {
1007  return false;
1008  }
1009  if (guid._e != _e)
1010  {
1011  return false;
1012  }
1013  if (guid._f != _f)
1014  {
1015  return false;
1016  }
1017  if (guid._g != _g)
1018  {
1019  return false;
1020  }
1021  if (guid._h != _h)
1022  {
1023  return false;
1024  }
1025  if (guid._i != _i)
1026  {
1027  return false;
1028  }
1029  if (guid._j != _j)
1030  {
1031  return false;
1032  }
1033  if (guid._k != _k)
1034  {
1035  return false;
1036  }
1037  return true;
1038  }
1039 
1044  [__DynamicallyInvokable]
1045  public bool Equals(Guid g)
1046  {
1047  if (g._a != _a)
1048  {
1049  return false;
1050  }
1051  if (g._b != _b)
1052  {
1053  return false;
1054  }
1055  if (g._c != _c)
1056  {
1057  return false;
1058  }
1059  if (g._d != _d)
1060  {
1061  return false;
1062  }
1063  if (g._e != _e)
1064  {
1065  return false;
1066  }
1067  if (g._f != _f)
1068  {
1069  return false;
1070  }
1071  if (g._g != _g)
1072  {
1073  return false;
1074  }
1075  if (g._h != _h)
1076  {
1077  return false;
1078  }
1079  if (g._i != _i)
1080  {
1081  return false;
1082  }
1083  if (g._j != _j)
1084  {
1085  return false;
1086  }
1087  if (g._k != _k)
1088  {
1089  return false;
1090  }
1091  return true;
1092  }
1093 
1094  private int GetResult(uint me, uint them)
1095  {
1096  if (me < them)
1097  {
1098  return -1;
1099  }
1100  return 1;
1101  }
1102 
1108  public int CompareTo(object value)
1109  {
1110  if (value == null)
1111  {
1112  return 1;
1113  }
1114  if (!(value is Guid))
1115  {
1116  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeGuid"));
1117  }
1118  Guid guid = (Guid)value;
1119  if (guid._a != _a)
1120  {
1121  return GetResult((uint)_a, (uint)guid._a);
1122  }
1123  if (guid._b != _b)
1124  {
1125  return GetResult((uint)_b, (uint)guid._b);
1126  }
1127  if (guid._c != _c)
1128  {
1129  return GetResult((uint)_c, (uint)guid._c);
1130  }
1131  if (guid._d != _d)
1132  {
1133  return GetResult(_d, guid._d);
1134  }
1135  if (guid._e != _e)
1136  {
1137  return GetResult(_e, guid._e);
1138  }
1139  if (guid._f != _f)
1140  {
1141  return GetResult(_f, guid._f);
1142  }
1143  if (guid._g != _g)
1144  {
1145  return GetResult(_g, guid._g);
1146  }
1147  if (guid._h != _h)
1148  {
1149  return GetResult(_h, guid._h);
1150  }
1151  if (guid._i != _i)
1152  {
1153  return GetResult(_i, guid._i);
1154  }
1155  if (guid._j != _j)
1156  {
1157  return GetResult(_j, guid._j);
1158  }
1159  if (guid._k != _k)
1160  {
1161  return GetResult(_k, guid._k);
1162  }
1163  return 0;
1164  }
1165 
1169  [__DynamicallyInvokable]
1170  public int CompareTo(Guid value)
1171  {
1172  if (value._a != _a)
1173  {
1174  return GetResult((uint)_a, (uint)value._a);
1175  }
1176  if (value._b != _b)
1177  {
1178  return GetResult((uint)_b, (uint)value._b);
1179  }
1180  if (value._c != _c)
1181  {
1182  return GetResult((uint)_c, (uint)value._c);
1183  }
1184  if (value._d != _d)
1185  {
1186  return GetResult(_d, value._d);
1187  }
1188  if (value._e != _e)
1189  {
1190  return GetResult(_e, value._e);
1191  }
1192  if (value._f != _f)
1193  {
1194  return GetResult(_f, value._f);
1195  }
1196  if (value._g != _g)
1197  {
1198  return GetResult(_g, value._g);
1199  }
1200  if (value._h != _h)
1201  {
1202  return GetResult(_h, value._h);
1203  }
1204  if (value._i != _i)
1205  {
1206  return GetResult(_i, value._i);
1207  }
1208  if (value._j != _j)
1209  {
1210  return GetResult(_j, value._j);
1211  }
1212  if (value._k != _k)
1213  {
1214  return GetResult(_k, value._k);
1215  }
1216  return 0;
1217  }
1218 
1224  [__DynamicallyInvokable]
1225  public static bool operator ==(Guid a, Guid b)
1226  {
1227  if (a._a != b._a)
1228  {
1229  return false;
1230  }
1231  if (a._b != b._b)
1232  {
1233  return false;
1234  }
1235  if (a._c != b._c)
1236  {
1237  return false;
1238  }
1239  if (a._d != b._d)
1240  {
1241  return false;
1242  }
1243  if (a._e != b._e)
1244  {
1245  return false;
1246  }
1247  if (a._f != b._f)
1248  {
1249  return false;
1250  }
1251  if (a._g != b._g)
1252  {
1253  return false;
1254  }
1255  if (a._h != b._h)
1256  {
1257  return false;
1258  }
1259  if (a._i != b._i)
1260  {
1261  return false;
1262  }
1263  if (a._j != b._j)
1264  {
1265  return false;
1266  }
1267  if (a._k != b._k)
1268  {
1269  return false;
1270  }
1271  return true;
1272  }
1273 
1279  [__DynamicallyInvokable]
1280  public static bool operator !=(Guid a, Guid b)
1281  {
1282  return !(a == b);
1283  }
1284 
1287  [SecuritySafeCritical]
1288  [__DynamicallyInvokable]
1289  public static Guid NewGuid()
1290  {
1291  Marshal.ThrowExceptionForHR(Win32Native.CoCreateGuid(out Guid guid), new IntPtr(-1));
1292  return guid;
1293  }
1294 
1299  [__DynamicallyInvokable]
1300  public string ToString(string format)
1301  {
1302  return ToString(format, null);
1303  }
1304 
1305  private static char HexToChar(int a)
1306  {
1307  a &= 0xF;
1308  return (char)((a > 9) ? (a - 10 + 97) : (a + 48));
1309  }
1310 
1311  [SecurityCritical]
1312  private unsafe static int HexsToChars(char* guidChars, int offset, int a, int b)
1313  {
1314  return HexsToChars(guidChars, offset, a, b, hex: false);
1315  }
1316 
1317  [SecurityCritical]
1318  private unsafe static int HexsToChars(char* guidChars, int offset, int a, int b, bool hex)
1319  {
1320  if (hex)
1321  {
1322  guidChars[offset++] = '0';
1323  guidChars[offset++] = 'x';
1324  }
1325  guidChars[offset++] = HexToChar(a >> 4);
1326  guidChars[offset++] = HexToChar(a);
1327  if (hex)
1328  {
1329  guidChars[offset++] = ',';
1330  guidChars[offset++] = '0';
1331  guidChars[offset++] = 'x';
1332  }
1333  guidChars[offset++] = HexToChar(b >> 4);
1334  guidChars[offset++] = HexToChar(b);
1335  return offset;
1336  }
1337 
1343  [SecuritySafeCritical]
1344  public unsafe string ToString(string format, IFormatProvider provider)
1345  {
1346  if (format == null || format.Length == 0)
1347  {
1348  format = "D";
1349  }
1350  int num = 0;
1351  bool flag = true;
1352  bool flag2 = false;
1353  if (format.Length != 1)
1354  {
1355  throw new FormatException(Environment.GetResourceString("Format_InvalidGuidFormatSpecification"));
1356  }
1357  string text;
1358  switch (format[0])
1359  {
1360  case 'D':
1361  case 'd':
1362  text = string.FastAllocateString(36);
1363  break;
1364  case 'N':
1365  case 'n':
1366  text = string.FastAllocateString(32);
1367  flag = false;
1368  break;
1369  case 'B':
1370  case 'b':
1371  text = string.FastAllocateString(38);
1372  fixed (char* ptr3 = text)
1373  {
1374  ptr3[num++] = '{';
1375  ptr3[37] = '}';
1376  }
1377  break;
1378  case 'P':
1379  case 'p':
1380  text = string.FastAllocateString(38);
1381  fixed (char* ptr2 = text)
1382  {
1383  ptr2[num++] = '(';
1384  ptr2[37] = ')';
1385  }
1386  break;
1387  case 'X':
1388  case 'x':
1389  text = string.FastAllocateString(68);
1390  fixed (char* ptr = text)
1391  {
1392  ptr[num++] = '{';
1393  ptr[67] = '}';
1394  }
1395  flag = false;
1396  flag2 = true;
1397  break;
1398  default:
1399  throw new FormatException(Environment.GetResourceString("Format_InvalidGuidFormatSpecification"));
1400  }
1401  fixed (char* ptr4 = text)
1402  {
1403  if (flag2)
1404  {
1405  ptr4[num++] = '0';
1406  ptr4[num++] = 'x';
1407  num = HexsToChars(ptr4, num, _a >> 24, _a >> 16);
1408  num = HexsToChars(ptr4, num, _a >> 8, _a);
1409  ptr4[num++] = ',';
1410  ptr4[num++] = '0';
1411  ptr4[num++] = 'x';
1412  num = HexsToChars(ptr4, num, _b >> 8, _b);
1413  ptr4[num++] = ',';
1414  ptr4[num++] = '0';
1415  ptr4[num++] = 'x';
1416  num = HexsToChars(ptr4, num, _c >> 8, _c);
1417  ptr4[num++] = ',';
1418  ptr4[num++] = '{';
1419  num = HexsToChars(ptr4, num, _d, _e, hex: true);
1420  ptr4[num++] = ',';
1421  num = HexsToChars(ptr4, num, _f, _g, hex: true);
1422  ptr4[num++] = ',';
1423  num = HexsToChars(ptr4, num, _h, _i, hex: true);
1424  ptr4[num++] = ',';
1425  num = HexsToChars(ptr4, num, _j, _k, hex: true);
1426  ptr4[num++] = '}';
1427  }
1428  else
1429  {
1430  num = HexsToChars(ptr4, num, _a >> 24, _a >> 16);
1431  num = HexsToChars(ptr4, num, _a >> 8, _a);
1432  if (flag)
1433  {
1434  ptr4[num++] = '-';
1435  }
1436  num = HexsToChars(ptr4, num, _b >> 8, _b);
1437  if (flag)
1438  {
1439  ptr4[num++] = '-';
1440  }
1441  num = HexsToChars(ptr4, num, _c >> 8, _c);
1442  if (flag)
1443  {
1444  ptr4[num++] = '-';
1445  }
1446  num = HexsToChars(ptr4, num, _d, _e);
1447  if (flag)
1448  {
1449  ptr4[num++] = '-';
1450  }
1451  num = HexsToChars(ptr4, num, _f, _g);
1452  num = HexsToChars(ptr4, num, _h, _i);
1453  num = HexsToChars(ptr4, num, _j, _k);
1454  }
1455  }
1456  return text;
1457  }
1458  }
1459 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
static bool operator !=(Guid a, Guid b)
Indicates whether the values of two specified T:System.Guid objects are not equal.
Definition: Guid.cs:1280
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Guid(uint a, ushort b, ushort c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
Initializes a new instance of the T:System.Guid structure by using the specified unsigned integers an...
Definition: Guid.cs:194
Definition: __Canon.cs:3
Provides a mechanism for retrieving an object to control formatting.
static Guid NewGuid()
Initializes a new instance of the T:System.Guid structure.
Definition: Guid.cs:1289
Defines a generalized type-specific comparison method that a value type or class implements to order ...
Definition: IComparable.cs:8
byte [] ToByteArray()
Returns a 16-element byte array that contains the value of this instance.
Definition: Guid.cs:940
Guid(int a, short b, short c, byte[] d)
Initializes a new instance of the T:System.Guid structure by using the specified integers and byte ar...
Definition: Guid.cs:219
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents a globally unique identifier (GUID).To browse the .NET Framework source code for this type...
Definition: Guid.cs:14
int CompareTo(Guid value)
Compares this instance to a specified T:System.Guid object and returns an indication of their relativ...
Definition: Guid.cs:1170
unsafe string ToString(string format, IFormatProvider provider)
Returns a string representation of the value of this instance of the T:System.Guid class,...
Definition: Guid.cs:1344
Attribute can be applied to any application element.
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
The exception that is thrown when the format of an argument is invalid, or when a composite format st...
Guid(byte[] b)
Initializes a new instance of the T:System.Guid structure by using the specified array of bytes.
Definition: Guid.cs:157
static bool TryParseExact(string input, string format, out Guid result)
Converts the string representation of a GUID to the equivalent T:System.Guid structure,...
Definition: Guid.cs:401
override string ToString()
Returns a string representation of the value of this instance in registry format.
Definition: Guid.cs:968
static void ThrowExceptionForHR(int errorCode)
Throws an exception with a specific failure HRESULT value.
Definition: Marshal.cs:1323
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
static Guid Parse(string input)
Converts the string representation of a GUID to the equivalent T:System.Guid structure.
Definition: Guid.cs:302
Guid(string g)
Initializes a new instance of the T:System.Guid structure by using the value represented by the speci...
Definition: Guid.cs:277
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
override bool Equals(object o)
Returns a value that indicates whether this instance is equal to a specified object.
Definition: Guid.cs:986
static bool TryParse(string input, out Guid result)
Converts the string representation of a GUID to the equivalent T:System.Guid structure.
Definition: Guid.cs:323
string ToString(string format)
Returns a string representation of the value of this T:System.Guid instance, according to the provide...
Definition: Guid.cs:1300
int CompareTo(object value)
Compares this instance to a specified object and returns an indication of their relative values.
Definition: Guid.cs:1108
The exception that is thrown when one of the arguments provided to a method is not valid.
override int GetHashCode()
Returns the hash code for this instance.
Definition: Guid.cs:976
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
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.
Guid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
Initializes a new instance of the T:System.Guid structure by using the specified integers and bytes.
Definition: Guid.cs:255
static readonly Guid Empty
A read-only instance of the T:System.Guid structure whose value is all zeros.
Definition: Guid.cs:126
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
static Guid ParseExact(string input, string format)
Converts the string representation of a GUID to the equivalent T:System.Guid structure,...
Definition: Guid.cs:345
static bool operator==(Guid a, Guid b)
Indicates whether the values of two specified T:System.Guid objects are equal.
Definition: Guid.cs:1225
Provides functionality to format the value of an object into a string representation.
Definition: IFormattable.cs:8
bool Equals(Guid g)
Returns a value indicating whether this instance and a specified T:System.Guid object represent the s...
Definition: Guid.cs:1045