mscorlib(4.0.0.0) API with additions
RuntimeMethodHandle.cs
1 using System.Diagnostics;
2 using System.Reflection;
7 using System.Security;
8 using System.Threading;
9 
10 namespace System
11 {
14  [Serializable]
15  [ComVisible(true)]
16  [__DynamicallyInvokable]
18  {
19  private IRuntimeMethodInfo m_value;
20 
21  internal static RuntimeMethodHandle EmptyHandle => default(RuntimeMethodHandle);
22 
25  public IntPtr Value
26  {
27  [SecurityCritical]
28  get
29  {
30  if (m_value == null)
31  {
32  return IntPtr.Zero;
33  }
34  return m_value.Value.Value;
35  }
36  }
37 
38  internal static IRuntimeMethodInfo EnsureNonNullMethodInfo(IRuntimeMethodInfo method)
39  {
40  if (method == null)
41  {
42  throw new ArgumentNullException(null, Environment.GetResourceString("Arg_InvalidHandle"));
43  }
44  return method;
45  }
46 
47  internal RuntimeMethodHandle(IRuntimeMethodInfo method)
48  {
49  m_value = method;
50  }
51 
52  internal IRuntimeMethodInfo GetMethodInfo()
53  {
54  return m_value;
55  }
56 
57  [SecurityCritical]
58  private static IntPtr GetValueInternal(RuntimeMethodHandle rmh)
59  {
60  return rmh.Value;
61  }
62 
63  [SecurityCritical]
64  private RuntimeMethodHandle(SerializationInfo info, StreamingContext context)
65  {
66  if (info == null)
67  {
68  throw new ArgumentNullException("info");
69  }
70  MethodBase methodBase = (MethodBase)info.GetValue("MethodObj", typeof(MethodBase));
71  RuntimeMethodHandle methodHandle = methodBase.MethodHandle;
72  m_value = methodHandle.m_value;
73  if (m_value == null)
74  {
75  throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
76  }
77  }
78 
86  [SecurityCritical]
88  {
89  if (info == null)
90  {
91  throw new ArgumentNullException("info");
92  }
93  if (m_value == null)
94  {
95  throw new SerializationException(Environment.GetResourceString("Serialization_InvalidFieldState"));
96  }
97  MethodBase methodBase = RuntimeType.GetMethodBase(m_value);
98  info.AddValue("MethodObj", methodBase, typeof(MethodBase));
99  }
100 
103  [SecuritySafeCritical]
104  [__DynamicallyInvokable]
105  public override int GetHashCode()
106  {
107  return ValueType.GetHashCodeOfPtr(Value);
108  }
109 
114  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
115  [SecuritySafeCritical]
116  [__DynamicallyInvokable]
117  public override bool Equals(object obj)
118  {
119  if (!(obj is RuntimeMethodHandle))
120  {
121  return false;
122  }
123  return ((RuntimeMethodHandle)obj).Value == Value;
124  }
125 
131  [__DynamicallyInvokable]
132  public static bool operator ==(RuntimeMethodHandle left, RuntimeMethodHandle right)
133  {
134  return left.Equals(right);
135  }
136 
142  [__DynamicallyInvokable]
143  public static bool operator !=(RuntimeMethodHandle left, RuntimeMethodHandle right)
144  {
145  return !left.Equals(right);
146  }
147 
152  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
153  [SecuritySafeCritical]
154  [__DynamicallyInvokable]
155  public bool Equals(RuntimeMethodHandle handle)
156  {
157  return handle.Value == Value;
158  }
159 
160  internal bool IsNullHandle()
161  {
162  return m_value == null;
163  }
164 
165  [DllImport("QCall", CharSet = CharSet.Unicode)]
166  [SecurityCritical]
167  [SuppressUnmanagedCodeSecurity]
168  internal static extern IntPtr GetFunctionPointer(RuntimeMethodHandleInternal handle);
169 
173  [SecurityCritical]
175  {
176  IntPtr functionPointer = GetFunctionPointer(EnsureNonNullMethodInfo(m_value).Value);
177  GC.KeepAlive(m_value);
178  return functionPointer;
179  }
180 
181  [MethodImpl(MethodImplOptions.InternalCall)]
182  [SecurityCritical]
183  internal static extern void CheckLinktimeDemands(IRuntimeMethodInfo method, RuntimeModule module, bool isDecoratedTargetSecurityTransparent);
184 
185  [DllImport("QCall", CharSet = CharSet.Unicode)]
186  [SecurityCritical]
187  [SuppressUnmanagedCodeSecurity]
188  internal static extern bool IsCAVisibleFromDecoratedType(RuntimeTypeHandle attrTypeHandle, IRuntimeMethodInfo attrCtor, RuntimeTypeHandle sourceTypeHandle, RuntimeModule sourceModule);
189 
190  [MethodImpl(MethodImplOptions.InternalCall)]
191  [SecurityCritical]
192  private static extern IRuntimeMethodInfo _GetCurrentMethod(ref StackCrawlMark stackMark);
193 
194  [SecuritySafeCritical]
195  internal static IRuntimeMethodInfo GetCurrentMethod(ref StackCrawlMark stackMark)
196  {
197  return _GetCurrentMethod(ref stackMark);
198  }
199 
200  [MethodImpl(MethodImplOptions.InternalCall)]
201  [SecurityCritical]
202  internal static extern MethodAttributes GetAttributes(RuntimeMethodHandleInternal method);
203 
204  [SecurityCritical]
205  internal static MethodAttributes GetAttributes(IRuntimeMethodInfo method)
206  {
207  MethodAttributes attributes = GetAttributes(method.Value);
208  GC.KeepAlive(method);
209  return attributes;
210  }
211 
212  [MethodImpl(MethodImplOptions.InternalCall)]
213  [SecuritySafeCritical]
214  internal static extern MethodImplAttributes GetImplAttributes(IRuntimeMethodInfo method);
215 
216  [DllImport("QCall", CharSet = CharSet.Unicode)]
217  [SecurityCritical]
218  [SuppressUnmanagedCodeSecurity]
219  private static extern void ConstructInstantiation(IRuntimeMethodInfo method, TypeNameFormatFlags format, StringHandleOnStack retString);
220 
221  [SecuritySafeCritical]
222  internal static string ConstructInstantiation(IRuntimeMethodInfo method, TypeNameFormatFlags format)
223  {
224  string s = null;
225  ConstructInstantiation(EnsureNonNullMethodInfo(method), format, JitHelpers.GetStringHandleOnStack(ref s));
226  return s;
227  }
228 
229  [MethodImpl(MethodImplOptions.InternalCall)]
230  [SecurityCritical]
231  internal static extern RuntimeType GetDeclaringType(RuntimeMethodHandleInternal method);
232 
233  [SecuritySafeCritical]
234  internal static RuntimeType GetDeclaringType(IRuntimeMethodInfo method)
235  {
236  RuntimeType declaringType = GetDeclaringType(method.Value);
237  GC.KeepAlive(method);
238  return declaringType;
239  }
240 
241  [MethodImpl(MethodImplOptions.InternalCall)]
242  [SecurityCritical]
243  internal static extern int GetSlot(RuntimeMethodHandleInternal method);
244 
245  [SecurityCritical]
246  internal static int GetSlot(IRuntimeMethodInfo method)
247  {
248  int slot = GetSlot(method.Value);
249  GC.KeepAlive(method);
250  return slot;
251  }
252 
253  [MethodImpl(MethodImplOptions.InternalCall)]
254  [SecurityCritical]
255  internal static extern int GetMethodDef(IRuntimeMethodInfo method);
256 
257  [MethodImpl(MethodImplOptions.InternalCall)]
258  [SecurityCritical]
259  internal static extern string GetName(RuntimeMethodHandleInternal method);
260 
261  [SecurityCritical]
262  internal static string GetName(IRuntimeMethodInfo method)
263  {
264  string name = GetName(method.Value);
265  GC.KeepAlive(method);
266  return name;
267  }
268 
269  [MethodImpl(MethodImplOptions.InternalCall)]
270  [SecurityCritical]
271  private unsafe static extern void* _GetUtf8Name(RuntimeMethodHandleInternal method);
272 
273  [SecurityCritical]
274  internal unsafe static Utf8String GetUtf8Name(RuntimeMethodHandleInternal method)
275  {
276  return new Utf8String(_GetUtf8Name(method));
277  }
278 
279  [MethodImpl(MethodImplOptions.InternalCall)]
280  [SecurityCritical]
281  internal static extern bool MatchesNameHash(RuntimeMethodHandleInternal method, uint hash);
282 
283  [MethodImpl(MethodImplOptions.InternalCall)]
284  [SecuritySafeCritical]
285  [DebuggerStepThrough]
286  [DebuggerHidden]
287  internal static extern object InvokeMethod(object target, object[] arguments, Signature sig, bool constructor);
288 
289  [SecurityCritical]
290  internal static INVOCATION_FLAGS GetSecurityFlags(IRuntimeMethodInfo handle)
291  {
292  return (INVOCATION_FLAGS)GetSpecialSecurityFlags(handle);
293  }
294 
295  [MethodImpl(MethodImplOptions.InternalCall)]
296  [SecurityCritical]
297  internal static extern uint GetSpecialSecurityFlags(IRuntimeMethodInfo method);
298 
299  [MethodImpl(MethodImplOptions.InternalCall)]
300  [SecurityCritical]
301  internal static extern void PerformSecurityCheck(object obj, RuntimeMethodHandleInternal method, RuntimeType parent, uint invocationFlags);
302 
303  [SecurityCritical]
304  internal static void PerformSecurityCheck(object obj, IRuntimeMethodInfo method, RuntimeType parent, uint invocationFlags)
305  {
306  PerformSecurityCheck(obj, method.Value, parent, invocationFlags);
307  GC.KeepAlive(method);
308  }
309 
310  [MethodImpl(MethodImplOptions.InternalCall)]
311  [SecuritySafeCritical]
312  [DebuggerStepThrough]
313  [DebuggerHidden]
314  internal static extern void SerializationInvoke(IRuntimeMethodInfo method, object target, SerializationInfo info, ref StreamingContext context);
315 
316  [MethodImpl(MethodImplOptions.InternalCall)]
317  [SecurityCritical]
318  internal static extern bool _IsTokenSecurityTransparent(RuntimeModule module, int metaDataToken);
319 
320  [SecurityCritical]
321  internal static bool IsTokenSecurityTransparent(Module module, int metaDataToken)
322  {
323  return _IsTokenSecurityTransparent(module.ModuleHandle.GetRuntimeModule(), metaDataToken);
324  }
325 
326  [DllImport("QCall", CharSet = CharSet.Unicode)]
327  [SecurityCritical]
328  [SuppressUnmanagedCodeSecurity]
329  [return: MarshalAs(UnmanagedType.Bool)]
330  private static extern bool _IsSecurityCritical(IRuntimeMethodInfo method);
331 
332  [SecuritySafeCritical]
333  internal static bool IsSecurityCritical(IRuntimeMethodInfo method)
334  {
335  return _IsSecurityCritical(method);
336  }
337 
338  [DllImport("QCall", CharSet = CharSet.Unicode)]
339  [SecurityCritical]
340  [SuppressUnmanagedCodeSecurity]
341  [return: MarshalAs(UnmanagedType.Bool)]
342  private static extern bool _IsSecuritySafeCritical(IRuntimeMethodInfo method);
343 
344  [SecuritySafeCritical]
345  internal static bool IsSecuritySafeCritical(IRuntimeMethodInfo method)
346  {
347  return _IsSecuritySafeCritical(method);
348  }
349 
350  [DllImport("QCall", CharSet = CharSet.Unicode)]
351  [SecurityCritical]
352  [SuppressUnmanagedCodeSecurity]
353  [return: MarshalAs(UnmanagedType.Bool)]
354  private static extern bool _IsSecurityTransparent(IRuntimeMethodInfo method);
355 
356  [SecuritySafeCritical]
357  internal static bool IsSecurityTransparent(IRuntimeMethodInfo method)
358  {
359  return _IsSecurityTransparent(method);
360  }
361 
362  [DllImport("QCall", CharSet = CharSet.Unicode)]
363  [SecurityCritical]
364  [SuppressUnmanagedCodeSecurity]
365  private static extern void GetMethodInstantiation(RuntimeMethodHandleInternal method, ObjectHandleOnStack types, bool fAsRuntimeTypeArray);
366 
367  [SecuritySafeCritical]
368  internal static RuntimeType[] GetMethodInstantiationInternal(IRuntimeMethodInfo method)
369  {
370  RuntimeType[] o = null;
371  GetMethodInstantiation(EnsureNonNullMethodInfo(method).Value, JitHelpers.GetObjectHandleOnStack(ref o), fAsRuntimeTypeArray: true);
372  GC.KeepAlive(method);
373  return o;
374  }
375 
376  [SecuritySafeCritical]
377  internal static RuntimeType[] GetMethodInstantiationInternal(RuntimeMethodHandleInternal method)
378  {
379  RuntimeType[] o = null;
380  GetMethodInstantiation(method, JitHelpers.GetObjectHandleOnStack(ref o), fAsRuntimeTypeArray: true);
381  return o;
382  }
383 
384  [SecuritySafeCritical]
385  internal static Type[] GetMethodInstantiationPublic(IRuntimeMethodInfo method)
386  {
387  RuntimeType[] o = null;
388  GetMethodInstantiation(EnsureNonNullMethodInfo(method).Value, JitHelpers.GetObjectHandleOnStack(ref o), fAsRuntimeTypeArray: false);
389  GC.KeepAlive(method);
390  return o;
391  }
392 
393  [MethodImpl(MethodImplOptions.InternalCall)]
394  [SecurityCritical]
395  internal static extern bool HasMethodInstantiation(RuntimeMethodHandleInternal method);
396 
397  [SecuritySafeCritical]
398  internal static bool HasMethodInstantiation(IRuntimeMethodInfo method)
399  {
400  bool result = HasMethodInstantiation(method.Value);
401  GC.KeepAlive(method);
402  return result;
403  }
404 
405  [MethodImpl(MethodImplOptions.InternalCall)]
406  [SecurityCritical]
407  internal static extern RuntimeMethodHandleInternal GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation);
408 
409  [MethodImpl(MethodImplOptions.InternalCall)]
410  [SecurityCritical]
411  internal static extern RuntimeMethodHandleInternal GetMethodFromCanonical(RuntimeMethodHandleInternal method, RuntimeType declaringType);
412 
413  [MethodImpl(MethodImplOptions.InternalCall)]
414  [SecurityCritical]
415  internal static extern bool IsGenericMethodDefinition(RuntimeMethodHandleInternal method);
416 
417  [SecuritySafeCritical]
418  internal static bool IsGenericMethodDefinition(IRuntimeMethodInfo method)
419  {
420  bool result = IsGenericMethodDefinition(method.Value);
421  GC.KeepAlive(method);
422  return result;
423  }
424 
425  [MethodImpl(MethodImplOptions.InternalCall)]
426  [SecuritySafeCritical]
427  internal static extern bool IsTypicalMethodDefinition(IRuntimeMethodInfo method);
428 
429  [DllImport("QCall", CharSet = CharSet.Unicode)]
430  [SecurityCritical]
431  [SuppressUnmanagedCodeSecurity]
432  private static extern void GetTypicalMethodDefinition(IRuntimeMethodInfo method, ObjectHandleOnStack outMethod);
433 
434  [SecuritySafeCritical]
435  internal static IRuntimeMethodInfo GetTypicalMethodDefinition(IRuntimeMethodInfo method)
436  {
437  if (!IsTypicalMethodDefinition(method))
438  {
439  GetTypicalMethodDefinition(method, JitHelpers.GetObjectHandleOnStack(ref method));
440  }
441  return method;
442  }
443 
444  [DllImport("QCall", CharSet = CharSet.Unicode)]
445  [SecurityCritical]
446  [SuppressUnmanagedCodeSecurity]
447  private static extern void StripMethodInstantiation(IRuntimeMethodInfo method, ObjectHandleOnStack outMethod);
448 
449  [SecuritySafeCritical]
450  internal static IRuntimeMethodInfo StripMethodInstantiation(IRuntimeMethodInfo method)
451  {
452  IRuntimeMethodInfo o = method;
453  StripMethodInstantiation(method, JitHelpers.GetObjectHandleOnStack(ref o));
454  return o;
455  }
456 
457  [MethodImpl(MethodImplOptions.InternalCall)]
458  [SecuritySafeCritical]
459  internal static extern bool IsDynamicMethod(RuntimeMethodHandleInternal method);
460 
461  [DllImport("QCall", CharSet = CharSet.Unicode)]
462  [SecurityCritical]
463  [SuppressUnmanagedCodeSecurity]
464  internal static extern void Destroy(RuntimeMethodHandleInternal method);
465 
466  [MethodImpl(MethodImplOptions.InternalCall)]
467  [SecuritySafeCritical]
468  internal static extern Resolver GetResolver(RuntimeMethodHandleInternal method);
469 
470  [DllImport("QCall", CharSet = CharSet.Unicode)]
471  [SecurityCritical]
472  [SuppressUnmanagedCodeSecurity]
473  private static extern void GetCallerType(StackCrawlMarkHandle stackMark, ObjectHandleOnStack retType);
474 
475  [SecuritySafeCritical]
476  internal static RuntimeType GetCallerType(ref StackCrawlMark stackMark)
477  {
478  RuntimeType o = null;
479  GetCallerType(JitHelpers.GetStackCrawlMarkHandle(ref stackMark), JitHelpers.GetObjectHandleOnStack(ref o));
480  return o;
481  }
482 
483  [MethodImpl(MethodImplOptions.InternalCall)]
484  [SecuritySafeCritical]
485  internal static extern MethodBody GetMethodBody(IRuntimeMethodInfo method, RuntimeType declaringType);
486 
487  [MethodImpl(MethodImplOptions.InternalCall)]
488  [SecurityCritical]
489  internal static extern bool IsConstructor(RuntimeMethodHandleInternal method);
490 
491  [MethodImpl(MethodImplOptions.InternalCall)]
492  [SecurityCritical]
493  internal static extern LoaderAllocator GetLoaderAllocator(RuntimeMethodHandleInternal method);
494  }
495 }
IntPtr GetFunctionPointer()
Obtains a pointer to the method represented by this instance.
Performs reflection on a module.
Definition: Module.cs:17
static bool operator==(RuntimeMethodHandle left, RuntimeMethodHandle right)
Indicates whether two instances of T:System.RuntimeMethodHandle are equal.
MethodAttributes
Specifies flags for method attributes. These flags are defined in the corhdr.h file.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
MethodImplAttributes
Specifies flags for the attributes of a method implementation.
override int GetHashCode()
Returns the hash code for this instance.
Definition: __Canon.cs:3
abstract RuntimeMethodHandle MethodHandle
Gets a handle to the internal metadata representation of a method.
Definition: MethodBase.cs:46
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.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
override bool Equals(object obj)
Indicates whether this instance is equal to a specified object.
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data necessary to deserialize t...
IntPtr Value
Gets the value of this instance.
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
Provides access to the metadata and MSIL for the body of a method.
Definition: MethodBody.cs:8
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.
static bool operator !=(RuntimeMethodHandle left, RuntimeMethodHandle right)
Indicates whether two instances of T:System.RuntimeMethodHandle are not equal.
UnmanagedType
Identifies how to marshal parameters or fields to unmanaged code.
Definition: UnmanagedType.cs:7
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
Provides information about methods and constructors.
Definition: MethodBase.cs:19
The exception thrown when an error occurs during serialization or deserialization.
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
Specifies that the class can be serialized.
bool Equals(RuntimeMethodHandle handle)
Indicates whether this instance is equal to a specified T:System.RuntimeMethodHandle.
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
ModuleHandle ModuleHandle
Gets a handle for the module.
Definition: Module.cs:148
Provides the base class for value types.
Definition: ValueType.cs:12