2 * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
6 using System
.Runtime
.InteropServices
;
8 namespace Mono
.PEToolkit
.Metadata
{
10 [StructLayoutAttribute(LayoutKind
.Sequential
)]
11 public struct MDToken
{
16 /// Creates new token with specified token type and record id.
18 /// <param name="type">Token type.</param>
19 /// <param name="rid">Record IDentifier.</param>
20 public MDToken(TokenType type
, int rid
)
22 token
= (int)type
| rid
;
26 /// Creates new Nil token of a given type.
28 /// <param name="type"></param>
29 public MDToken(TokenType type
) : this(type
, 0)
35 /// <param name="tok"></param>
36 public MDToken(MDToken tok
) : this(tok
.Type
, tok
.RID
)
42 /// Gets or sets metadata token Record IDentifier (RID).
46 return token
& (~
(int)TokenType
.__mask
);
49 token
&= (int)TokenType
.__mask
;
55 /// Gets or sets metadata token type.
57 public TokenType Type
{
59 return (TokenType
) token
& (TokenType
.__mask
);
62 token
&= ~
(int)TokenType
.__mask
;
68 /// Returns true if this token is a Nil token (it's RID is 0).
70 public bool IsNilToken
{
78 /// Returns token value.
80 /// <returns></returns>
81 public override int GetHashCode ()
88 // See Metadata Unmanaged API doc (10.8)
89 public int Compress(out int len
)
95 // Make room for type bits.
98 TokenType type
= this.Type
;
100 // Token type (table that this token indexes) is encoded
101 // in the least significant 2 bits:
107 case TokenType
.TypeDef
:
109 case TokenType
.TypeRef
:
112 case TokenType
.TypeSpec
:
115 case TokenType
.BaseType
:
119 // Invalid operation for this type of token.
123 len
= MDUtils
.CompressData(rid
, out res
);
129 unsafe public static int Size
{
135 public static implicit operator MDToken (uint val
) {
136 MDToken res
= new MDToken();
137 res
.token
= (int) val
;
141 public static implicit operator uint (MDToken tok
) {
142 return (uint)tok
.token
;
145 public override string ToString()
147 if (this.token
== 0) return "NULL";
148 return String
.Format("{0}[{1}]",
149 ((int)Type
>> (int)TokenType
.__shift
<= (int)TableId
.MAX
)
150 ? ((TableId
)((int)Type
>> (int)TokenType
.__shift
)).ToString()
151 : Type
.ToString(), RID
);
152 //String.Format ("type = {0}, RID = {1}", Type, RID);