MetadataToken.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. namespace ILRuntime.Mono.Cecil {
  12. public struct MetadataToken : IEquatable<MetadataToken> {
  13. readonly uint token;
  14. public uint RID {
  15. get { return token & 0x00ffffff; }
  16. }
  17. public TokenType TokenType {
  18. get { return (TokenType) (token & 0xff000000); }
  19. }
  20. public static readonly MetadataToken Zero = new MetadataToken ((uint) 0);
  21. public MetadataToken (uint token)
  22. {
  23. this.token = token;
  24. }
  25. public MetadataToken (TokenType type)
  26. : this (type, 0)
  27. {
  28. }
  29. public MetadataToken (TokenType type, uint rid)
  30. {
  31. token = (uint) type | rid;
  32. }
  33. public MetadataToken (TokenType type, int rid)
  34. {
  35. token = (uint) type | (uint) rid;
  36. }
  37. public int ToInt32 ()
  38. {
  39. return (int) token;
  40. }
  41. public uint ToUInt32 ()
  42. {
  43. return token;
  44. }
  45. public override int GetHashCode ()
  46. {
  47. return (int) token;
  48. }
  49. public bool Equals (MetadataToken other)
  50. {
  51. return other.token == token;
  52. }
  53. public override bool Equals (object obj)
  54. {
  55. if (obj is MetadataToken) {
  56. var other = (MetadataToken) obj;
  57. return other.token == token;
  58. }
  59. return false;
  60. }
  61. public static bool operator == (MetadataToken one, MetadataToken other)
  62. {
  63. return one.token == other.token;
  64. }
  65. public static bool operator != (MetadataToken one, MetadataToken other)
  66. {
  67. return one.token != other.token;
  68. }
  69. public override string ToString ()
  70. {
  71. return string.Format ("[{0}:0x{1}]", TokenType, RID.ToString ("x4"));
  72. }
  73. }
  74. }