[runtime] Fix Empty generic enumerator equality
[mono.git] / mcs / class / corlib / System / ModuleHandle.cs
1 //
2 // System.ModuleHandle.cs
3 //
4 // Author:
5 //   Zoltan Varga (vargaz@gmail.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37
38 using System.Runtime.ConstrainedExecution;
39
40 namespace System
41 {
42         [ComVisible (true)]
43         public struct ModuleHandle
44         {
45                 IntPtr value;
46
47                 public static readonly ModuleHandle EmptyHandle = new ModuleHandle (IntPtr.Zero);
48
49                 internal ModuleHandle (IntPtr v)
50                 {
51                         value = v;
52                 }
53
54                 internal IntPtr Value {
55                         get {
56                                 return value;
57                         }
58                 }
59
60                 public int MDStreamVersion { 
61                         get {
62                                 if (value == IntPtr.Zero)
63                                         throw new ArgumentNullException (String.Empty, "Invalid handle");
64                                 return Module.GetMDStreamVersion (value);
65                         }
66                 }
67
68                 internal void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine)
69                 {
70                         if (value == IntPtr.Zero)
71                                 throw new ArgumentNullException (String.Empty, "Invalid handle");
72                         Module.GetPEKind (value, out peKind, out machine);
73                 }
74
75                 public RuntimeFieldHandle ResolveFieldHandle (int fieldToken)
76                 {
77                         return ResolveFieldHandle (fieldToken, null, null);
78                 }
79
80                 public RuntimeMethodHandle ResolveMethodHandle (int methodToken)
81                 {
82                         return ResolveMethodHandle (methodToken, null, null);
83                 }
84
85                 public RuntimeTypeHandle ResolveTypeHandle (int typeToken)
86                 {
87                         return ResolveTypeHandle (typeToken, null, null);
88                 }
89
90                 private IntPtr[] ptrs_from_handles (RuntimeTypeHandle[] handles) {
91                         if (handles == null)
92                                 return null;
93                         else {
94                                 IntPtr[] res = new IntPtr [handles.Length];
95                                 for (int i = 0; i < handles.Length; ++i)
96                                         res [i] = handles [i].Value;
97                                 return res;
98                         }
99                 }
100                                 
101                 public RuntimeTypeHandle ResolveTypeHandle (int typeToken,
102                                                                                                         RuntimeTypeHandle[] typeInstantiationContext,
103                                                                                                         RuntimeTypeHandle[] methodInstantiationContext) {
104                         ResolveTokenError error;
105                         if (value == IntPtr.Zero)
106                                 throw new ArgumentNullException (String.Empty, "Invalid handle");
107                         IntPtr res = Module.ResolveTypeToken (value, typeToken, ptrs_from_handles (typeInstantiationContext), ptrs_from_handles (methodInstantiationContext), out error);
108                         if (res == IntPtr.Zero)
109                                 throw new TypeLoadException (String.Format ("Could not load type '0x{0:x}' from assembly '0x{1:x}'", typeToken, value.ToInt64 ()));
110                         else
111                                 return new RuntimeTypeHandle (res);
112                 }                       
113
114                 public RuntimeMethodHandle ResolveMethodHandle (int methodToken,
115                                                                                                                 RuntimeTypeHandle[] typeInstantiationContext,
116                                                                                                                 RuntimeTypeHandle[] methodInstantiationContext) {
117                         ResolveTokenError error;
118                         if (value == IntPtr.Zero)
119                                 throw new ArgumentNullException (String.Empty, "Invalid handle");
120                         IntPtr res = Module.ResolveMethodToken (value, methodToken, ptrs_from_handles (typeInstantiationContext), ptrs_from_handles (methodInstantiationContext), out error);
121                         if (res == IntPtr.Zero)
122                                 throw new Exception (String.Format ("Could not load method '0x{0:x}' from assembly '0x{1:x}'", methodToken, value.ToInt64 ()));
123                         else
124                                 return new RuntimeMethodHandle (res);
125                 }                       
126
127                 public RuntimeFieldHandle ResolveFieldHandle (int fieldToken,
128                                                                                                           RuntimeTypeHandle[] typeInstantiationContext,
129                                                                                                           RuntimeTypeHandle[] methodInstantiationContext) {
130                         ResolveTokenError error;
131                         if (value == IntPtr.Zero)
132                                 throw new ArgumentNullException (String.Empty, "Invalid handle");
133                         IntPtr res = Module.ResolveFieldToken (value, fieldToken, ptrs_from_handles (typeInstantiationContext), ptrs_from_handles (methodInstantiationContext), out error);
134                         if (res == IntPtr.Zero)
135                                 throw new Exception (String.Format ("Could not load field '0x{0:x}' from assembly '0x{1:x}'", fieldToken, value.ToInt64 ()));
136                         else
137                                 return new RuntimeFieldHandle (res);
138                 }                       
139
140                 public RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken (int fieldToken) {
141                         return ResolveFieldHandle (fieldToken);
142                 }
143
144                 public RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken (int methodToken)
145                 {
146                         return ResolveMethodHandle (methodToken);
147                 }
148
149                 public RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken (int typeToken)
150                 {
151                         return ResolveTypeHandle (typeToken);
152                 }
153
154                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
155                 public override bool Equals (object obj)
156                 {
157                         if (obj == null || GetType () != obj.GetType ())
158                                 return false;
159
160                         return value == ((ModuleHandle)obj).Value;
161                 }
162
163                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
164                 public bool Equals (ModuleHandle handle)
165                 {
166                         return value == handle.Value;
167                 }
168
169                 public override int GetHashCode ()
170                 {
171                         return value.GetHashCode ();
172                 }
173
174                 public static bool operator == (ModuleHandle left, ModuleHandle right)
175                 {
176                         return Equals (left, right);
177                 }
178
179                 public static bool operator != (ModuleHandle left, ModuleHandle right)
180                 {
181                         return !Equals (left, right);
182                 }
183         }
184 }
185