1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
9 using System.Reflection;
10 using System.Reflection.Emit;
12 using System.Runtime.ConstrainedExecution;
13 using System.Diagnostics;
14 using System.Runtime.Serialization;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
17 using System.Threading;
18 using System.Runtime.Versioning;
20 using System.Globalization;
21 using System.Security;
22 using Microsoft.Win32.SafeHandles;
23 using System.Diagnostics.Contracts;
24 using StackCrawlMark = System.Threading.StackCrawlMark;
26 public unsafe struct RuntimeTypeHandle : ISerializable
28 // Returns handle for interop with EE. The handle is guaranteed to be non-null.
29 internal RuntimeTypeHandle GetNativeHandle()
31 // Create local copy to avoid a race condition
32 RuntimeType type = m_type;
34 throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
35 return new RuntimeTypeHandle(type);
38 // Returns type for interop with EE. The type is guaranteed to be non-null.
39 internal RuntimeType GetTypeChecked()
41 // Create local copy to avoid a race condition
42 RuntimeType type = m_type;
44 throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
48 [MethodImplAttribute(MethodImplOptions.InternalCall)]
49 internal extern static bool IsInstanceOfType(RuntimeType type, Object o);
51 internal unsafe static Type GetTypeHelper(Type typeStart, Type[] genericArgs, IntPtr pModifiers, int cModifiers)
53 Type type = typeStart;
55 if (genericArgs != null)
57 type = type.MakeGenericType(genericArgs);
62 int* arModifiers = (int*)pModifiers.ToPointer();
63 for (int i = 0; i < cModifiers; i++)
65 if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.Ptr)
66 type = type.MakePointerType();
68 else if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.ByRef)
69 type = type.MakeByRefType();
71 else if ((CorElementType)Marshal.ReadInt32((IntPtr)arModifiers, i * sizeof(int)) == CorElementType.SzArray)
72 type = type.MakeArrayType();
75 type = type.MakeArrayType(Marshal.ReadInt32((IntPtr)arModifiers, ++i * sizeof(int)));
82 public static bool operator ==(RuntimeTypeHandle left, object right) { return left.Equals(right); }
84 public static bool operator ==(object left, RuntimeTypeHandle right) { return right.Equals(left); }
86 public static bool operator !=(RuntimeTypeHandle left, object right) { return !left.Equals(right); }
88 public static bool operator !=(object left, RuntimeTypeHandle right) { return !right.Equals(left); }
91 // This is the RuntimeType for the type
92 private RuntimeType m_type;
94 public override int GetHashCode()
96 return m_type != null ? m_type.GetHashCode() : 0;
99 public override bool Equals(object obj)
101 if (!(obj is RuntimeTypeHandle))
104 RuntimeTypeHandle handle = (RuntimeTypeHandle)obj;
105 return handle.m_type == m_type;
108 public bool Equals(RuntimeTypeHandle handle)
110 return handle.m_type == m_type;
117 return m_type != null ? m_type.m_handle : IntPtr.Zero;
121 [MethodImpl(MethodImplOptions.InternalCall)]
122 internal static extern IntPtr GetValueInternal(RuntimeTypeHandle handle);
124 internal RuntimeTypeHandle(RuntimeType type)
129 internal static bool IsTypeDefinition(RuntimeType type)
131 CorElementType corElemType = GetCorElementType(type);
132 if (!((corElemType >= CorElementType.Void && corElemType < CorElementType.Ptr) ||
133 corElemType == CorElementType.ValueType ||
134 corElemType == CorElementType.Class ||
135 corElemType == CorElementType.TypedByRef ||
136 corElemType == CorElementType.I ||
137 corElemType == CorElementType.U ||
138 corElemType == CorElementType.Object))
141 if (HasInstantiation(type) && !IsGenericTypeDefinition(type))
147 internal static bool IsPrimitive(RuntimeType type)
149 CorElementType corElemType = GetCorElementType(type);
150 return (corElemType >= CorElementType.Boolean && corElemType <= CorElementType.R8) ||
151 corElemType == CorElementType.I ||
152 corElemType == CorElementType.U;
155 internal static bool IsByRef(RuntimeType type)
157 CorElementType corElemType = GetCorElementType(type);
158 return (corElemType == CorElementType.ByRef);
161 internal static bool IsPointer(RuntimeType type)
163 CorElementType corElemType = GetCorElementType(type);
164 return (corElemType == CorElementType.Ptr);
167 internal static bool IsArray(RuntimeType type)
169 CorElementType corElemType = GetCorElementType(type);
170 return (corElemType == CorElementType.Array || corElemType == CorElementType.SzArray);
173 internal static bool IsSZArray(RuntimeType type)
175 CorElementType corElemType = GetCorElementType(type);
176 return (corElemType == CorElementType.SzArray);
179 internal static bool HasElementType(RuntimeType type)
181 CorElementType corElemType = GetCorElementType(type);
183 return ((corElemType == CorElementType.Array || corElemType == CorElementType.SzArray) // IsArray
184 || (corElemType == CorElementType.Ptr) // IsPointer
185 || (corElemType == CorElementType.ByRef)); // IsByRef
188 internal static IntPtr[] CopyRuntimeTypeHandles(RuntimeTypeHandle[] inHandles, out int length)
190 if (inHandles == null || inHandles.Length == 0)
196 IntPtr[] outHandles = new IntPtr[inHandles.Length];
197 for (int i = 0; i < inHandles.Length; i++)
199 outHandles[i] = inHandles[i].Value;
201 length = outHandles.Length;
205 internal static IntPtr[] CopyRuntimeTypeHandles(Type[] inHandles, out int length)
207 if (inHandles == null || inHandles.Length == 0)
213 IntPtr[] outHandles = new IntPtr[inHandles.Length];
214 for (int i = 0; i < inHandles.Length; i++)
216 outHandles[i] = inHandles[i].GetTypeHandleInternal().Value;
218 length = outHandles.Length;
222 [MethodImplAttribute(MethodImplOptions.InternalCall)]
223 internal static extern Object CreateInstance(RuntimeType type, bool publicOnly, ref bool canBeCached, ref RuntimeMethodHandleInternal ctor);
225 [MethodImplAttribute(MethodImplOptions.InternalCall)]
226 internal static extern Object CreateCaInstance(RuntimeType type, IRuntimeMethodInfo ctor);
228 [MethodImplAttribute(MethodImplOptions.InternalCall)]
229 internal static extern Object Allocate(RuntimeType type);
231 [MethodImplAttribute(MethodImplOptions.InternalCall)]
232 internal static extern Object CreateInstanceForAnotherGenericParameter(RuntimeType type, RuntimeType genericParameter);
234 internal RuntimeType GetRuntimeType()
239 [MethodImplAttribute(MethodImplOptions.InternalCall)]
240 internal extern static CorElementType GetCorElementType(RuntimeType type);
242 [MethodImplAttribute(MethodImplOptions.InternalCall)]
243 internal extern static RuntimeAssembly GetAssembly(RuntimeType type);
245 [MethodImplAttribute(MethodImplOptions.InternalCall)]
246 internal extern static RuntimeModule GetModule(RuntimeType type);
248 [CLSCompliant(false)]
249 public ModuleHandle GetModuleHandle()
251 return new ModuleHandle(RuntimeTypeHandle.GetModule(m_type));
254 [MethodImplAttribute(MethodImplOptions.InternalCall)]
255 internal extern static RuntimeType GetBaseType(RuntimeType type);
257 [MethodImplAttribute(MethodImplOptions.InternalCall)]
258 internal extern static TypeAttributes GetAttributes(RuntimeType type);
260 [MethodImplAttribute(MethodImplOptions.InternalCall)]
261 internal extern static RuntimeType GetElementType(RuntimeType type);
263 [MethodImplAttribute(MethodImplOptions.InternalCall)]
264 internal extern static bool CompareCanonicalHandles(RuntimeType left, RuntimeType right);
266 [MethodImplAttribute(MethodImplOptions.InternalCall)]
267 internal extern static int GetArrayRank(RuntimeType type);
269 [MethodImplAttribute(MethodImplOptions.InternalCall)]
270 internal extern static int GetToken(RuntimeType type);
272 [MethodImplAttribute(MethodImplOptions.InternalCall)]
273 internal extern static RuntimeMethodHandleInternal GetMethodAt(RuntimeType type, int slot);
275 // This is managed wrapper for MethodTable::IntroducedMethodIterator
276 internal struct IntroducedMethodEnumerator
278 private bool _firstCall;
279 private RuntimeMethodHandleInternal _handle;
281 internal IntroducedMethodEnumerator(RuntimeType type)
283 _handle = RuntimeTypeHandle.GetFirstIntroducedMethod(type);
287 public bool MoveNext()
293 else if (_handle.Value != IntPtr.Zero)
295 RuntimeTypeHandle.GetNextIntroducedMethod(ref _handle);
297 return !(_handle.Value == IntPtr.Zero);
300 public RuntimeMethodHandleInternal Current
308 // Glue to make this work nicely with C# foreach statement
309 public IntroducedMethodEnumerator GetEnumerator()
315 internal static IntroducedMethodEnumerator GetIntroducedMethods(RuntimeType type)
317 return new IntroducedMethodEnumerator(type);
320 [MethodImplAttribute(MethodImplOptions.InternalCall)]
321 private static extern RuntimeMethodHandleInternal GetFirstIntroducedMethod(RuntimeType type);
323 [MethodImplAttribute(MethodImplOptions.InternalCall)]
324 private static extern void GetNextIntroducedMethod(ref RuntimeMethodHandleInternal method);
326 [MethodImplAttribute(MethodImplOptions.InternalCall)]
327 internal extern static bool GetFields(RuntimeType type, IntPtr* result, int* count);
329 [MethodImplAttribute(MethodImplOptions.InternalCall)]
330 internal extern static Type[] GetInterfaces(RuntimeType type);
332 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
333 [SuppressUnmanagedCodeSecurity]
334 private extern static void GetConstraints(RuntimeTypeHandle handle, ObjectHandleOnStack types);
336 internal Type[] GetConstraints()
339 GetConstraints(GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref types));
344 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
345 [SuppressUnmanagedCodeSecurity]
346 private extern static IntPtr GetGCHandle(RuntimeTypeHandle handle, GCHandleType type);
348 internal IntPtr GetGCHandle(GCHandleType type)
350 return GetGCHandle(GetNativeHandle(), type);
353 [MethodImplAttribute(MethodImplOptions.InternalCall)]
354 internal extern static int GetNumVirtuals(RuntimeType type);
356 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
357 [SuppressUnmanagedCodeSecurity]
358 private extern static void VerifyInterfaceIsImplemented(RuntimeTypeHandle handle, RuntimeTypeHandle interfaceHandle);
360 internal void VerifyInterfaceIsImplemented(RuntimeTypeHandle interfaceHandle)
362 VerifyInterfaceIsImplemented(GetNativeHandle(), interfaceHandle.GetNativeHandle());
365 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
366 [SuppressUnmanagedCodeSecurity]
367 private extern static int GetInterfaceMethodImplementationSlot(RuntimeTypeHandle handle, RuntimeTypeHandle interfaceHandle, RuntimeMethodHandleInternal interfaceMethodHandle);
369 internal int GetInterfaceMethodImplementationSlot(RuntimeTypeHandle interfaceHandle, RuntimeMethodHandleInternal interfaceMethodHandle)
371 return GetInterfaceMethodImplementationSlot(GetNativeHandle(), interfaceHandle.GetNativeHandle(), interfaceMethodHandle);
374 [MethodImplAttribute(MethodImplOptions.InternalCall)]
375 internal extern static bool IsComObject(RuntimeType type, bool isGenericCOM);
377 [MethodImplAttribute(MethodImplOptions.InternalCall)]
378 internal extern static bool IsInterface(RuntimeType type);
380 [MethodImplAttribute(MethodImplOptions.InternalCall)]
381 internal extern static bool IsByRefLike(RuntimeType type);
383 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
384 [SuppressUnmanagedCodeSecurity]
385 [return: MarshalAs(UnmanagedType.Bool)]
386 private extern static bool _IsVisible(RuntimeTypeHandle typeHandle);
388 internal static bool IsVisible(RuntimeType type)
390 return _IsVisible(new RuntimeTypeHandle(type));
393 [MethodImplAttribute(MethodImplOptions.InternalCall)]
394 internal extern static bool IsValueType(RuntimeType type);
396 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
397 [SuppressUnmanagedCodeSecurity]
398 private extern static void ConstructName(RuntimeTypeHandle handle, TypeNameFormatFlags formatFlags, StringHandleOnStack retString);
400 internal string ConstructName(TypeNameFormatFlags formatFlags)
403 ConstructName(GetNativeHandle(), formatFlags, JitHelpers.GetStringHandleOnStack(ref name));
407 [MethodImplAttribute(MethodImplOptions.InternalCall)]
408 private extern static void* _GetUtf8Name(RuntimeType type);
410 internal static Utf8String GetUtf8Name(RuntimeType type)
412 return new Utf8String(_GetUtf8Name(type));
415 [MethodImplAttribute(MethodImplOptions.InternalCall)]
416 internal extern static bool CanCastTo(RuntimeType type, RuntimeType target);
418 [MethodImplAttribute(MethodImplOptions.InternalCall)]
419 internal extern static RuntimeType GetDeclaringType(RuntimeType type);
421 [MethodImplAttribute(MethodImplOptions.InternalCall)]
422 internal extern static IRuntimeMethodInfo GetDeclaringMethod(RuntimeType type);
424 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
425 [SuppressUnmanagedCodeSecurity]
426 private extern static void GetDefaultConstructor(RuntimeTypeHandle handle, ObjectHandleOnStack method);
428 internal IRuntimeMethodInfo GetDefaultConstructor()
430 IRuntimeMethodInfo ctor = null;
431 GetDefaultConstructor(GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref ctor));
435 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
436 [SuppressUnmanagedCodeSecurity]
437 private extern static void GetTypeByName(string name, bool throwOnError, bool ignoreCase, bool reflectionOnly, StackCrawlMarkHandle stackMark,
438 IntPtr pPrivHostBinder,
439 bool loadTypeFromPartialName, ObjectHandleOnStack type, ObjectHandleOnStack keepalive);
441 // Wrapper function to reduce the need for ifdefs.
442 internal static RuntimeType GetTypeByName(string name, bool throwOnError, bool ignoreCase, bool reflectionOnly, ref StackCrawlMark stackMark, bool loadTypeFromPartialName)
444 return GetTypeByName(name, throwOnError, ignoreCase, reflectionOnly, ref stackMark, IntPtr.Zero, loadTypeFromPartialName);
447 internal static RuntimeType GetTypeByName(string name, bool throwOnError, bool ignoreCase, bool reflectionOnly, ref StackCrawlMark stackMark,
448 IntPtr pPrivHostBinder,
449 bool loadTypeFromPartialName)
451 if (name == null || name.Length == 0)
454 throw new TypeLoadException(SR.Arg_TypeLoadNullStr);
459 RuntimeType type = null;
461 Object keepAlive = null;
462 GetTypeByName(name, throwOnError, ignoreCase, reflectionOnly,
463 JitHelpers.GetStackCrawlMarkHandle(ref stackMark),
465 loadTypeFromPartialName, JitHelpers.GetObjectHandleOnStack(ref type), JitHelpers.GetObjectHandleOnStack(ref keepAlive));
466 GC.KeepAlive(keepAlive);
471 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
472 [SuppressUnmanagedCodeSecurity]
473 private extern static void GetTypeByNameUsingCARules(string name, RuntimeModule scope, ObjectHandleOnStack type);
475 internal static RuntimeType GetTypeByNameUsingCARules(string name, RuntimeModule scope)
477 if (name == null || name.Length == 0)
478 throw new ArgumentException(null, nameof(name));
479 Contract.EndContractBlock();
481 RuntimeType type = null;
482 GetTypeByNameUsingCARules(name, scope.GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref type));
487 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
488 [SuppressUnmanagedCodeSecurity]
489 internal extern static void GetInstantiation(RuntimeTypeHandle type, ObjectHandleOnStack types, bool fAsRuntimeTypeArray);
491 internal RuntimeType[] GetInstantiationInternal()
493 RuntimeType[] types = null;
494 GetInstantiation(GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref types), true);
498 internal Type[] GetInstantiationPublic()
501 GetInstantiation(GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref types), false);
505 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
506 [SuppressUnmanagedCodeSecurity]
507 private extern static void Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, int numGenericArgs, ObjectHandleOnStack type);
509 internal RuntimeType Instantiate(Type[] inst)
511 // defensive copy to be sure array is not mutated from the outside during processing
513 IntPtr[] instHandles = CopyRuntimeTypeHandles(inst, out instCount);
515 fixed (IntPtr* pInst = instHandles)
517 RuntimeType type = null;
518 Instantiate(GetNativeHandle(), pInst, instCount, JitHelpers.GetObjectHandleOnStack(ref type));
524 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
525 [SuppressUnmanagedCodeSecurity]
526 private extern static void MakeArray(RuntimeTypeHandle handle, int rank, ObjectHandleOnStack type);
528 internal RuntimeType MakeArray(int rank)
530 RuntimeType type = null;
531 MakeArray(GetNativeHandle(), rank, JitHelpers.GetObjectHandleOnStack(ref type));
535 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
536 [SuppressUnmanagedCodeSecurity]
537 private extern static void MakeSZArray(RuntimeTypeHandle handle, ObjectHandleOnStack type);
539 internal RuntimeType MakeSZArray()
541 RuntimeType type = null;
542 MakeSZArray(GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref type));
546 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
547 [SuppressUnmanagedCodeSecurity]
548 private extern static void MakeByRef(RuntimeTypeHandle handle, ObjectHandleOnStack type);
550 internal RuntimeType MakeByRef()
552 RuntimeType type = null;
553 MakeByRef(GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref type));
557 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
558 [SuppressUnmanagedCodeSecurity]
559 private extern static void MakePointer(RuntimeTypeHandle handle, ObjectHandleOnStack type);
561 internal RuntimeType MakePointer()
563 RuntimeType type = null;
564 MakePointer(GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref type));
568 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
569 [SuppressUnmanagedCodeSecurity]
570 internal extern static bool IsCollectible(RuntimeTypeHandle handle);
572 [MethodImplAttribute(MethodImplOptions.InternalCall)]
573 internal extern static bool HasInstantiation(RuntimeType type);
575 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
576 [SuppressUnmanagedCodeSecurity]
577 private extern static void GetGenericTypeDefinition(RuntimeTypeHandle type, ObjectHandleOnStack retType);
579 internal static RuntimeType GetGenericTypeDefinition(RuntimeType type)
581 RuntimeType retType = type;
583 if (HasInstantiation(retType) && !IsGenericTypeDefinition(retType))
584 GetGenericTypeDefinition(retType.GetTypeHandleInternal(), JitHelpers.GetObjectHandleOnStack(ref retType));
589 [MethodImplAttribute(MethodImplOptions.InternalCall)]
590 internal extern static bool IsGenericTypeDefinition(RuntimeType type);
592 [MethodImplAttribute(MethodImplOptions.InternalCall)]
593 internal extern static bool IsGenericVariable(RuntimeType type);
595 [MethodImplAttribute(MethodImplOptions.InternalCall)]
596 private extern static int GetGenericVariableIndex(RuntimeType type);
598 internal int GetGenericVariableIndex()
600 RuntimeType type = GetTypeChecked();
602 if (!IsGenericVariable(type))
603 throw new InvalidOperationException(SR.Arg_NotGenericParameter);
605 return GetGenericVariableIndex(type);
608 [MethodImplAttribute(MethodImplOptions.InternalCall)]
609 internal extern static bool ContainsGenericVariables(RuntimeType handle);
611 internal bool ContainsGenericVariables()
613 return ContainsGenericVariables(GetTypeChecked());
616 [MethodImplAttribute(MethodImplOptions.InternalCall)]
617 private extern static bool SatisfiesConstraints(RuntimeType paramType, IntPtr* pTypeContext, int typeContextLength, IntPtr* pMethodContext, int methodContextLength, RuntimeType toType);
619 internal static bool SatisfiesConstraints(RuntimeType paramType, RuntimeType[] typeContext, RuntimeType[] methodContext, RuntimeType toType)
621 int typeContextLength;
622 int methodContextLength;
623 IntPtr[] typeContextHandles = CopyRuntimeTypeHandles(typeContext, out typeContextLength);
624 IntPtr[] methodContextHandles = CopyRuntimeTypeHandles(methodContext, out methodContextLength);
626 fixed (IntPtr* pTypeContextHandles = typeContextHandles, pMethodContextHandles = methodContextHandles)
628 bool result = SatisfiesConstraints(paramType, pTypeContextHandles, typeContextLength, pMethodContextHandles, methodContextLength, toType);
630 GC.KeepAlive(typeContext);
631 GC.KeepAlive(methodContext);
637 [MethodImplAttribute(MethodImplOptions.InternalCall)]
638 private extern static IntPtr _GetMetadataImport(RuntimeType type);
640 internal static MetadataImport GetMetadataImport(RuntimeType type)
642 return new MetadataImport(_GetMetadataImport(type), type);
645 public void GetObjectData(SerializationInfo info, StreamingContext context)
647 throw new PlatformNotSupportedException();
651 // This type is used to remove the expense of having a managed reference object that is dynamically
652 // created when we can prove that we don't need that object. Use of this type requires code to ensure
653 // that the underlying native resource is not freed.
654 // Cases in which this may be used:
655 // 1. When native code calls managed code passing one of these as a parameter
656 // 2. When managed code acquires one of these from an IRuntimeMethodInfo, and ensure that the IRuntimeMethodInfo is preserved
657 // across the lifetime of the RuntimeMethodHandleInternal instance
658 // 3. When another object is used to keep the RuntimeMethodHandleInternal alive. See delegates, CreateInstance cache, Signature structure
659 // When in doubt, do not use.
660 internal struct RuntimeMethodHandleInternal
662 internal static RuntimeMethodHandleInternal EmptyHandle
666 return new RuntimeMethodHandleInternal();
670 internal bool IsNullHandle()
672 return m_handle.IsNull();
675 internal IntPtr Value
683 internal RuntimeMethodHandleInternal(IntPtr value)
688 internal IntPtr m_handle;
691 internal class RuntimeMethodInfoStub : IRuntimeMethodInfo
693 public RuntimeMethodInfoStub(RuntimeMethodHandleInternal methodHandleValue, object keepalive)
695 m_keepalive = keepalive;
696 m_value = methodHandleValue;
699 public RuntimeMethodInfoStub(IntPtr methodHandleValue, object keepalive)
701 m_keepalive = keepalive;
702 m_value = new RuntimeMethodHandleInternal(methodHandleValue);
705 private object m_keepalive;
707 // These unused variables are used to ensure that this class has the same layout as RuntimeMethodInfo
708 #pragma warning disable 169
716 #pragma warning restore 169
718 public RuntimeMethodHandleInternal m_value;
720 RuntimeMethodHandleInternal IRuntimeMethodInfo.Value
729 internal interface IRuntimeMethodInfo
731 RuntimeMethodHandleInternal Value
737 public unsafe struct RuntimeMethodHandle : ISerializable
739 // Returns handle for interop with EE. The handle is guaranteed to be non-null.
740 internal static IRuntimeMethodInfo EnsureNonNullMethodInfo(IRuntimeMethodInfo method)
743 throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
747 private IRuntimeMethodInfo m_value;
749 internal RuntimeMethodHandle(IRuntimeMethodInfo method)
754 internal IRuntimeMethodInfo GetMethodInfo()
760 private static IntPtr GetValueInternal(RuntimeMethodHandle rmh)
765 // ISerializable interface
766 public void GetObjectData(SerializationInfo info, StreamingContext context)
768 throw new PlatformNotSupportedException();
775 return m_value != null ? m_value.Value.Value : IntPtr.Zero;
779 public override int GetHashCode()
781 return ValueType.GetHashCodeOfPtr(Value);
784 public override bool Equals(object obj)
786 if (!(obj is RuntimeMethodHandle))
789 RuntimeMethodHandle handle = (RuntimeMethodHandle)obj;
791 return handle.Value == Value;
794 public static bool operator ==(RuntimeMethodHandle left, RuntimeMethodHandle right)
796 return left.Equals(right);
799 public static bool operator !=(RuntimeMethodHandle left, RuntimeMethodHandle right)
801 return !left.Equals(right);
804 public bool Equals(RuntimeMethodHandle handle)
806 return handle.Value == Value;
810 internal bool IsNullHandle()
812 return m_value == null;
815 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
816 [SuppressUnmanagedCodeSecurity]
817 internal extern static IntPtr GetFunctionPointer(RuntimeMethodHandleInternal handle);
819 public IntPtr GetFunctionPointer()
821 IntPtr ptr = GetFunctionPointer(EnsureNonNullMethodInfo(m_value).Value);
822 GC.KeepAlive(m_value);
826 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
827 [SuppressUnmanagedCodeSecurity]
828 internal extern static bool IsCAVisibleFromDecoratedType(
829 RuntimeTypeHandle attrTypeHandle,
830 IRuntimeMethodInfo attrCtor,
831 RuntimeTypeHandle sourceTypeHandle,
832 RuntimeModule sourceModule);
834 [MethodImplAttribute(MethodImplOptions.InternalCall)]
835 private static extern IRuntimeMethodInfo _GetCurrentMethod(ref StackCrawlMark stackMark);
836 internal static IRuntimeMethodInfo GetCurrentMethod(ref StackCrawlMark stackMark)
838 return _GetCurrentMethod(ref stackMark);
842 [MethodImplAttribute(MethodImplOptions.InternalCall)]
843 internal static extern MethodAttributes GetAttributes(RuntimeMethodHandleInternal method);
845 internal static MethodAttributes GetAttributes(IRuntimeMethodInfo method)
847 MethodAttributes retVal = RuntimeMethodHandle.GetAttributes(method.Value);
848 GC.KeepAlive(method);
852 [MethodImplAttribute(MethodImplOptions.InternalCall)]
853 internal static extern MethodImplAttributes GetImplAttributes(IRuntimeMethodInfo method);
855 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
856 [SuppressUnmanagedCodeSecurity]
857 private extern static void ConstructInstantiation(IRuntimeMethodInfo method, TypeNameFormatFlags format, StringHandleOnStack retString);
859 internal static string ConstructInstantiation(IRuntimeMethodInfo method, TypeNameFormatFlags format)
862 ConstructInstantiation(EnsureNonNullMethodInfo(method), format, JitHelpers.GetStringHandleOnStack(ref name));
866 [MethodImplAttribute(MethodImplOptions.InternalCall)]
867 internal extern static RuntimeType GetDeclaringType(RuntimeMethodHandleInternal method);
869 internal static RuntimeType GetDeclaringType(IRuntimeMethodInfo method)
871 RuntimeType type = RuntimeMethodHandle.GetDeclaringType(method.Value);
872 GC.KeepAlive(method);
876 [MethodImplAttribute(MethodImplOptions.InternalCall)]
877 internal extern static int GetSlot(RuntimeMethodHandleInternal method);
879 internal static int GetSlot(IRuntimeMethodInfo method)
881 Contract.Requires(method != null);
883 int slot = RuntimeMethodHandle.GetSlot(method.Value);
884 GC.KeepAlive(method);
888 [MethodImplAttribute(MethodImplOptions.InternalCall)]
889 internal extern static int GetMethodDef(IRuntimeMethodInfo method);
891 [MethodImplAttribute(MethodImplOptions.InternalCall)]
892 internal extern static string GetName(RuntimeMethodHandleInternal method);
894 internal static string GetName(IRuntimeMethodInfo method)
896 string name = RuntimeMethodHandle.GetName(method.Value);
897 GC.KeepAlive(method);
901 [MethodImplAttribute(MethodImplOptions.InternalCall)]
902 private extern static void* _GetUtf8Name(RuntimeMethodHandleInternal method);
904 internal static Utf8String GetUtf8Name(RuntimeMethodHandleInternal method)
906 return new Utf8String(_GetUtf8Name(method));
909 [MethodImplAttribute(MethodImplOptions.InternalCall)]
910 internal static extern bool MatchesNameHash(RuntimeMethodHandleInternal method, uint hash);
912 [DebuggerStepThroughAttribute]
913 [Diagnostics.DebuggerHidden]
914 [MethodImplAttribute(MethodImplOptions.InternalCall)]
915 internal extern static object InvokeMethod(object target, object[] arguments, Signature sig, bool constructor);
917 #region Private Invocation Helpers
918 internal static INVOCATION_FLAGS GetSecurityFlags(IRuntimeMethodInfo handle)
920 return (INVOCATION_FLAGS)RuntimeMethodHandle.GetSpecialSecurityFlags(handle);
923 [MethodImplAttribute(MethodImplOptions.InternalCall)]
924 static extern internal uint GetSpecialSecurityFlags(IRuntimeMethodInfo method);
928 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
929 [SuppressUnmanagedCodeSecurity]
930 private extern static void GetMethodInstantiation(RuntimeMethodHandleInternal method, ObjectHandleOnStack types, bool fAsRuntimeTypeArray);
932 internal static RuntimeType[] GetMethodInstantiationInternal(IRuntimeMethodInfo method)
934 RuntimeType[] types = null;
935 GetMethodInstantiation(EnsureNonNullMethodInfo(method).Value, JitHelpers.GetObjectHandleOnStack(ref types), true);
936 GC.KeepAlive(method);
940 internal static RuntimeType[] GetMethodInstantiationInternal(RuntimeMethodHandleInternal method)
942 RuntimeType[] types = null;
943 GetMethodInstantiation(method, JitHelpers.GetObjectHandleOnStack(ref types), true);
947 internal static Type[] GetMethodInstantiationPublic(IRuntimeMethodInfo method)
949 RuntimeType[] types = null;
950 GetMethodInstantiation(EnsureNonNullMethodInfo(method).Value, JitHelpers.GetObjectHandleOnStack(ref types), false);
951 GC.KeepAlive(method);
955 [MethodImplAttribute(MethodImplOptions.InternalCall)]
956 internal extern static bool HasMethodInstantiation(RuntimeMethodHandleInternal method);
958 internal static bool HasMethodInstantiation(IRuntimeMethodInfo method)
960 bool fRet = RuntimeMethodHandle.HasMethodInstantiation(method.Value);
961 GC.KeepAlive(method);
965 [MethodImplAttribute(MethodImplOptions.InternalCall)]
966 internal extern static RuntimeMethodHandleInternal GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation);
968 [MethodImplAttribute(MethodImplOptions.InternalCall)]
969 internal extern static RuntimeMethodHandleInternal GetMethodFromCanonical(RuntimeMethodHandleInternal method, RuntimeType declaringType);
971 [MethodImplAttribute(MethodImplOptions.InternalCall)]
972 internal extern static bool IsGenericMethodDefinition(RuntimeMethodHandleInternal method);
974 internal static bool IsGenericMethodDefinition(IRuntimeMethodInfo method)
976 bool fRet = RuntimeMethodHandle.IsGenericMethodDefinition(method.Value);
977 GC.KeepAlive(method);
982 [MethodImplAttribute(MethodImplOptions.InternalCall)]
983 internal extern static bool IsTypicalMethodDefinition(IRuntimeMethodInfo method);
985 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
986 [SuppressUnmanagedCodeSecurity]
987 private extern static void GetTypicalMethodDefinition(IRuntimeMethodInfo method, ObjectHandleOnStack outMethod);
989 internal static IRuntimeMethodInfo GetTypicalMethodDefinition(IRuntimeMethodInfo method)
991 if (!IsTypicalMethodDefinition(method))
992 GetTypicalMethodDefinition(method, JitHelpers.GetObjectHandleOnStack(ref method));
997 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
998 [SuppressUnmanagedCodeSecurity]
999 private extern static void StripMethodInstantiation(IRuntimeMethodInfo method, ObjectHandleOnStack outMethod);
1001 internal static IRuntimeMethodInfo StripMethodInstantiation(IRuntimeMethodInfo method)
1003 IRuntimeMethodInfo strippedMethod = method;
1005 StripMethodInstantiation(method, JitHelpers.GetObjectHandleOnStack(ref strippedMethod));
1007 return strippedMethod;
1010 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1011 internal extern static bool IsDynamicMethod(RuntimeMethodHandleInternal method);
1013 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1014 [SuppressUnmanagedCodeSecurity]
1015 internal extern static void Destroy(RuntimeMethodHandleInternal method);
1017 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1018 internal extern static Resolver GetResolver(RuntimeMethodHandleInternal method);
1020 [MethodImpl(MethodImplOptions.InternalCall)]
1021 internal extern static MethodBody GetMethodBody(IRuntimeMethodInfo method, RuntimeType declaringType);
1023 [MethodImpl(MethodImplOptions.InternalCall)]
1024 internal extern static bool IsConstructor(RuntimeMethodHandleInternal method);
1026 [MethodImpl(MethodImplOptions.InternalCall)]
1027 internal extern static LoaderAllocator GetLoaderAllocator(RuntimeMethodHandleInternal method);
1030 // This type is used to remove the expense of having a managed reference object that is dynamically
1031 // created when we can prove that we don't need that object. Use of this type requires code to ensure
1032 // that the underlying native resource is not freed.
1033 // Cases in which this may be used:
1034 // 1. When native code calls managed code passing one of these as a parameter
1035 // 2. When managed code acquires one of these from an RtFieldInfo, and ensure that the RtFieldInfo is preserved
1036 // across the lifetime of the RuntimeFieldHandleInternal instance
1037 // 3. When another object is used to keep the RuntimeFieldHandleInternal alive.
1038 // When in doubt, do not use.
1039 internal struct RuntimeFieldHandleInternal
1041 internal bool IsNullHandle()
1043 return m_handle.IsNull();
1046 internal IntPtr Value
1054 internal RuntimeFieldHandleInternal(IntPtr value)
1059 internal IntPtr m_handle;
1062 internal interface IRuntimeFieldInfo
1064 RuntimeFieldHandleInternal Value
1070 [StructLayout(LayoutKind.Sequential)]
1071 internal class RuntimeFieldInfoStub : IRuntimeFieldInfo
1073 // These unused variables are used to ensure that this class has the same layout as RuntimeFieldInfo
1074 #pragma warning disable 169
1075 private object m_keepalive;
1080 private RuntimeFieldHandleInternal m_fieldHandle;
1081 #pragma warning restore 169
1083 RuntimeFieldHandleInternal IRuntimeFieldInfo.Value
1087 return m_fieldHandle;
1092 public unsafe struct RuntimeFieldHandle : ISerializable
1094 // Returns handle for interop with EE. The handle is guaranteed to be non-null.
1095 internal RuntimeFieldHandle GetNativeHandle()
1097 // Create local copy to avoid a race condition
1098 IRuntimeFieldInfo field = m_ptr;
1100 throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
1101 return new RuntimeFieldHandle(field);
1104 private IRuntimeFieldInfo m_ptr;
1106 internal RuntimeFieldHandle(IRuntimeFieldInfo fieldInfo)
1111 internal IRuntimeFieldInfo GetRuntimeFieldInfo()
1120 return m_ptr != null ? m_ptr.Value.Value : IntPtr.Zero;
1124 internal bool IsNullHandle()
1126 return m_ptr == null;
1129 public override int GetHashCode()
1131 return ValueType.GetHashCodeOfPtr(Value);
1134 public override bool Equals(object obj)
1136 if (!(obj is RuntimeFieldHandle))
1139 RuntimeFieldHandle handle = (RuntimeFieldHandle)obj;
1141 return handle.Value == Value;
1144 public unsafe bool Equals(RuntimeFieldHandle handle)
1146 return handle.Value == Value;
1149 public static bool operator ==(RuntimeFieldHandle left, RuntimeFieldHandle right)
1151 return left.Equals(right);
1154 public static bool operator !=(RuntimeFieldHandle left, RuntimeFieldHandle right)
1156 return !left.Equals(right);
1159 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1160 internal static extern String GetName(RtFieldInfo field);
1162 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1163 private static extern unsafe void* _GetUtf8Name(RuntimeFieldHandleInternal field);
1165 internal static unsafe Utf8String GetUtf8Name(RuntimeFieldHandleInternal field) { return new Utf8String(_GetUtf8Name(field)); }
1167 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1168 internal static extern bool MatchesNameHash(RuntimeFieldHandleInternal handle, uint hash);
1170 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1171 internal static extern FieldAttributes GetAttributes(RuntimeFieldHandleInternal field);
1173 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1174 internal static extern RuntimeType GetApproxDeclaringType(RuntimeFieldHandleInternal field);
1176 internal static RuntimeType GetApproxDeclaringType(IRuntimeFieldInfo field)
1178 RuntimeType type = GetApproxDeclaringType(field.Value);
1179 GC.KeepAlive(field);
1183 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1184 internal static extern int GetToken(RtFieldInfo field);
1186 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1187 internal static extern Object GetValue(RtFieldInfo field, Object instance, RuntimeType fieldType, RuntimeType declaringType, ref bool domainInitialized);
1189 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1190 internal static extern Object GetValueDirect(RtFieldInfo field, RuntimeType fieldType, void* pTypedRef, RuntimeType contextType);
1192 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1193 internal static extern void SetValue(RtFieldInfo field, Object obj, Object value, RuntimeType fieldType, FieldAttributes fieldAttr, RuntimeType declaringType, ref bool domainInitialized);
1195 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1196 internal static extern void SetValueDirect(RtFieldInfo field, RuntimeType fieldType, void* pTypedRef, Object value, RuntimeType contextType);
1198 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1199 internal static extern RuntimeFieldHandleInternal GetStaticFieldForGenericType(RuntimeFieldHandleInternal field, RuntimeType declaringType);
1201 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1202 internal static extern bool AcquiresContextFromThis(RuntimeFieldHandleInternal field);
1204 // ISerializable interface
1205 public void GetObjectData(SerializationInfo info, StreamingContext context)
1207 throw new PlatformNotSupportedException();
1211 public unsafe struct ModuleHandle
1213 // Returns handle for interop with EE. The handle is guaranteed to be non-null.
1214 #region Public Static Members
1215 public static readonly ModuleHandle EmptyHandle = GetEmptyMH();
1218 unsafe static private ModuleHandle GetEmptyMH()
1220 return new ModuleHandle();
1223 #region Private Data Members
1224 private RuntimeModule m_ptr;
1228 internal ModuleHandle(RuntimeModule module)
1234 #region Internal FCalls
1236 internal RuntimeModule GetRuntimeModule()
1241 public override int GetHashCode()
1243 return m_ptr != null ? m_ptr.GetHashCode() : 0;
1246 public override bool Equals(object obj)
1248 if (!(obj is ModuleHandle))
1251 ModuleHandle handle = (ModuleHandle)obj;
1253 return handle.m_ptr == m_ptr;
1256 public unsafe bool Equals(ModuleHandle handle)
1258 return handle.m_ptr == m_ptr;
1261 public static bool operator ==(ModuleHandle left, ModuleHandle right)
1263 return left.Equals(right);
1266 public static bool operator !=(ModuleHandle left, ModuleHandle right)
1268 return !left.Equals(right);
1271 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1272 internal static extern IRuntimeMethodInfo GetDynamicMethod(DynamicMethod method, RuntimeModule module, string name, byte[] sig, Resolver resolver);
1274 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1275 internal static extern int GetToken(RuntimeModule module);
1277 private static void ValidateModulePointer(RuntimeModule module)
1279 // Make sure we have a valid Module to resolve against.
1281 throw new InvalidOperationException(SR.InvalidOperation_NullModuleHandle);
1284 // SQL-CLR LKG9 Compiler dependency
1285 public RuntimeTypeHandle GetRuntimeTypeHandleFromMetadataToken(int typeToken) { return ResolveTypeHandle(typeToken); }
1286 public RuntimeTypeHandle ResolveTypeHandle(int typeToken)
1288 return new RuntimeTypeHandle(ResolveTypeHandleInternal(GetRuntimeModule(), typeToken, null, null));
1290 public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
1292 return new RuntimeTypeHandle(ModuleHandle.ResolveTypeHandleInternal(GetRuntimeModule(), typeToken, typeInstantiationContext, methodInstantiationContext));
1295 internal static RuntimeType ResolveTypeHandleInternal(RuntimeModule module, int typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
1297 ValidateModulePointer(module);
1298 if (!ModuleHandle.GetMetadataImport(module).IsValidToken(typeToken))
1299 throw new ArgumentOutOfRangeException(nameof(typeToken),
1300 SR.Format(SR.Argument_InvalidToken, typeToken, new ModuleHandle(module)));
1302 int typeInstCount, methodInstCount;
1303 IntPtr[] typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount);
1304 IntPtr[] methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount);
1306 fixed (IntPtr* typeInstArgs = typeInstantiationContextHandles, methodInstArgs = methodInstantiationContextHandles)
1308 RuntimeType type = null;
1309 ResolveType(module, typeToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount, JitHelpers.GetObjectHandleOnStack(ref type));
1310 GC.KeepAlive(typeInstantiationContext);
1311 GC.KeepAlive(methodInstantiationContext);
1316 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1317 [SuppressUnmanagedCodeSecurity]
1318 private extern static void ResolveType(RuntimeModule module,
1320 IntPtr* typeInstArgs,
1322 IntPtr* methodInstArgs,
1323 int methodInstCount,
1324 ObjectHandleOnStack type);
1326 // SQL-CLR LKG9 Compiler dependency
1327 public RuntimeMethodHandle GetRuntimeMethodHandleFromMetadataToken(int methodToken) { return ResolveMethodHandle(methodToken); }
1328 public RuntimeMethodHandle ResolveMethodHandle(int methodToken) { return ResolveMethodHandle(methodToken, null, null); }
1329 internal static IRuntimeMethodInfo ResolveMethodHandleInternal(RuntimeModule module, int methodToken) { return ModuleHandle.ResolveMethodHandleInternal(module, methodToken, null, null); }
1330 public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
1332 return new RuntimeMethodHandle(ResolveMethodHandleInternal(GetRuntimeModule(), methodToken, typeInstantiationContext, methodInstantiationContext));
1335 internal static IRuntimeMethodInfo ResolveMethodHandleInternal(RuntimeModule module, int methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
1337 int typeInstCount, methodInstCount;
1339 IntPtr[] typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount);
1340 IntPtr[] methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount);
1342 RuntimeMethodHandleInternal handle = ResolveMethodHandleInternalCore(module, methodToken, typeInstantiationContextHandles, typeInstCount, methodInstantiationContextHandles, methodInstCount);
1343 IRuntimeMethodInfo retVal = new RuntimeMethodInfoStub(handle, RuntimeMethodHandle.GetLoaderAllocator(handle));
1344 GC.KeepAlive(typeInstantiationContext);
1345 GC.KeepAlive(methodInstantiationContext);
1349 internal static RuntimeMethodHandleInternal ResolveMethodHandleInternalCore(RuntimeModule module, int methodToken, IntPtr[] typeInstantiationContext, int typeInstCount, IntPtr[] methodInstantiationContext, int methodInstCount)
1351 ValidateModulePointer(module);
1352 if (!ModuleHandle.GetMetadataImport(module.GetNativeHandle()).IsValidToken(methodToken))
1353 throw new ArgumentOutOfRangeException(nameof(methodToken),
1354 SR.Format(SR.Argument_InvalidToken, methodToken, new ModuleHandle(module)));
1356 fixed (IntPtr* typeInstArgs = typeInstantiationContext, methodInstArgs = methodInstantiationContext)
1358 return ResolveMethod(module.GetNativeHandle(), methodToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount);
1362 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1363 [SuppressUnmanagedCodeSecurity]
1364 private extern static RuntimeMethodHandleInternal ResolveMethod(RuntimeModule module,
1366 IntPtr* typeInstArgs,
1368 IntPtr* methodInstArgs,
1369 int methodInstCount);
1371 // SQL-CLR LKG9 Compiler dependency
1372 public RuntimeFieldHandle GetRuntimeFieldHandleFromMetadataToken(int fieldToken) { return ResolveFieldHandle(fieldToken); }
1373 public RuntimeFieldHandle ResolveFieldHandle(int fieldToken) { return new RuntimeFieldHandle(ResolveFieldHandleInternal(GetRuntimeModule(), fieldToken, null, null)); }
1374 public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
1375 { return new RuntimeFieldHandle(ResolveFieldHandleInternal(GetRuntimeModule(), fieldToken, typeInstantiationContext, methodInstantiationContext)); }
1377 internal static IRuntimeFieldInfo ResolveFieldHandleInternal(RuntimeModule module, int fieldToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
1379 ValidateModulePointer(module);
1380 if (!ModuleHandle.GetMetadataImport(module.GetNativeHandle()).IsValidToken(fieldToken))
1381 throw new ArgumentOutOfRangeException(nameof(fieldToken),
1382 SR.Format(SR.Argument_InvalidToken, fieldToken, new ModuleHandle(module)));
1384 // defensive copy to be sure array is not mutated from the outside during processing
1385 int typeInstCount, methodInstCount;
1386 IntPtr[] typeInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(typeInstantiationContext, out typeInstCount);
1387 IntPtr[] methodInstantiationContextHandles = RuntimeTypeHandle.CopyRuntimeTypeHandles(methodInstantiationContext, out methodInstCount);
1389 fixed (IntPtr* typeInstArgs = typeInstantiationContextHandles, methodInstArgs = methodInstantiationContextHandles)
1391 IRuntimeFieldInfo field = null;
1392 ResolveField(module.GetNativeHandle(), fieldToken, typeInstArgs, typeInstCount, methodInstArgs, methodInstCount, JitHelpers.GetObjectHandleOnStack(ref field));
1393 GC.KeepAlive(typeInstantiationContext);
1394 GC.KeepAlive(methodInstantiationContext);
1399 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1400 [SuppressUnmanagedCodeSecurity]
1401 private extern static void ResolveField(RuntimeModule module,
1403 IntPtr* typeInstArgs,
1405 IntPtr* methodInstArgs,
1406 int methodInstCount,
1407 ObjectHandleOnStack retField);
1409 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1410 [SuppressUnmanagedCodeSecurity]
1411 private extern static bool _ContainsPropertyMatchingHash(RuntimeModule module, int propertyToken, uint hash);
1413 internal static bool ContainsPropertyMatchingHash(RuntimeModule module, int propertyToken, uint hash)
1415 return _ContainsPropertyMatchingHash(module.GetNativeHandle(), propertyToken, hash);
1418 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1419 [SuppressUnmanagedCodeSecurity]
1420 internal extern static void GetModuleType(RuntimeModule handle, ObjectHandleOnStack type);
1422 internal static RuntimeType GetModuleType(RuntimeModule module)
1424 RuntimeType type = null;
1425 GetModuleType(module.GetNativeHandle(), JitHelpers.GetObjectHandleOnStack(ref type));
1429 [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
1430 [SuppressUnmanagedCodeSecurity]
1431 private extern static void GetPEKind(RuntimeModule handle, out int peKind, out int machine);
1433 // making this internal, used by Module.GetPEKind
1434 internal static void GetPEKind(RuntimeModule module, out PortableExecutableKinds peKind, out ImageFileMachine machine)
1436 int lKind, lMachine;
1437 GetPEKind(module.GetNativeHandle(), out lKind, out lMachine);
1438 peKind = (PortableExecutableKinds)lKind;
1439 machine = (ImageFileMachine)lMachine;
1442 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1443 internal extern static int GetMDStreamVersion(RuntimeModule module);
1445 public int MDStreamVersion
1447 get { return GetMDStreamVersion(GetRuntimeModule().GetNativeHandle()); }
1450 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1451 private extern static IntPtr _GetMetadataImport(RuntimeModule module);
1453 internal static MetadataImport GetMetadataImport(RuntimeModule module)
1455 return new MetadataImport(_GetMetadataImport(module.GetNativeHandle()), module);
1460 internal unsafe class Signature
1463 internal enum MdSigCallingConvention : byte
1467 ExplicitThis = 0x40,
1468 CallConvMask = 0x0F,
1485 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1486 private extern void GetSignature(
1487 void* pCorSig, int cCorSig,
1488 RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType);
1492 #region Private Data Members
1494 // Keep the layout in sync with SignatureNative in the VM
1496 internal RuntimeType[] m_arguments;
1497 internal RuntimeType m_declaringType;
1498 internal RuntimeType m_returnTypeORfieldType;
1499 internal object m_keepalive;
1500 internal void* m_sig;
1501 internal int m_managedCallingConventionAndArgIteratorFlags; // lowest byte is CallingConvention, upper 3 bytes are ArgIterator flags
1502 internal int m_nSizeOfArgStack;
1503 internal int m_csig;
1504 internal RuntimeMethodHandleInternal m_pMethod;
1507 #region Constructors
1509 IRuntimeMethodInfo method,
1510 RuntimeType[] arguments,
1511 RuntimeType returnType,
1512 CallingConventions callingConvention)
1514 m_pMethod = method.Value;
1515 m_arguments = arguments;
1516 m_returnTypeORfieldType = returnType;
1517 m_managedCallingConventionAndArgIteratorFlags = (byte)callingConvention;
1519 GetSignature(null, 0, new RuntimeFieldHandleInternal(), method, null);
1522 public Signature(IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
1524 GetSignature(null, 0, new RuntimeFieldHandleInternal(), methodHandle, declaringType);
1527 public Signature(IRuntimeFieldInfo fieldHandle, RuntimeType declaringType)
1529 GetSignature(null, 0, fieldHandle.Value, null, declaringType);
1530 GC.KeepAlive(fieldHandle);
1533 public Signature(void* pCorSig, int cCorSig, RuntimeType declaringType)
1535 GetSignature(pCorSig, cCorSig, new RuntimeFieldHandleInternal(), null, declaringType);
1539 #region Internal Members
1540 internal CallingConventions CallingConvention { get { return (CallingConventions)(byte)m_managedCallingConventionAndArgIteratorFlags; } }
1541 internal RuntimeType[] Arguments { get { return m_arguments; } }
1542 internal RuntimeType ReturnType { get { return m_returnTypeORfieldType; } }
1543 internal RuntimeType FieldType { get { return m_returnTypeORfieldType; } }
1545 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1546 internal static extern bool CompareSig(Signature sig1, Signature sig2);
1548 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1549 internal extern Type[] GetCustomModifiers(int position, bool required);
1554 internal abstract class Resolver
1556 internal struct CORINFO_EH_CLAUSE
1559 internal int TryOffset;
1560 internal int TryLength;
1561 internal int HandlerOffset;
1562 internal int HandlerLength;
1563 internal int ClassTokenOrFilterOffset;
1567 internal abstract RuntimeType GetJitContext(ref int securityControlFlags);
1568 internal abstract byte[] GetCodeInfo(ref int stackSize, ref int initLocals, ref int EHCount);
1569 internal abstract byte[] GetLocalsSignature();
1570 internal abstract unsafe void GetEHInfo(int EHNumber, void* exception);
1571 internal abstract unsafe byte[] GetRawEHInfo();
1573 internal abstract String GetStringLiteral(int token);
1574 internal abstract void ResolveToken(int token, out IntPtr typeHandle, out IntPtr methodHandle, out IntPtr fieldHandle);
1575 internal abstract byte[] ResolveSignature(int token, int fromMethod);
1577 internal abstract MethodInfo GetDynamicMethod();