MetadataToken.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // MetadataToken.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. namespace Mono.Cecil {
  29. public struct MetadataToken {
  30. readonly uint token;
  31. public uint RID {
  32. get { return token & 0x00ffffff; }
  33. }
  34. public TokenType TokenType {
  35. get { return (TokenType) (token & 0xff000000); }
  36. }
  37. public static readonly MetadataToken Zero = new MetadataToken ((uint) 0);
  38. public MetadataToken (uint token)
  39. {
  40. this.token = token;
  41. }
  42. public MetadataToken (TokenType type)
  43. : this (type, 0)
  44. {
  45. }
  46. public MetadataToken (TokenType type, uint rid)
  47. {
  48. token = (uint) type | rid;
  49. }
  50. public MetadataToken (TokenType type, int rid)
  51. {
  52. token = (uint) type | (uint) rid;
  53. }
  54. public int ToInt32 ()
  55. {
  56. return (int) token;
  57. }
  58. public uint ToUInt32 ()
  59. {
  60. return token;
  61. }
  62. public override int GetHashCode ()
  63. {
  64. return (int) token;
  65. }
  66. public override bool Equals (object obj)
  67. {
  68. if (obj is MetadataToken) {
  69. var other = (MetadataToken) obj;
  70. return other.token == token;
  71. }
  72. return false;
  73. }
  74. public static bool operator == (MetadataToken one, MetadataToken other)
  75. {
  76. return one.token == other.token;
  77. }
  78. public static bool operator != (MetadataToken one, MetadataToken other)
  79. {
  80. return one.token != other.token;
  81. }
  82. public override string ToString ()
  83. {
  84. return string.Format ("[{0}:0x{1}]", TokenType, RID.ToString ("x4"));
  85. }
  86. }
  87. }