2 // System.ModuleHandle.cs
5 // Zoltan Varga (vargaz@gmail.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
33 using System.Reflection;
34 using System.Runtime.Serialization;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
38 using System.Runtime.ConstrainedExecution;
43 public struct ModuleHandle
47 public static readonly ModuleHandle EmptyHandle = new ModuleHandle (IntPtr.Zero);
49 internal ModuleHandle (IntPtr v)
54 internal IntPtr Value {
60 public int MDStreamVersion {
62 if (value == IntPtr.Zero)
63 throw new ArgumentNullException (String.Empty, "Invalid handle");
64 return Module.GetMDStreamVersion (value);
68 internal void GetPEKind (out PortableExecutableKinds peKind, out ImageFileMachine machine)
70 if (value == IntPtr.Zero)
71 throw new ArgumentNullException (String.Empty, "Invalid handle");
72 Module.GetPEKind (value, out peKind, out machine);
75 public RuntimeFieldHandle ResolveFieldHandle (int fieldToken)
77 return ResolveFieldHandle (fieldToken, null, null);
80 public RuntimeMethodHandle ResolveMethodHandle (int methodToken)
82 return ResolveMethodHandle (methodToken, null, null);
85 public RuntimeTypeHandle ResolveTypeHandle (int typeToken)
87 return ResolveTypeHandle (typeToken, null, null);
90 private IntPtr[] ptrs_from_handles (RuntimeTypeHandle[] handles) {
94 IntPtr[] res = new IntPtr [handles.Length];
95 for (int i = 0; i < handles.Length; ++i)
96 res [i] = handles [i].Value;
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 ()));
111 return new RuntimeTypeHandle (res);
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 ()));
124 return new RuntimeMethodHandle (res);
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 ()));
137 return new RuntimeFieldHandle (res);
140 public RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken (int fieldToken) {
141 return ResolveFieldHandle (fieldToken);
144 public RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken (int methodToken)
146 return ResolveMethodHandle (methodToken);
149 public RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken (int typeToken)
151 return ResolveTypeHandle (typeToken);
154 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
155 public override bool Equals (object obj)
157 if (obj == null || GetType () != obj.GetType ())
160 return value == ((ModuleHandle)obj).Value;
163 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
164 public bool Equals (ModuleHandle handle)
166 return value == handle.Value;
169 public override int GetHashCode ()
171 return value.GetHashCode ();
174 public static bool operator == (ModuleHandle left, ModuleHandle right)
176 return Equals (left, right);
179 public static bool operator != (ModuleHandle left, ModuleHandle right)
181 return !Equals (left, right);