mscorlib(4.0.0.0) API with additions
ModuleHandle.cs
1 using System.Reflection;
6 using System.Security;
7 
8 namespace System
9 {
11  [ComVisible(true)]
12  public struct ModuleHandle
13  {
15  public static readonly ModuleHandle EmptyHandle = GetEmptyMH();
16 
17  private RuntimeModule m_ptr;
18 
21  public int MDStreamVersion
22  {
23  [SecuritySafeCritical]
24  get
25  {
26  return GetMDStreamVersion(GetRuntimeModule().GetNativeHandle());
27  }
28  }
29 
30  private static ModuleHandle GetEmptyMH()
31  {
32  return default(ModuleHandle);
33  }
34 
35  internal ModuleHandle(RuntimeModule module)
36  {
37  m_ptr = module;
38  }
39 
40  internal RuntimeModule GetRuntimeModule()
41  {
42  return m_ptr;
43  }
44 
45  internal bool IsNullHandle()
46  {
47  return m_ptr == null;
48  }
49 
52  public override int GetHashCode()
53  {
54  if (!(m_ptr != null))
55  {
56  return 0;
57  }
58  return m_ptr.GetHashCode();
59  }
60 
65  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
66  public override bool Equals(object obj)
67  {
68  if (!(obj is ModuleHandle))
69  {
70  return false;
71  }
72  ModuleHandle moduleHandle = (ModuleHandle)obj;
73  return moduleHandle.m_ptr == m_ptr;
74  }
75 
80  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
81  public bool Equals(ModuleHandle handle)
82  {
83  return handle.m_ptr == m_ptr;
84  }
85 
91  public static bool operator ==(ModuleHandle left, ModuleHandle right)
92  {
93  return left.Equals(right);
94  }
95 
101  public static bool operator !=(ModuleHandle left, ModuleHandle right)
102  {
103  return !left.Equals(right);
104  }
105 
106  [MethodImpl(MethodImplOptions.InternalCall)]
107  [SecurityCritical]
108  internal static extern IRuntimeMethodInfo GetDynamicMethod(DynamicMethod method, RuntimeModule module, string name, byte[] sig, Resolver resolver);
109 
110  [MethodImpl(MethodImplOptions.InternalCall)]
111  [SecurityCritical]
112  internal static extern int GetToken(RuntimeModule module);
113 
114  private static void ValidateModulePointer(RuntimeModule module)
115  {
116  if (module == null)
117  {
118  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullModuleHandle"));
119  }
120  }
121 
126  {
127  return ResolveTypeHandle(typeToken);
128  }
129 
138  public RuntimeTypeHandle ResolveTypeHandle(int typeToken)
139  {
140  return new RuntimeTypeHandle(ResolveTypeHandleInternal(GetRuntimeModule(), typeToken, null, null));
141  }
142 
155  public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
156  {
157  return new RuntimeTypeHandle(ResolveTypeHandleInternal(GetRuntimeModule(), typeToken, typeInstantiationContext, methodInstantiationContext));
158  }
159 
160  [SecuritySafeCritical]
161  internal unsafe static RuntimeType ResolveTypeHandleInternal(RuntimeModule module, int typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
162  {
163  ValidateModulePointer(module);
164  if (!GetMetadataImport(module).IsValidToken(typeToken))
165  {
166  throw new ArgumentOutOfRangeException("metadataToken", Environment.GetResourceString("Argument_InvalidToken", typeToken, new ModuleHandle(module)));
167  }
168  int length;
169  IntPtr[] array = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out length);
170  int length2;
171  IntPtr[] array2 = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out length2);
172  IntPtr[] array3 = array;
173  fixed (IntPtr* typeInstArgs = array3)
174  {
175  IntPtr[] array4 = array2;
176  fixed (IntPtr* methodInstArgs = array4)
177  {
178  RuntimeType o = null;
179  ResolveType(module, typeToken, typeInstArgs, length, methodInstArgs, length2, JitHelpers.GetObjectHandleOnStack(ref o));
180  GC.KeepAlive(typeInstantiationContext);
181  GC.KeepAlive(methodInstantiationContext);
182  return o;
183  }
184  }
185  }
186 
187  [DllImport("QCall", CharSet = CharSet.Unicode)]
188  [SecurityCritical]
189  [SuppressUnmanagedCodeSecurity]
190  private unsafe static extern void ResolveType(RuntimeModule module, int typeToken, IntPtr* typeInstArgs, int typeInstCount, IntPtr* methodInstArgs, int methodInstCount, ObjectHandleOnStack type);
191 
196  {
197  return ResolveMethodHandle(methodToken);
198  }
199 
208  public RuntimeMethodHandle ResolveMethodHandle(int methodToken)
209  {
210  return ResolveMethodHandle(methodToken, null, null);
211  }
212 
213  internal static IRuntimeMethodInfo ResolveMethodHandleInternal(RuntimeModule module, int methodToken)
214  {
215  return ResolveMethodHandleInternal(module, methodToken, null, null);
216  }
217 
230  public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
231  {
232  return new RuntimeMethodHandle(ResolveMethodHandleInternal(GetRuntimeModule(), methodToken, typeInstantiationContext, methodInstantiationContext));
233  }
234 
235  [SecuritySafeCritical]
236  internal static IRuntimeMethodInfo ResolveMethodHandleInternal(RuntimeModule module, int methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
237  {
238  int length;
239  IntPtr[] typeInstantiationContext2 = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out length);
240  int length2;
241  IntPtr[] methodInstantiationContext2 = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out length2);
242  RuntimeMethodHandleInternal runtimeMethodHandleInternal = ResolveMethodHandleInternalCore(module, methodToken, typeInstantiationContext2, length, methodInstantiationContext2, length2);
243  IRuntimeMethodInfo result = new RuntimeMethodInfoStub(runtimeMethodHandleInternal, RuntimeMethodHandle.GetLoaderAllocator(runtimeMethodHandleInternal));
244  GC.KeepAlive(typeInstantiationContext);
245  GC.KeepAlive(methodInstantiationContext);
246  return result;
247  }
248 
249  [SecurityCritical]
250  internal unsafe static RuntimeMethodHandleInternal ResolveMethodHandleInternalCore(RuntimeModule module, int methodToken, IntPtr[] typeInstantiationContext, int typeInstCount, IntPtr[] methodInstantiationContext, int methodInstCount)
251  {
252  ValidateModulePointer(module);
253  if (!GetMetadataImport(module.GetNativeHandle()).IsValidToken(methodToken))
254  {
255  throw new ArgumentOutOfRangeException("metadataToken", Environment.GetResourceString("Argument_InvalidToken", methodToken, new ModuleHandle(module)));
256  }
257  fixed (IntPtr* typeInstArgs = typeInstantiationContext)
258  {
259  fixed (IntPtr* methodInstArgs = methodInstantiationContext)
260  {
261  return ResolveMethod(module.GetNativeHandle(), methodToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount);
262  }
263  }
264  }
265 
266  [DllImport("QCall", CharSet = CharSet.Unicode)]
267  [SecurityCritical]
268  [SuppressUnmanagedCodeSecurity]
269  private unsafe static extern RuntimeMethodHandleInternal ResolveMethod(RuntimeModule module, int methodToken, IntPtr* typeInstArgs, int typeInstCount, IntPtr* methodInstArgs, int methodInstCount);
270 
275  {
276  return ResolveFieldHandle(fieldToken);
277  }
278 
287  public RuntimeFieldHandle ResolveFieldHandle(int fieldToken)
288  {
289  return new RuntimeFieldHandle(ResolveFieldHandleInternal(GetRuntimeModule(), fieldToken, null, null));
290  }
291 
304  public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
305  {
306  return new RuntimeFieldHandle(ResolveFieldHandleInternal(GetRuntimeModule(), fieldToken, typeInstantiationContext, methodInstantiationContext));
307  }
308 
309  [SecuritySafeCritical]
310  internal unsafe static IRuntimeFieldInfo ResolveFieldHandleInternal(RuntimeModule module, int fieldToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
311  {
312  ValidateModulePointer(module);
313  if (!GetMetadataImport(module.GetNativeHandle()).IsValidToken(fieldToken))
314  {
315  throw new ArgumentOutOfRangeException("metadataToken", Environment.GetResourceString("Argument_InvalidToken", fieldToken, new ModuleHandle(module)));
316  }
317  int length;
318  IntPtr[] array = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out length);
319  int length2;
320  IntPtr[] array2 = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out length2);
321  IntPtr[] array3 = array;
322  fixed (IntPtr* typeInstArgs = array3)
323  {
324  IntPtr[] array4 = array2;
325  fixed (IntPtr* methodInstArgs = array4)
326  {
327  IRuntimeFieldInfo o = null;
328  ResolveField(module.GetNativeHandle(), fieldToken, typeInstArgs, length, methodInstArgs, length2, JitHelpers.GetObjectHandleOnStack(ref o));
329  GC.KeepAlive(typeInstantiationContext);
330  GC.KeepAlive(methodInstantiationContext);
331  return o;
332  }
333  }
334  }
335 
336  [DllImport("QCall", CharSet = CharSet.Unicode)]
337  [SecurityCritical]
338  [SuppressUnmanagedCodeSecurity]
339  private unsafe static extern void ResolveField(RuntimeModule module, int fieldToken, IntPtr* typeInstArgs, int typeInstCount, IntPtr* methodInstArgs, int methodInstCount, ObjectHandleOnStack retField);
340 
341  [DllImport("QCall", CharSet = CharSet.Unicode)]
342  [SecurityCritical]
343  [SuppressUnmanagedCodeSecurity]
344  private static extern bool _ContainsPropertyMatchingHash(RuntimeModule module, int propertyToken, uint hash);
345 
346  [SecurityCritical]
347  internal static bool ContainsPropertyMatchingHash(RuntimeModule module, int propertyToken, uint hash)
348  {
349  return _ContainsPropertyMatchingHash(module.GetNativeHandle(), propertyToken, hash);
350  }
351 
352  [DllImport("QCall", CharSet = CharSet.Unicode)]
353  [SecurityCritical]
354  [SuppressUnmanagedCodeSecurity]
355  private static extern void GetAssembly(RuntimeModule handle, ObjectHandleOnStack retAssembly);
356 
357  [SecuritySafeCritical]
358  internal static RuntimeAssembly GetAssembly(RuntimeModule module)
359  {
360  RuntimeAssembly o = null;
361  GetAssembly(module.GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref o));
362  return o;
363  }
364 
365  [DllImport("QCall", CharSet = CharSet.Unicode)]
366  [SecurityCritical]
367  [SuppressUnmanagedCodeSecurity]
368  internal static extern void GetModuleType(RuntimeModule handle, ObjectHandleOnStack type);
369 
370  [SecuritySafeCritical]
371  internal static RuntimeType GetModuleType(RuntimeModule module)
372  {
373  RuntimeType o = null;
374  GetModuleType(module.GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref o));
375  return o;
376  }
377 
378  [DllImport("QCall", CharSet = CharSet.Unicode)]
379  [SecurityCritical]
380  [SuppressUnmanagedCodeSecurity]
381  private static extern void GetPEKind(RuntimeModule handle, out int peKind, out int machine);
382 
383  [SecuritySafeCritical]
384  internal static void GetPEKind(RuntimeModule module, out PortableExecutableKinds peKind, out ImageFileMachine machine)
385  {
386  GetPEKind(module.GetNativeHandle(), out int peKind2, out int machine2);
387  peKind = (PortableExecutableKinds)peKind2;
388  machine = (ImageFileMachine)machine2;
389  }
390 
391  [MethodImpl(MethodImplOptions.InternalCall)]
392  [SecurityCritical]
393  internal static extern int GetMDStreamVersion(RuntimeModule module);
394 
395  [MethodImpl(MethodImplOptions.InternalCall)]
396  [SecurityCritical]
397  private static extern IntPtr _GetMetadataImport(RuntimeModule module);
398 
399  [SecurityCritical]
400  internal static MetadataImport GetMetadataImport(RuntimeModule module)
401  {
402  return new MetadataImport(_GetMetadataImport(module.GetNativeHandle()), module);
403  }
404  }
405 }
override bool Equals(object obj)
Returns a T:System.Boolean value indicating whether the specified object is a T:System....
Definition: ModuleHandle.cs:66
RuntimeTypeHandle ResolveTypeHandle(int typeToken)
Returns a runtime type handle for the type identified by the specified metadata token.
RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
Returns a runtime type handle for the type identified by the specified metadata token,...
Defines and represents a dynamic method that can be compiled, executed, and discarded....
int MDStreamVersion
Gets the metadata stream version.
Definition: ModuleHandle.cs:22
static bool operator==(ModuleHandle left, ModuleHandle right)
Tests whether two T:System.ModuleHandle structures are equal.
Definition: ModuleHandle.cs:91
RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
Returns a runtime method handle for the method or constructor identified by the specified metadata to...
Definition: __Canon.cs:3
Represents a field using an internal metadata token.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
ImageFileMachine
Identifies the platform targeted by an executable.
Represents a runtime handle for a module.
Definition: ModuleHandle.cs:12
static void KeepAlive(object obj)
References the specified object, which makes it ineligible for garbage collection from the start of t...
Definition: GC.cs:267
Represents a type using an internal metadata token.
RuntimeMethodHandle ResolveMethodHandle(int methodToken)
Returns a runtime method handle for the method or constructor identified by the specified metadata to...
override int GetHashCode()
Returns the hash code for this instance.
Definition: ModuleHandle.cs:52
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
T:System.RuntimeMethodHandle is a handle to the internal metadata representation of a method.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken(int fieldToken)
Returns a runtime handle for the field identified by the specified metadata token.
bool Equals(ModuleHandle handle)
Returns a T:System.Boolean value indicating whether the specified T:System.ModuleHandle structure is ...
Definition: ModuleHandle.cs:81
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
RuntimeFieldHandle ResolveFieldHandle(int fieldToken)
Returns a runtime handle for the field identified by the specified metadata token.
RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken(int methodToken)
Returns a runtime method handle for the method or constructor identified by the specified metadata to...
static bool operator !=(ModuleHandle left, ModuleHandle right)
Tests whether two T:System.ModuleHandle structures are unequal.
PortableExecutableKinds
Identifies the nature of the code in an executable file.
static readonly ModuleHandle EmptyHandle
Represents an empty module handle.
Definition: ModuleHandle.cs:15
The exception that is thrown when a method call is invalid for the object's current state.
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken(int typeToken)
Returns a runtime type handle for the type identified by the specified metadata token.
RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
Returns a runtime field handle for the field identified by the specified metadata token,...