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.
8 #include "runtimehandles.h"
12 #include "typehandle.h"
14 #include "siginfo.hpp"
15 #include "clsload.hpp"
16 #include "typestring.h"
17 #include "typeparse.h"
21 #include "jitinterface.h"
22 #include "stackprobe.h"
25 #include "interoputil.h"
27 #include "virtualcallstub.h"
28 #include "contractimpl.h"
29 #include "dynamicmethod.h"
30 #include "peimagelayout.inl"
31 #include "eventtrace.h"
32 #include "invokeutil.h"
35 FCIMPL3(FC_BOOL_RET, Utf8String::EqualsCaseSensitive, LPCUTF8 szLhs, LPCUTF8 szRhs, INT32 stringNumBytes)
39 PRECONDITION(CheckPointer(szLhs));
40 PRECONDITION(CheckPointer(szRhs));
44 // Important: the string in pSsz isn't null terminated so the length must be used
45 // when performing operations on the string.
47 // At this point, both the left and right strings are guaranteed to have the
49 FC_RETURN_BOOL(strncmp(szLhs, szRhs, stringNumBytes) == 0);
53 BOOL QCALLTYPE Utf8String::EqualsCaseInsensitive(LPCUTF8 szLhs, LPCUTF8 szRhs, INT32 stringNumBytes)
57 // Important: the string in pSsz isn't null terminated so the length must be used
58 // when performing operations on the string.
60 BOOL fStringsEqual = FALSE;
64 _ASSERTE(CheckPointer(szLhs));
65 _ASSERTE(CheckPointer(szRhs));
67 // At this point, both the left and right strings are guaranteed to have the
69 StackSString lhs(SString::Utf8, szLhs, stringNumBytes);
70 StackSString rhs(SString::Utf8, szRhs, stringNumBytes);
72 // We can use SString for simple case insensitive compares
73 fStringsEqual = lhs.EqualsCaseInsensitive(rhs);
80 ULONG QCALLTYPE Utf8String::HashCaseInsensitive(LPCUTF8 sz, INT32 stringNumBytes)
84 // Important: the string in pSsz isn't null terminated so the length must be used
85 // when performing operations on the string.
91 StackSString str(SString::Utf8, sz, stringNumBytes);
92 hashValue = str.HashCaseInsensitive();
99 static BOOL CheckCAVisibilityFromDecoratedType(MethodTable* pCAMT, MethodDesc* pCACtor, MethodTable* pDecoratedMT, Module* pDecoratedModule)
106 PRECONDITION(CheckPointer(pCAMT));
107 PRECONDITION(CheckPointer(pCACtor, NULL_OK));
108 PRECONDITION(CheckPointer(pDecoratedMT, NULL_OK));
109 PRECONDITION(CheckPointer(pDecoratedModule));
113 DWORD dwAttr = mdPublic;
117 // Allowing a dangerous method to be called in custom attribute instantiation is, well, dangerous.
118 // E.g. a malicious user can craft a custom attribute record that fools us into creating a DynamicMethod
119 // object attached to typeof(System.Reflection.CustomAttribute) and thus gain access to mscorlib internals.
120 if (InvokeUtil::IsDangerousMethod(pCACtor))
123 _ASSERTE(pCACtor->IsCtor());
125 dwAttr = pCACtor->GetAttrs();
128 StaticAccessCheckContext accessContext(NULL, pDecoratedMT, pDecoratedModule->GetAssembly());
130 return ClassLoader::CanAccess(
133 pCAMT->GetAssembly(),
137 *AccessCheckOptions::s_pNormalAccessChecks);
140 BOOL QCALLTYPE RuntimeMethodHandle::IsCAVisibleFromDecoratedType(
141 EnregisteredTypeHandle targetTypeHandle,
142 MethodDesc * pTargetCtor,
143 EnregisteredTypeHandle sourceTypeHandle,
144 QCall::ModuleHandle sourceModuleHandle)
151 TypeHandle sourceHandle = TypeHandle::FromPtr(sourceTypeHandle);
152 TypeHandle targetHandle = TypeHandle::FromPtr(targetTypeHandle);
154 _ASSERTE((sourceHandle.IsNull() || !sourceHandle.IsTypeDesc()) &&
155 !targetHandle.IsNull() &&
156 !targetHandle.IsTypeDesc());
158 if (sourceHandle.IsTypeDesc() ||
159 targetHandle.IsNull() ||
160 targetHandle.IsTypeDesc())
161 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
163 bResult = CheckCAVisibilityFromDecoratedType(targetHandle.AsMethodTable(), pTargetCtor, sourceHandle.AsMethodTable(), sourceModuleHandle);
170 NOINLINE static ReflectClassBaseObject* GetRuntimeTypeHelper(LPVOID __me, TypeHandle typeHandle, OBJECTREF keepAlive)
172 FC_INNER_PROLOG_NO_ME_SETUP();
173 if (typeHandle.AsPtr() == NULL)
176 // RuntimeTypeHandle::GetRuntimeType has picked off the most common case, but does not cover array types.
177 // Before we do the really heavy weight option of setting up a helper method frame, check if we have to.
178 OBJECTREF refType = typeHandle.GetManagedClassObjectFast();
180 return (ReflectClassBaseObject*)OBJECTREFToObject(refType);
182 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
183 refType = typeHandle.GetManagedClassObject();
184 HELPER_METHOD_FRAME_END();
187 return (ReflectClassBaseObject*)OBJECTREFToObject(refType);
190 #define RETURN_CLASS_OBJECT(typeHandle, keepAlive) FC_INNER_RETURN(ReflectClassBaseObject*, GetRuntimeTypeHelper(__me, typeHandle, keepAlive))
192 NOINLINE ReflectModuleBaseObject* GetRuntimeModuleHelper(LPVOID __me, Module *pModule, OBJECTREF keepAlive)
194 FC_INNER_PROLOG_NO_ME_SETUP();
198 DomainFile * pDomainFile = pModule->FindDomainFile(GetAppDomain());
200 OBJECTREF refModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
202 if(refModule != NULL)
203 return (ReflectModuleBaseObject*)OBJECTREFToObject(refModule);
205 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
206 refModule = pModule->GetExposedObject();
207 HELPER_METHOD_FRAME_END();
210 return (ReflectModuleBaseObject*)OBJECTREFToObject(refModule);
213 NOINLINE AssemblyBaseObject* GetRuntimeAssemblyHelper(LPVOID __me, DomainAssembly *pAssembly, OBJECTREF keepAlive)
215 FC_INNER_PROLOG_NO_ME_SETUP();
216 if (pAssembly == NULL)
219 OBJECTREF refAssembly = (pAssembly != NULL) ? pAssembly->GetExposedAssemblyObjectIfExists() : NULL;
221 if(refAssembly != NULL)
222 return (AssemblyBaseObject*)OBJECTREFToObject(refAssembly);
224 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
225 refAssembly = pAssembly->GetExposedAssemblyObject();
226 HELPER_METHOD_FRAME_END();
229 return (AssemblyBaseObject*)OBJECTREFToObject(refAssembly);
233 // This is the routine that is called by the 'typeof()' operator in C#. It is one of the most commonly used
234 // reflection operations. This call should be optimized away in nearly all situations
235 FCIMPL1_V(ReflectClassBaseObject*, RuntimeTypeHandle::GetTypeFromHandle, FCALLRuntimeTypeHandle th)
240 return FCALL_RTH_TO_REFLECTCLASS(th);
244 FCIMPL1(ReflectClassBaseObject*, RuntimeTypeHandle::GetRuntimeType, EnregisteredTypeHandle th)
248 TypeHandle typeHandle = TypeHandle::FromPtr(th);
249 _ASSERTE(CheckPointer(typeHandle.AsPtr(), NULL_OK));
250 if (typeHandle.AsPtr()!= NULL)
252 if (!typeHandle.IsTypeDesc())
254 OBJECTREF typePtr = typeHandle.AsMethodTable()->GetManagedClassObjectIfExists();
257 return (ReflectClassBaseObject*)OBJECTREFToObject(typePtr);
264 RETURN_CLASS_OBJECT(typeHandle, NULL);
268 FCIMPL1_V(EnregisteredTypeHandle, RuntimeTypeHandle::GetValueInternal, FCALLRuntimeTypeHandle RTH)
272 if (FCALL_RTH_TO_REFLECTCLASS(RTH) == NULL)
275 return FCALL_RTH_TO_REFLECTCLASS(RTH) ->GetType().AsPtr();
279 // TypeEqualsHelper and TypeNotEqualsHelper are almost identical.
280 // Unfortunately we cannot combime them because they need to hardcode the caller's name
281 NOINLINE static BOOL TypeEqualSlow(OBJECTREF refL, OBJECTREF refR, LPVOID __me)
285 FC_INNER_PROLOG_NO_ME_SETUP();
287 _ASSERTE(refL != NULL && refR != NULL);
289 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_2(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, refL, refR);
291 MethodDescCallSite TypeEqualsMethod(METHOD__OBJECT__EQUALS, &refL);
299 ret = TypeEqualsMethod.Call_RetBool(args);
301 HELPER_METHOD_FRAME_END();
310 #include <optsmallperfcritical.h>
312 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::TypeEQ, Object* left, Object* right)
316 OBJECTREF refL = (OBJECTREF)left;
317 OBJECTREF refR = (OBJECTREF)right;
321 FC_RETURN_BOOL(TRUE);
326 FC_RETURN_BOOL(FALSE);
329 if ((refL->GetMethodTable() == g_pRuntimeTypeClass || refR->GetMethodTable() == g_pRuntimeTypeClass))
331 // Quick path for negative common case
332 FC_RETURN_BOOL(FALSE);
335 // The fast path didn't get us the result
336 // Let's try the slow path: refL.Equals(refR);
337 FC_INNER_RETURN(FC_BOOL_RET, (FC_BOOL_RET)(!!TypeEqualSlow(refL, refR, __me)));
341 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::TypeNEQ, Object* left, Object* right)
345 OBJECTREF refL = (OBJECTREF)left;
346 OBJECTREF refR = (OBJECTREF)right;
350 FC_RETURN_BOOL(FALSE);
355 FC_RETURN_BOOL(TRUE);
358 if ((refL->GetMethodTable() == g_pRuntimeTypeClass || refR->GetMethodTable() == g_pRuntimeTypeClass))
360 // Quick path for negative common case
361 FC_RETURN_BOOL(TRUE);
364 // The fast path didn't get us the result
365 // Let's try the slow path: refL.Equals(refR);
366 FC_INNER_RETURN(FC_BOOL_RET, (FC_BOOL_RET)(!TypeEqualSlow(refL, refR, __me)));
370 #include <optdefault.h>
375 #ifdef FEATURE_COMINTEROP
376 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsWindowsRuntimeObjectType, ReflectClassBaseObject *rtTypeUNSAFE)
380 BOOL isWindowsRuntimeType = FALSE;
382 TypeHandle typeHandle = rtTypeUNSAFE->GetType();
383 MethodTable *pMT = typeHandle.GetMethodTable();
387 isWindowsRuntimeType = pMT->IsWinRTObjectType();
390 FC_RETURN_BOOL(isWindowsRuntimeType);
394 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsTypeExportedToWindowsRuntime, ReflectClassBaseObject *rtTypeUNSAFE)
398 BOOL isExportedToWinRT = FALSE;
400 TypeHandle typeHandle = rtTypeUNSAFE->GetType();
401 MethodTable *pMT = typeHandle.GetMethodTable();
405 isExportedToWinRT = pMT->IsExportedToWinRT();
408 FC_RETURN_BOOL(isExportedToWinRT);
411 #endif // FEATURE_COMINTEROP
413 NOINLINE static MethodDesc * RestoreMethodHelper(MethodDesc * pMethod, LPVOID __me)
415 FC_INNER_PROLOG_NO_ME_SETUP();
417 HELPER_METHOD_FRAME_BEGIN_RET_0();
418 pMethod->CheckRestore();
419 HELPER_METHOD_FRAME_END();
426 FCIMPL1(MethodDesc *, RuntimeTypeHandle::GetFirstIntroducedMethod, ReflectClassBaseObject *pTypeUNSAFE) {
429 PRECONDITION(CheckPointer(pTypeUNSAFE));
433 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
434 TypeHandle typeHandle = refType->GetType();
436 if (typeHandle.IsGenericVariable())
437 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
439 if (typeHandle.IsTypeDesc()) {
440 if (!typeHandle.IsArray())
444 MethodTable* pMT = typeHandle.GetMethodTable();
448 MethodDesc* pMethod = MethodTable::IntroducedMethodIterator::GetFirst(pMT);
450 // The only method that can show up here unrestored is instantiated methods. Check for it before performing the expensive IsRestored() check.
451 if (pMethod != NULL && pMethod->GetClassification() == mcInstantiated && !pMethod->IsRestored()) {
452 FC_INNER_RETURN(MethodDesc *, RestoreMethodHelper(pMethod, __me));
455 _ASSERTE(pMethod == NULL || pMethod->IsRestored());
460 #include <optsmallperfcritical.h>
461 FCIMPL1(void, RuntimeTypeHandle::GetNextIntroducedMethod, MethodDesc ** ppMethod) {
464 PRECONDITION(CheckPointer(ppMethod));
465 PRECONDITION(CheckPointer(*ppMethod));
469 MethodDesc *pMethod = MethodTable::IntroducedMethodIterator::GetNext(*ppMethod);
473 if (pMethod != NULL && pMethod->GetClassification() == mcInstantiated && !pMethod->IsRestored()) {
474 FC_INNER_RETURN_VOID(RestoreMethodHelper(pMethod, __me));
477 _ASSERTE(pMethod == NULL || pMethod->IsRestored());
480 #include <optdefault.h>
482 FCIMPL1(INT32, RuntimeTypeHandle::GetCorElementType, ReflectClassBaseObject *pTypeUNSAFE) {
488 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
491 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
493 return refType->GetType().GetSignatureCorElementType();
497 FCIMPL1(AssemblyBaseObject*, RuntimeTypeHandle::GetAssembly, ReflectClassBaseObject *pTypeUNSAFE) {
503 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
506 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
508 DomainFile *pDomainFile = NULL;
510 Module *pModule = refType->GetType().GetAssembly()->GetManifestModule();
512 pDomainFile = pModule->FindDomainFile(GetAppDomain());
513 #ifdef FEATURE_LOADER_OPTIMIZATION
514 if (pDomainFile == NULL)
516 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
518 pDomainFile = GetAppDomain()->LoadDomainNeutralModuleDependency(pModule, FILE_LOADED);
520 HELPER_METHOD_FRAME_END();
522 #endif // FEATURE_LOADER_OPTIMIZATION
525 FC_RETURN_ASSEMBLY_OBJECT((DomainAssembly *)pDomainFile, refType);
530 FCIMPL1(FC_BOOL_RET, RuntimeFieldHandle::AcquiresContextFromThis, FieldDesc *pField)
534 PRECONDITION(CheckPointer(pField));
538 FC_RETURN_BOOL(pField->IsSharedByGenericInstantiations());
543 FCIMPL1(ReflectModuleBaseObject*, RuntimeTypeHandle::GetModule, ReflectClassBaseObject *pTypeUNSAFE) {
551 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
554 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
556 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
558 result = refType->GetType().GetModule();
560 END_SO_INTOLERANT_CODE;
562 FC_RETURN_MODULE_OBJECT(result, refType);
566 FCIMPL1(ReflectClassBaseObject *, RuntimeTypeHandle::GetBaseType, ReflectClassBaseObject *pTypeUNSAFE) {
572 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
575 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
577 TypeHandle typeHandle = refType->GetType();
579 if (typeHandle.IsGenericVariable())
580 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
582 if (typeHandle.IsTypeDesc()) {
583 if (!typeHandle.IsArray())
587 RETURN_CLASS_OBJECT(typeHandle.GetParent(), refType);
591 FCIMPL1(ReflectClassBaseObject *, RuntimeTypeHandle::GetElementType, ReflectClassBaseObject *pTypeUNSAFE) {
597 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
600 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
602 TypeHandle typeHandle = refType->GetType();
604 if (!typeHandle.IsTypeDesc())
607 if (typeHandle.IsGenericVariable())
610 TypeHandle typeReturn;
612 if (typeHandle.IsArray())
613 typeReturn = typeHandle.AsArray()->GetArrayElementTypeHandle();
615 typeReturn = typeHandle.AsTypeDesc()->GetTypeParam();
617 RETURN_CLASS_OBJECT(typeReturn, refType);
621 FCIMPL1(INT32, RuntimeTypeHandle::GetArrayRank, ReflectClassBaseObject *pTypeUNSAFE) {
624 PRECONDITION(CheckPointer(pTypeUNSAFE));
625 PRECONDITION(pTypeUNSAFE->GetType().IsArray());
629 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
631 return (INT32)refType->GetType().AsArray()->GetRank();
635 FCIMPL1(INT32, RuntimeTypeHandle::GetNumVirtuals, ReflectClassBaseObject *pTypeUNSAFE) {
641 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
644 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
646 TypeHandle typeHandle = refType->GetType();
648 if (typeHandle.IsGenericVariable())
649 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
651 MethodTable *pMT = typeHandle.GetMethodTable();
654 return (INT32)pMT->GetNumVirtuals();
656 return 0; //REVIEW: should this return the number of methods in Object?
660 FCIMPL2(MethodDesc *, RuntimeTypeHandle::GetMethodAt, ReflectClassBaseObject *pTypeUNSAFE, INT32 slot) {
666 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
669 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
671 TypeHandle typeHandle = refType->GetType();
673 MethodDesc* pRetMethod = NULL;
675 if (typeHandle.IsGenericVariable())
676 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
678 if (slot < 0 || slot >= (INT32)typeHandle.GetMethodTable()->GetNumVirtuals())
679 FCThrowRes(kArgumentException, W("Arg_ArgumentOutOfRangeException"));
681 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
682 pRetMethod = typeHandle.GetMethodTable()->GetMethodDescForSlot((DWORD)slot);
683 HELPER_METHOD_FRAME_END();
690 FCIMPL3(FC_BOOL_RET, RuntimeTypeHandle::GetFields, ReflectClassBaseObject *pTypeUNSAFE, INT32 **result, INT32 *pCount) {
696 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
698 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
700 TypeHandle typeHandle = refType->GetType();
702 if (!pCount || !result)
703 FCThrow(kArgumentNullException);
705 if (typeHandle.IsGenericVariable())
706 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
708 if (typeHandle.IsTypeDesc()) {
710 FC_RETURN_BOOL(TRUE);
713 MethodTable *pMT= typeHandle.GetMethodTable();
715 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
718 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
719 // <TODO>Check this approximation - we may be losing exact type information </TODO>
720 ApproxFieldDescIterator fdIterator(pMT, ApproxFieldDescIterator::ALL_FIELDS);
721 INT32 count = (INT32)fdIterator.Count();
729 for(INT32 i = 0; i < count; i ++)
730 result[i] = (INT32*)fdIterator.Next();
735 HELPER_METHOD_FRAME_END();
736 FC_RETURN_BOOL(retVal);
740 void QCALLTYPE RuntimeMethodHandle::ConstructInstantiation(MethodDesc * pMethod, DWORD format, QCall::StringHandleOnStack retString)
747 TypeString::AppendInst(ss, pMethod->LoadMethodInstantiation(), format);
753 void QCALLTYPE RuntimeTypeHandle::ConstructName(EnregisteredTypeHandle pTypeHandle, DWORD format, QCall::StringHandleOnStack retString)
760 TypeString::AppendType(ss, TypeHandle::FromPtr(pTypeHandle), format);
766 PTRARRAYREF CopyRuntimeTypeHandles(TypeHandle * prgTH, FixupPointer<TypeHandle> * prgTH2, INT32 numTypeHandles, BinderClassID arrayElemType)
775 PTRARRAYREF refReturn = NULL;
776 PTRARRAYREF refArray = NULL;
778 if (numTypeHandles == 0)
781 _ASSERTE((prgTH != NULL) || (prgTH2 != NULL));
784 _ASSERTE(prgTH2 == NULL);
787 GCPROTECT_BEGIN(refArray);
788 TypeHandle thRuntimeType = TypeHandle(MscorlibBinder::GetClass(arrayElemType));
789 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(thRuntimeType, ELEMENT_TYPE_SZARRAY);
790 refArray = (PTRARRAYREF)AllocateArrayEx(arrayHandle, &numTypeHandles, 1);
792 for (INT32 i = 0; i < numTypeHandles; i++)
799 th = prgTH2[i].GetValue();
801 OBJECTREF refType = th.GetManagedClassObject();
802 refArray->SetAt(i, refType);
805 refReturn = refArray;
811 void QCALLTYPE RuntimeTypeHandle::GetConstraints(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retTypeArray)
815 TypeHandle* constraints = NULL;
819 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
821 if (!typeHandle.IsGenericVariable())
822 COMPlusThrow(kArgumentException, W("Arg_InvalidHandle"));
824 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
827 constraints = pGenericVariable->GetConstraints(&dwCount);
830 retTypeArray.Set(CopyRuntimeTypeHandles(constraints, NULL, dwCount, CLASS__TYPE));
837 FCIMPL1(PtrArray*, RuntimeTypeHandle::GetInterfaces, ReflectClassBaseObject *pTypeUNSAFE) {
843 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
846 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
848 TypeHandle typeHandle = refType->GetType();
850 if (typeHandle.IsGenericVariable())
851 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
853 INT32 ifaceCount = 0;
855 PTRARRAYREF refRetVal = NULL;
856 HELPER_METHOD_FRAME_BEGIN_RET_2(refRetVal, refType);
858 if (typeHandle.IsTypeDesc())
860 if (typeHandle.IsArray())
862 ifaceCount = typeHandle.GetMethodTable()->GetNumInterfaces();
871 ifaceCount = typeHandle.GetMethodTable()->GetNumInterfaces();
874 // Allocate the array
877 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(g_pRuntimeTypeClass), ELEMENT_TYPE_SZARRAY);
878 refRetVal = (PTRARRAYREF)AllocateArrayEx(arrayHandle, &ifaceCount, 1);
880 // populate type array
883 MethodTable::InterfaceMapIterator it = typeHandle.GetMethodTable()->IterateInterfaceMap();
886 OBJECTREF refInterface = it.GetInterface()->GetManagedClassObject();
887 refRetVal->SetAt(i, refInterface);
888 _ASSERTE(refRetVal->GetAt(i) != NULL);
893 HELPER_METHOD_FRAME_END();
895 return (PtrArray*)OBJECTREFToObject(refRetVal);
899 FCIMPL1(INT32, RuntimeTypeHandle::GetAttributes, ReflectClassBaseObject *pTypeUNSAFE) {
905 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
908 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
910 TypeHandle typeHandle = refType->GetType();
912 if (typeHandle.IsTypeDesc()) {
914 if (typeHandle.IsGenericVariable()) {
918 if (!typeHandle.IsArray())
922 #ifdef FEATURE_COMINTEROP
923 // __ComObject types are always public.
924 if (IsComObjectClass(typeHandle))
925 return (typeHandle.GetMethodTable()->GetAttrClass() & tdVisibilityMask) | tdPublic;
926 #endif // FEATURE_COMINTEROP
930 ret = (INT32)typeHandle.GetMethodTable()->GetAttrClass();
936 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsValueType, ReflectClassBaseObject *pTypeUNSAFE)
943 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
945 _ASSERTE(refType != NULL);
947 TypeHandle typeHandle = refType->GetType();
949 FC_RETURN_BOOL(typeHandle.IsValueType());
953 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsInterface, ReflectClassBaseObject *pTypeUNSAFE)
960 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
962 _ASSERTE(refType != NULL);
964 TypeHandle typeHandle = refType->GetType();
966 FC_RETURN_BOOL(typeHandle.IsInterface());
971 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsByRefLike, ReflectClassBaseObject *pTypeUNSAFE)
978 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
980 _ASSERTE(refType != NULL);
982 TypeHandle typeHandle = refType->GetType();
984 FC_RETURN_BOOL(typeHandle.IsByRefLike());
990 RuntimeTypeHandle::IsVisible(
991 EnregisteredTypeHandle pTypeHandle)
999 BOOL fIsExternallyVisible = FALSE;
1003 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1005 _ASSERTE(!typeHandle.IsNull());
1007 fIsExternallyVisible = typeHandle.IsExternallyVisible();
1011 return fIsExternallyVisible;
1012 } // RuntimeTypeHandle::IsVisible
1014 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::IsComObject, ReflectClassBaseObject *pTypeUNSAFE, CLR_BOOL isGenericCOM) {
1015 #ifdef FEATURE_COMINTEROP
1023 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1025 if (refType == NULL)
1026 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1028 TypeHandle typeHandle = refType->GetType();
1030 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1033 ret = IsComObjectClass(typeHandle);
1035 ret = IsComWrapperClass(typeHandle);
1037 HELPER_METHOD_FRAME_END();
1039 FC_RETURN_BOOL(ret);
1045 PRECONDITION(CheckPointer(pTypeUNSAFE));
1049 FC_RETURN_BOOL(FALSE);
1054 FCIMPL1(LPCUTF8, RuntimeTypeHandle::GetUtf8Name, ReflectClassBaseObject* pTypeUNSAFE) {
1060 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1062 if (refType == NULL)
1063 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1065 TypeHandle typeHandle = refType->GetType();
1066 INT32 tkTypeDef = mdTypeDefNil;
1067 LPCUTF8 szName = NULL;
1069 if (typeHandle.IsGenericVariable())
1070 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1072 if (typeHandle.IsTypeDesc())
1073 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1075 MethodTable* pMT= typeHandle.GetMethodTable();
1078 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1080 tkTypeDef = (INT32)pMT->GetCl();
1082 if (IsNilToken(tkTypeDef))
1083 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1085 if (FAILED(pMT->GetMDImport()->GetNameOfTypeDef(tkTypeDef, &szName, NULL)))
1087 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1090 _ASSERTE(CheckPointer(szName, NULL_OK));
1096 FCIMPL1(INT32, RuntimeTypeHandle::GetToken, ReflectClassBaseObject *pTypeUNSAFE) {
1102 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1104 if (refType == NULL)
1105 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1107 TypeHandle typeHandle = refType->GetType();
1109 if (typeHandle.IsTypeDesc())
1111 if (typeHandle.IsGenericVariable())
1113 INT32 tkTypeDef = typeHandle.AsGenericVariable()->GetToken();
1115 _ASSERTE(!IsNilToken(tkTypeDef) && TypeFromToken(tkTypeDef) == mdtGenericParam);
1120 return mdTypeDefNil;
1123 return (INT32)typeHandle.AsMethodTable()->GetCl();
1127 PVOID QCALLTYPE RuntimeTypeHandle::GetGCHandle(EnregisteredTypeHandle pTypeHandle, INT32 handleType)
1131 OBJECTHANDLE objHandle = NULL;
1137 TypeHandle th = TypeHandle::FromPtr(pTypeHandle);
1138 assert(handleType >= HNDTYPE_WEAK_SHORT && handleType <= HNDTYPE_WEAK_WINRT);
1139 objHandle = th.GetDomain()->CreateTypedHandle(NULL, static_cast<HandleType>(handleType));
1140 th.GetLoaderAllocator()->RegisterHandleForCleanup(objHandle);
1147 void QCALLTYPE RuntimeTypeHandle::VerifyInterfaceIsImplemented(EnregisteredTypeHandle pTypeHandle, EnregisteredTypeHandle pIFaceHandle)
1153 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1154 TypeHandle ifaceHandle = TypeHandle::FromPtr(pIFaceHandle);
1156 if (typeHandle.IsGenericVariable())
1157 COMPlusThrow(kArgumentException, W("Arg_InvalidHandle"));
1159 if (typeHandle.IsTypeDesc()) {
1160 if (!typeHandle.IsArray())
1161 COMPlusThrow(kArgumentException, W("Arg_NotFoundIFace"));
1164 if (typeHandle.IsInterface())
1165 COMPlusThrow(kArgumentException, W("Argument_InterfaceMap"));
1167 if (!ifaceHandle.IsInterface())
1168 COMPlusThrow(kArgumentException, W("Arg_MustBeInterface"));
1170 // First try the cheap check, which amounts to iterating the interface map looking for
1171 // the ifaceHandle MethodTable.
1172 if (!typeHandle.GetMethodTable()->ImplementsInterface(ifaceHandle.AsMethodTable()))
1173 { // If the cheap check fails, try the more expensive but complete check.
1174 if (!typeHandle.CanCastTo(ifaceHandle))
1175 { // If the complete check fails, we're certain that this type
1176 // does not implement the interface specified.
1177 COMPlusThrow(kArgumentException, W("Arg_NotFoundIFace"));
1184 INT32 QCALLTYPE RuntimeTypeHandle::GetInterfaceMethodImplementationSlot(EnregisteredTypeHandle pTypeHandle, EnregisteredTypeHandle pOwner, MethodDesc * pMD)
1188 INT32 slotNumber = -1;
1192 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1193 TypeHandle thOwnerOfMD = TypeHandle::FromPtr(pOwner);
1195 // Ok to have INVALID_SLOT in the case where abstract class does not implement an interface method.
1196 // This case can not be reproed using C# "implements" all interface methods
1197 // with at least an abstract method. b19897_GetInterfaceMap_Abstract.exe tests this case.
1198 //@TODO:STUBDISPATCH: Don't need to track down the implementation, just the declaration, and this can
1199 //@TODO: be done faster - just need to make a function FindDispatchDecl.
1200 DispatchSlot slot(typeHandle.GetMethodTable()->FindDispatchSlotForInterfaceMD(thOwnerOfMD, pMD));
1202 slotNumber = slot.GetMethodDesc()->GetSlot();
1209 void QCALLTYPE RuntimeTypeHandle::GetDefaultConstructor(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retMethod)
1215 MethodDesc* pCtor = NULL;
1217 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1219 if (!typeHandle.IsTypeDesc())
1221 MethodTable* pMethodTable = typeHandle.AsMethodTable();
1222 if (pMethodTable->HasDefaultConstructor())
1223 pCtor = pMethodTable->GetDefaultConstructor();
1229 retMethod.Set(pCtor->GetStubMethodInfo());
1236 FCIMPL1(ReflectMethodObject*, RuntimeTypeHandle::GetDeclaringMethod, ReflectClassBaseObject *pTypeUNSAFE) {
1242 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1244 if (refType == NULL)
1245 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1247 TypeHandle typeHandle = refType->GetType();;
1249 if (!typeHandle.IsTypeDesc())
1252 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
1253 mdToken defToken = pGenericVariable->GetTypeOrMethodDef();
1254 if (TypeFromToken(defToken) != mdtMethodDef)
1257 REFLECTMETHODREF pRet = NULL;
1258 HELPER_METHOD_FRAME_BEGIN_RET_0();
1259 MethodDesc * pMD = pGenericVariable->LoadOwnerMethod();
1260 pMD->CheckRestore();
1261 pRet = pMD->GetStubMethodInfo();
1262 HELPER_METHOD_FRAME_END();
1264 return (ReflectMethodObject*)OBJECTREFToObject(pRet);
1268 FCIMPL1(ReflectClassBaseObject*, RuntimeTypeHandle::GetDeclaringType, ReflectClassBaseObject *pTypeUNSAFE) {
1274 TypeHandle retTypeHandle;
1276 BOOL fThrowException = FALSE;
1277 LPCWSTR argName = W("Arg_InvalidHandle");
1278 RuntimeExceptionKind reKind = kArgumentNullException;
1280 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1282 if (refType == NULL)
1283 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1285 TypeHandle typeHandle = refType->GetType();
1287 MethodTable* pMT = NULL;
1288 mdTypeDef tkTypeDef = mdTokenNil;
1290 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1291 if (typeHandle.IsTypeDesc()) {
1293 if (typeHandle.IsGenericVariable()) {
1294 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
1295 mdToken defToken = pGenericVariable->GetTypeOrMethodDef();
1297 // Try the fast way first (if the declaring type has been loaded already).
1298 if (TypeFromToken(defToken) == mdtMethodDef)
1300 MethodDesc * retMethod = pGenericVariable->GetModule()->LookupMethodDef(defToken);
1301 if (retMethod != NULL)
1302 retTypeHandle = retMethod->GetMethodTable();
1306 retTypeHandle = pGenericVariable->GetModule()->LookupTypeDef(defToken);
1309 if (!retTypeHandle.IsNull() && retTypeHandle.IsFullyLoaded())
1312 // OK, need to go the slow way and load the type first.
1313 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1315 if (TypeFromToken(defToken) == mdtMethodDef)
1317 retTypeHandle = pGenericVariable->LoadOwnerMethod()->GetMethodTable();
1321 retTypeHandle = pGenericVariable->LoadOwnerType();
1323 retTypeHandle.CheckRestore();
1325 HELPER_METHOD_FRAME_END();
1328 if (!typeHandle.IsArray())
1330 retTypeHandle = TypeHandle();
1335 pMT = typeHandle.GetMethodTable();
1339 fThrowException = TRUE;
1343 if(!pMT->GetClass()->IsNested())
1345 retTypeHandle = TypeHandle();
1349 tkTypeDef = pMT->GetCl();
1351 if (FAILED(typeHandle.GetModule()->GetMDImport()->GetNestedClassProps(tkTypeDef, &tkTypeDef)))
1353 fThrowException = TRUE;
1354 reKind = kBadImageFormatException;
1359 // Try the fast way first (if the declaring type has been loaded already).
1360 retTypeHandle = typeHandle.GetModule()->LookupTypeDef(tkTypeDef);
1361 if (retTypeHandle.IsNull())
1363 // OK, need to go the slow way and load the type first.
1364 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1366 retTypeHandle = ClassLoader::LoadTypeDefThrowing(typeHandle.GetModule(), tkTypeDef,
1367 ClassLoader::ThrowIfNotFound,
1368 ClassLoader::PermitUninstDefOrRef);
1370 HELPER_METHOD_FRAME_END();
1374 END_SO_INTOLERANT_CODE;
1376 if (fThrowException)
1378 FCThrowRes(reKind, argName);
1381 RETURN_CLASS_OBJECT(retTypeHandle, refType);
1385 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CanCastTo, ReflectClassBaseObject *pTypeUNSAFE, ReflectClassBaseObject *pTargetUNSAFE) {
1392 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1393 REFLECTCLASSBASEREF refTarget = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTargetUNSAFE);
1395 if ((refType == NULL) || (refTarget == NULL))
1396 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1398 TypeHandle fromHandle = refType->GetType();
1399 TypeHandle toHandle = refTarget->GetType();
1403 TypeHandle::CastResult r = fromHandle.CanCastToNoGC(toHandle);
1404 if (r == TypeHandle::MaybeCast)
1406 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refTarget);
1407 iRetVal = fromHandle.CanCastTo(toHandle);
1408 HELPER_METHOD_FRAME_END();
1412 iRetVal = (r == TypeHandle::CanCast);
1415 // We allow T to be cast to Nullable<T>
1416 if (!iRetVal && Nullable::IsNullableType(toHandle) && !fromHandle.IsTypeDesc())
1418 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refTarget);
1419 if (Nullable::IsNullableForType(toHandle, fromHandle.AsMethodTable()))
1423 HELPER_METHOD_FRAME_END();
1426 FC_RETURN_BOOL(iRetVal);
1430 void QCALLTYPE RuntimeTypeHandle::GetTypeByNameUsingCARules(LPCWSTR pwzClassName, QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
1434 TypeHandle typeHandle;
1439 COMPlusThrowArgumentNull(W("className"),W("ArgumentNull_String"));
1441 typeHandle = TypeName::GetTypeUsingCASearchRules(pwzClassName, pModule->GetAssembly());
1444 retType.Set(typeHandle.GetManagedClassObject());
1451 void QCALLTYPE RuntimeTypeHandle::GetTypeByName(LPCWSTR pwzClassName, BOOL bThrowOnError, BOOL bIgnoreCase, BOOL bReflectionOnly,
1452 QCall::StackCrawlMarkHandle pStackMark,
1453 ICLRPrivBinder * pPrivHostBinder,
1454 BOOL bLoadTypeFromPartialNameHack, QCall::ObjectHandleOnStack retType,
1455 QCall::ObjectHandleOnStack keepAlive)
1459 TypeHandle typeHandle;
1464 COMPlusThrowArgumentNull(W("className"),W("ArgumentNull_String"));
1467 typeHandle = TypeName::GetTypeManaged(pwzClassName, NULL, bThrowOnError, bIgnoreCase, bReflectionOnly, /*bProhibitAsmQualifiedName =*/ FALSE, pStackMark,
1468 bLoadTypeFromPartialNameHack, (OBJECTREF*)keepAlive.m_ppObject,
1472 if (!typeHandle.IsNull())
1475 retType.Set(typeHandle.GetManagedClassObject());
1483 FCIMPL6(FC_BOOL_RET, RuntimeTypeHandle::SatisfiesConstraints, PTR_ReflectClassBaseObject pParamTypeUNSAFE, TypeHandle *typeContextArgs, INT32 typeContextCount, TypeHandle *methodContextArgs, INT32 methodContextCount, PTR_ReflectClassBaseObject pArgumentTypeUNSAFE);
1487 PRECONDITION(CheckPointer(typeContextArgs, NULL_OK));
1488 PRECONDITION(CheckPointer(methodContextArgs, NULL_OK));
1492 REFLECTCLASSBASEREF refParamType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pParamTypeUNSAFE);
1493 REFLECTCLASSBASEREF refArgumentType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pArgumentTypeUNSAFE);
1495 TypeHandle thGenericParameter = refParamType->GetType();
1496 TypeHandle thGenericArgument = refArgumentType->GetType();
1497 BOOL bResult = FALSE;
1498 SigTypeContext typeContext;
1500 Instantiation classInst;
1501 Instantiation methodInst;
1503 if (typeContextArgs != NULL)
1505 classInst = Instantiation(typeContextArgs, typeContextCount);
1508 if (methodContextArgs != NULL)
1510 methodInst = Instantiation(methodContextArgs, methodContextCount);
1513 SigTypeContext::InitTypeContext(classInst, methodInst, &typeContext);
1515 HELPER_METHOD_FRAME_BEGIN_RET_2(refParamType, refArgumentType);
1517 bResult = thGenericParameter.AsGenericVariable()->SatisfiesConstraints(&typeContext, thGenericArgument);
1519 HELPER_METHOD_FRAME_END();
1521 FC_RETURN_BOOL(bResult);
1525 void QCALLTYPE RuntimeTypeHandle::GetInstantiation(EnregisteredTypeHandle pType, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
1531 TypeHandle typeHandle = TypeHandle::FromPtr(pType);
1532 Instantiation inst = typeHandle.GetInstantiation();
1534 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
1540 void QCALLTYPE RuntimeTypeHandle::MakeArray(EnregisteredTypeHandle pTypeHandle, INT32 rank, QCall::ObjectHandleOnStack retType)
1544 TypeHandle arrayHandle;
1547 arrayHandle = TypeHandle::FromPtr(pTypeHandle).MakeArray(rank);
1549 retType.Set(arrayHandle.GetManagedClassObject());
1555 void QCALLTYPE RuntimeTypeHandle::MakeSZArray(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1559 TypeHandle arrayHandle;
1562 arrayHandle = TypeHandle::FromPtr(pTypeHandle).MakeSZArray();
1564 retType.Set(arrayHandle.GetManagedClassObject());
1570 void QCALLTYPE RuntimeTypeHandle::MakePointer(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1574 TypeHandle pointerHandle;
1577 pointerHandle = TypeHandle::FromPtr(pTypeHandle).MakePointer();
1579 retType.Set(pointerHandle.GetManagedClassObject());
1585 void QCALLTYPE RuntimeTypeHandle::MakeByRef(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1589 TypeHandle byRefHandle;
1592 byRefHandle = TypeHandle::FromPtr(pTypeHandle).MakeByRef();
1594 retType.Set(byRefHandle.GetManagedClassObject());
1600 BOOL QCALLTYPE RuntimeTypeHandle::IsCollectible(EnregisteredTypeHandle pTypeHandle)
1604 BOOL retVal = FALSE;
1607 retVal = TypeHandle::FromPtr(pTypeHandle).GetLoaderAllocator()->IsCollectible();
1613 void QCALLTYPE RuntimeTypeHandle::Instantiate(EnregisteredTypeHandle pTypeHandle, TypeHandle * pInstArray, INT32 cInstArray, QCall::ObjectHandleOnStack retType)
1620 type = TypeHandle::FromPtr(pTypeHandle).Instantiate(Instantiation(pInstArray, cInstArray));
1622 retType.Set(type.GetManagedClassObject());
1628 void QCALLTYPE RuntimeTypeHandle::GetGenericTypeDefinition(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1636 TypeHandle genericType = TypeHandle::FromPtr(pTypeHandle);
1638 typeDef = ClassLoader::LoadTypeDefThrowing(genericType.GetModule(),
1639 genericType.GetMethodTable()->GetCl(),
1640 ClassLoader::ThrowIfNotFound,
1641 ClassLoader::PermitUninstDefOrRef);
1644 retType.Set(typeDef.GetManagedClassObject());
1651 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CompareCanonicalHandles, ReflectClassBaseObject *pLeftUNSAFE, ReflectClassBaseObject *pRightUNSAFE)
1655 REFLECTCLASSBASEREF refLeft = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pLeftUNSAFE);
1656 REFLECTCLASSBASEREF refRight = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pRightUNSAFE);
1658 if ((refLeft == NULL) || (refRight == NULL))
1659 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1661 FC_RETURN_BOOL(refLeft->GetType().GetCanonicalMethodTable() == refRight->GetType().GetCanonicalMethodTable());
1665 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::HasInstantiation, PTR_ReflectClassBaseObject pTypeUNSAFE)
1669 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1671 if (refType == NULL)
1672 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1674 FC_RETURN_BOOL(refType->GetType().HasInstantiation());
1678 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericTypeDefinition, PTR_ReflectClassBaseObject pTypeUNSAFE)
1682 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1684 if (refType == NULL)
1685 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1687 FC_RETURN_BOOL(refType->GetType().IsGenericTypeDefinition());
1691 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericVariable, PTR_ReflectClassBaseObject pTypeUNSAFE)
1695 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1697 if (refType == NULL)
1698 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1700 FC_RETURN_BOOL(refType->GetType().IsGenericVariable());
1704 FCIMPL1(INT32, RuntimeTypeHandle::GetGenericVariableIndex, PTR_ReflectClassBaseObject pTypeUNSAFE)
1708 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1710 if (refType == NULL)
1711 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1713 return (INT32)refType->GetType().AsGenericVariable()->GetIndex();
1717 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::ContainsGenericVariables, PTR_ReflectClassBaseObject pTypeUNSAFE)
1721 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1723 if (refType == NULL)
1724 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1726 FC_RETURN_BOOL(refType->GetType().ContainsGenericVariables());
1730 FCIMPL1(IMDInternalImport*, RuntimeTypeHandle::GetMetadataImport, ReflectClassBaseObject * pTypeUNSAFE)
1734 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1736 if (refType == NULL)
1737 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1739 Module *pModule = refType->GetType().GetModule();
1741 return pModule->GetMDImport();
1746 //***********************************************************************************
1747 //***********************************************************************************
1748 //***********************************************************************************
1750 void * QCALLTYPE RuntimeMethodHandle::GetFunctionPointer(MethodDesc * pMethod)
1758 funcPtr = (void*)pMethod->GetMultiCallableAddrOfCode();
1765 FCIMPL1(LPCUTF8, RuntimeMethodHandle::GetUtf8Name, MethodDesc *pMethod) {
1771 LPCUTF8 szName = NULL;
1774 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1776 szName = pMethod->GetName();
1778 _ASSERTE(CheckPointer(szName, NULL_OK));
1784 FCIMPL2(FC_BOOL_RET, RuntimeMethodHandle::MatchesNameHash, MethodDesc * pMethod, ULONG hash)
1788 FC_RETURN_BOOL(pMethod->MightHaveName(hash));
1792 FCIMPL1(StringObject*, RuntimeMethodHandle::GetName, MethodDesc *pMethod) {
1799 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1801 STRINGREF refName = NULL;
1803 HELPER_METHOD_FRAME_BEGIN_RET_0();
1804 refName = StringObject::NewString(pMethod->GetName());
1805 HELPER_METHOD_FRAME_END();
1807 return (StringObject*)OBJECTREFToObject(refName);
1811 FCIMPL1(INT32, RuntimeMethodHandle::GetAttributes, MethodDesc *pMethod) {
1818 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1821 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1822 retVal = (INT32)pMethod->GetAttrs();
1823 END_SO_INTOLERANT_CODE;
1828 FCIMPL1(INT32, RuntimeMethodHandle::GetImplAttributes, ReflectMethodObject *pMethodUNSAFE) {
1835 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1837 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
1838 INT32 attributes = 0;
1840 if (IsNilToken(pMethod->GetMemberDef()))
1843 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1845 attributes = (INT32)pMethod->GetImplAttrs();
1847 END_SO_INTOLERANT_CODE;
1854 FCIMPL1(ReflectClassBaseObject*, RuntimeMethodHandle::GetDeclaringType, MethodDesc *pMethod) {
1857 PRECONDITION(CheckPointer(pMethod));
1862 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1864 MethodTable *pMT = pMethod->GetMethodTable();
1865 TypeHandle declType(pMT);
1868 HELPER_METHOD_FRAME_BEGIN_RET_0();
1870 // Load the TypeDesc for the array type. Note the returned type is approximate, i.e.
1871 // if shared between reference array types then we will get object[] back.
1872 DWORD rank = pMT->GetRank();
1873 TypeHandle elemType = pMT->GetApproxArrayElementTypeHandle();
1874 declType = ClassLoader::LoadArrayTypeThrowing(elemType, pMT->GetInternalCorElementType(), rank);
1875 HELPER_METHOD_FRAME_END();
1877 RETURN_CLASS_OBJECT(declType, NULL);
1881 FCIMPL1(INT32, RuntimeMethodHandle::GetSlot, MethodDesc *pMethod) {
1888 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1890 return (INT32)pMethod->GetSlot();
1894 FCIMPL3(Object *, SignatureNative::GetCustomModifiers, SignatureNative* pSignatureUNSAFE,
1895 INT32 parameter, CLR_BOOL fRequired)
1904 SIGNATURENATIVEREF pSig;
1908 gc.pSig = (SIGNATURENATIVEREF)pSignatureUNSAFE;
1911 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
1914 BYTE callConv = *(BYTE*)gc.pSig->GetCorSig();
1915 SigTypeContext typeContext;
1916 gc.pSig->GetTypeContext(&typeContext);
1917 MetaSig sig(gc.pSig->GetCorSig(),
1918 gc.pSig->GetCorSigSize(),
1919 gc.pSig->GetModule(),
1921 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
1922 _ASSERTE(callConv == sig.GetCallingConventionInfo());
1924 SigPointer argument(NULL, 0);
1926 PRECONDITION(sig.GetCallingConvention() != IMAGE_CEE_CS_CALLCONV_FIELD || parameter == 1);
1930 argument = sig.GetReturnProps();
1934 for(INT32 i = 0; i < parameter; i++)
1937 argument = sig.GetArgProps();
1940 //if (parameter < 0 || parameter > (INT32)sig.NumFixedArgs())
1941 // FCThrowResVoid(kArgumentNullException, W("Arg_ArgumentOutOfRangeException"));
1943 SigPointer sp = argument;
1944 Module* pModule = sig.GetModule();
1946 CorElementType cmodType;
1948 CorElementType cmodTypeExpected = fRequired ? ELEMENT_TYPE_CMOD_REQD : ELEMENT_TYPE_CMOD_OPT;
1950 // Discover the number of required and optional custom modifiers.
1954 IfFailThrow(sp.GetByte(&data));
1955 cmodType = (CorElementType)data;
1957 if (cmodType == ELEMENT_TYPE_CMOD_REQD || cmodType == ELEMENT_TYPE_CMOD_OPT)
1959 if (cmodType == cmodTypeExpected)
1964 else if (cmodType != ELEMENT_TYPE_SENTINEL)
1969 IfFailThrow(sp.GetToken(NULL));
1972 // Reset sp and populate the arrays for the required and optional custom
1973 // modifiers now that we know how long they should be.
1976 MethodTable *pMT = MscorlibBinder::GetClass(CLASS__TYPE);
1977 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pMT), ELEMENT_TYPE_SZARRAY);
1979 gc.retVal = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &cMods, 1);
1984 IfFailThrow(sp.GetByte(&data));
1985 cmodType = (CorElementType)data;
1988 IfFailThrow(sp.GetToken(&token));
1990 if (cmodType == cmodTypeExpected)
1992 TypeHandle th = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, token,
1994 ClassLoader::ThrowIfNotFound,
1995 ClassLoader::FailIfUninstDefOrRef);
1997 OBJECTREF refType = th.GetManagedClassObject();
1998 gc.retVal->SetAt(--cMods, refType);
2002 HELPER_METHOD_FRAME_END();
2004 return OBJECTREFToObject(gc.retVal);
2008 FCIMPL1(INT32, RuntimeMethodHandle::GetMethodDef, ReflectMethodObject *pMethodUNSAFE) {
2015 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2017 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2019 if (pMethod->HasMethodInstantiation())
2021 HELPER_METHOD_FRAME_BEGIN_RET_1(pMethodUNSAFE);
2023 pMethod = pMethod->StripMethodInstantiation();
2025 HELPER_METHOD_FRAME_END();
2028 INT32 tkMethodDef = (INT32)pMethod->GetMemberDef();
2029 _ASSERTE(TypeFromToken(tkMethodDef) == mdtMethodDef);
2031 if (IsNilToken(tkMethodDef) || TypeFromToken(tkMethodDef) != mdtMethodDef)
2032 return mdMethodDefNil;
2038 FCIMPL6(void, SignatureNative::GetSignature,
2039 SignatureNative* pSignatureNativeUNSAFE,
2040 PCCOR_SIGNATURE pCorSig, DWORD cCorSig,
2041 FieldDesc *pFieldDesc, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE) {
2044 PRECONDITION(pDeclaringTypeUNSAFE || pMethodUNSAFE->GetMethod()->IsDynamicMethod());
2045 PRECONDITION(CheckPointer(pCorSig, NULL_OK));
2046 PRECONDITION(CheckPointer(pMethodUNSAFE, NULL_OK));
2047 PRECONDITION(CheckPointer(pFieldDesc, NULL_OK));
2053 REFLECTCLASSBASEREF refDeclaringType;
2054 REFLECTMETHODREF refMethod;
2055 SIGNATURENATIVEREF pSig;
2058 gc.refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2059 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2060 gc.pSig = (SIGNATURENATIVEREF)pSignatureNativeUNSAFE;
2062 MethodDesc *pMethod;
2063 TypeHandle declType;
2065 if (gc.refDeclaringType == NULL)
2067 // for dynamic method, see precondition
2068 pMethod = gc.refMethod->GetMethod();
2069 declType = pMethod->GetMethodTable();
2073 pMethod = gc.refMethod != NULL ? gc.refMethod->GetMethod() : NULL;
2074 declType = gc.refDeclaringType->GetType();
2077 HELPER_METHOD_FRAME_BEGIN_PROTECT(gc);
2079 Module* pModule = declType.GetModule();
2083 pMethod->GetSig(&pCorSig, &cCorSig);
2084 if (pMethod->GetClassification() == mcInstantiated)
2086 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2087 if (pLoaderAllocator->IsCollectible())
2088 gc.pSig->SetKeepAlive(pLoaderAllocator->GetExposedObject());
2091 else if (pFieldDesc)
2092 pFieldDesc->GetSig(&pCorSig, &cCorSig);
2094 gc.pSig->m_sig = pCorSig;
2095 gc.pSig->m_cSig = cCorSig;
2096 gc.pSig->m_pMethod = pMethod;
2098 REFLECTCLASSBASEREF refDeclType = (REFLECTCLASSBASEREF)declType.GetManagedClassObject();
2099 gc.pSig->SetDeclaringType(refDeclType);
2101 PREFIX_ASSUME(pCorSig!= NULL);
2102 BYTE callConv = *(BYTE*)pCorSig;
2103 SigTypeContext typeContext;
2105 SigTypeContext::InitTypeContext(
2106 pMethod, declType.GetClassOrArrayInstantiation(), pMethod->LoadMethodInstantiation(), &typeContext);
2108 SigTypeContext::InitTypeContext(declType, &typeContext);
2109 MetaSig msig(pCorSig, cCorSig, pModule, &typeContext,
2110 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
2112 if (callConv == IMAGE_CEE_CS_CALLCONV_FIELD)
2114 msig.NextArgNormalized();
2116 OBJECTREF refRetType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2117 gc.pSig->SetReturnType(refRetType);
2121 gc.pSig->SetCallingConvention(msig.GetCallingConventionInfo());
2123 OBJECTREF refRetType = msig.GetRetTypeHandleThrowing().GetManagedClassObject();
2124 gc.pSig->SetReturnType(refRetType);
2126 INT32 nArgs = msig.NumFixedArgs();
2127 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(g_pRuntimeTypeClass), ELEMENT_TYPE_SZARRAY);
2129 PTRARRAYREF ptrArrayarguments = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &nArgs, 1);
2130 gc.pSig->SetArgumentArray(ptrArrayarguments);
2132 for (INT32 i = 0; i < nArgs; i++)
2136 OBJECTREF refArgType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2137 gc.pSig->SetArgument(i, refArgType);
2140 _ASSERTE(gc.pSig->m_returnType != NULL);
2143 HELPER_METHOD_FRAME_END();
2147 FCIMPL2(FC_BOOL_RET, SignatureNative::CompareSig, SignatureNative* pLhsUNSAFE, SignatureNative* pRhsUNSAFE)
2155 SIGNATURENATIVEREF pLhs;
2156 SIGNATURENATIVEREF pRhs;
2159 gc.pLhs = (SIGNATURENATIVEREF)pLhsUNSAFE;
2160 gc.pRhs = (SIGNATURENATIVEREF)pRhsUNSAFE;
2162 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2164 ret = MetaSig::CompareMethodSigs(
2165 gc.pLhs->GetCorSig(), gc.pLhs->GetCorSigSize(), gc.pLhs->GetModule(), NULL,
2166 gc.pRhs->GetCorSig(), gc.pRhs->GetCorSigSize(), gc.pRhs->GetModule(), NULL);
2168 HELPER_METHOD_FRAME_END();
2169 FC_RETURN_BOOL(ret);
2173 void QCALLTYPE RuntimeMethodHandle::GetMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
2178 Instantiation inst = pMethod->LoadMethodInstantiation();
2181 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
2187 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::HasMethodInstantiation, MethodDesc * pMethod)
2191 FC_RETURN_BOOL(pMethod->HasMethodInstantiation());
2195 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsGenericMethodDefinition, MethodDesc * pMethod)
2199 FC_RETURN_BOOL(pMethod->IsGenericMethodDefinition());
2203 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsDynamicMethod, MethodDesc * pMethod)
2207 FC_RETURN_BOOL(pMethod->IsNoMetadata());
2211 FCIMPL1(Object*, RuntimeMethodHandle::GetResolver, MethodDesc * pMethod)
2216 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2218 OBJECTREF resolver = NULL;
2219 if (pMethod->IsLCGMethod())
2221 resolver = pMethod->AsDynamicMethodDesc()->GetLCGMethodResolver()->GetManagedResolver();
2223 return OBJECTREFToObject(resolver);
2227 void QCALLTYPE RuntimeMethodHandle::Destroy(MethodDesc * pMethod)
2233 if (pMethod == NULL)
2234 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2236 DynamicMethodDesc* pDynamicMethodDesc = pMethod->AsDynamicMethodDesc();
2240 // Destroy should be called only if the managed part is gone.
2241 _ASSERTE(OBJECTREFToObject(pDynamicMethodDesc->GetLCGMethodResolver()->GetManagedResolver()) == NULL);
2243 // Fire Unload Dynamic Method Event here
2244 ETW::MethodLog::DynamicMethodDestroyed(pMethod);
2246 BEGIN_PIN_PROFILER(CORProfilerIsMonitoringDynamicFunctionUnloads());
2247 g_profControlBlock.pProfInterface->DynamicMethodUnloaded((FunctionID)pMethod);
2250 pDynamicMethodDesc->Destroy();
2255 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsTypicalMethodDefinition, ReflectMethodObject *pMethodUNSAFE)
2260 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2262 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2264 FC_RETURN_BOOL(pMethod->IsTypicalMethodDefinition());
2268 void QCALLTYPE RuntimeMethodHandle::GetTypicalMethodDefinition(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2276 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2279 MethodDesc *pMethodTypical = pMethod->LoadTypicalMethodDefinition();
2280 if (pMethodTypical != pMethod)
2283 refMethod.Set(pMethodTypical->GetStubMethodInfo());
2290 void QCALLTYPE RuntimeMethodHandle::StripMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2297 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2302 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2305 MethodDesc *pMethodStripped = pMethod->StripMethodInstantiation();
2306 if (pMethodStripped != pMethod)
2309 refMethod.Set(pMethodStripped->GetStubMethodInfo());
2316 // In the VM there might be more than one MethodDescs for a "method"
2317 // examples are methods on generic types which may have additional instantiating stubs
2318 // and methods on value types which may have additional unboxing stubs.
2320 // For generic methods we always hand out an instantiating stub except for a generic method definition
2321 // For non-generic methods on generic types we need an instantiating stub if it's one of the following
2322 // - static method on a generic class
2323 // - static or instance method on a generic interface
2324 // - static or instance method on a generic value type
2325 // The Reflection policy is to always hand out instantiating stubs in these cases
2327 // For methods on non-generic value types we can use either the cannonical method or the unboxing stub
2328 // The Reflection policy is to always hand out unboxing stubs if the methods are virtual methods
2329 // The reason for this is that in the current implementation of the class loader, the v-table slots for
2330 // those methods point to unboxing stubs already. Note that this is just a implementation choice
2331 // that might change in the future. But we should always keep this Reflection policy an invariant.
2333 // For virtual methods on generic value types (intersection of the two cases), reflection will hand
2334 // out an unboxing instantiating stub
2336 // GetInstantiatingStub is called to:
2337 // 1. create an InstantiatedMethodDesc for a generic method when calling BindGenericArguments() on a generic
2338 // method. In this case instArray will not be null.
2339 // 2. create an InstantiatedMethodDesc for a method in a generic class. In this case instArray will be null.
2340 // 3. create an UnboxingStub for a method in a value type. In this case instArray will be null.
2341 // For case 2 and 3, an instantiating stub or unboxing stub might not be needed in which case the original
2342 // MethodDesc is returned.
2343 FCIMPL3(MethodDesc*, RuntimeMethodHandle::GetStubIfNeeded,
2344 MethodDesc *pMethod,
2345 ReflectClassBaseObject *pTypeUNSAFE,
2346 PtrArray* instArrayUNSAFE)
2353 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2354 PTRARRAYREF instArray = (PTRARRAYREF)ObjectToOBJECTREF(instArrayUNSAFE);
2356 if (refType == NULL)
2357 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2359 TypeHandle instType = refType->GetType();
2360 MethodDesc *pNewMethod = pMethod;
2364 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
2366 if (instType.IsNull())
2367 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2369 // Perf optimization: this logic is actually duplicated in FindOrCreateAssociatedMethodDescForReflection, but since it
2370 // is the more common case it's worth the duplicate check here to avoid the helper method frame
2371 if ( instArray == NULL &&
2372 ( pMethod->HasMethodInstantiation() ||
2373 ( !instType.IsValueType() &&
2374 ( !instType.HasInstantiation() || instType.IsGenericTypeDefinition() ) ) ) )
2379 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, instArray);
2381 TypeHandle *inst = NULL;
2384 if (instArray != NULL)
2386 ntypars = instArray->GetNumComponents();
2388 size_t size = ntypars * sizeof(TypeHandle);
2389 if ((size / sizeof(TypeHandle)) != ntypars) // uint over/underflow
2390 COMPlusThrow(kArgumentException);
2391 inst = (TypeHandle*) _alloca(size);
2393 for (DWORD i = 0; i < ntypars; i++)
2395 REFLECTCLASSBASEREF instRef = (REFLECTCLASSBASEREF)instArray->GetAt(i);
2397 if (instRef == NULL)
2398 COMPlusThrowArgumentNull(W("inst"), W("ArgumentNull_ArrayElement"));
2400 inst[i] = instRef->GetType();
2404 pNewMethod = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMethod, instType, Instantiation(inst, ntypars));
2406 HELPER_METHOD_FRAME_END();
2413 FCIMPL2(MethodDesc*, RuntimeMethodHandle::GetMethodFromCanonical, MethodDesc *pMethod, ReflectClassBaseObject *pTypeUNSAFE)
2417 PRECONDITION(CheckPointer(pMethod));
2421 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2423 TypeHandle instType = refType->GetType();
2424 MethodDesc* pMDescInCanonMT = instType.GetMethodTable()->GetParallelMethodDesc(pMethod);
2426 return pMDescInCanonMT;
2431 FCIMPL2(MethodBody *, RuntimeMethodHandle::GetMethodBody, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2441 METHODBODYREF MethodBodyObj;
2442 EXCEPTIONHANDLINGCLAUSEREF EHClauseObj;
2443 LOCALVARIABLEINFOREF LocalVariableInfoObj;
2445 BASEARRAYREF TempArray;
2446 REFLECTCLASSBASEREF declaringType;
2447 REFLECTMETHODREF refMethod;
2450 gc.MethodBodyObj = NULL;
2451 gc.EHClauseObj = NULL;
2452 gc.LocalVariableInfoObj = NULL;
2454 gc.TempArray = NULL;
2455 gc.declaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2456 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2460 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2462 MethodDesc* pMethod = gc.refMethod->GetMethod();
2464 TypeHandle declaringType = gc.declaringType == NULL ? TypeHandle() : gc.declaringType->GetType();
2466 if (!pMethod->IsIL())
2469 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2471 MethodDesc *pMethodIL = pMethod;
2472 if (pMethod->IsWrapperStub())
2473 pMethodIL = pMethod->GetWrappedMethodDesc();
2475 COR_ILMETHOD* pILHeader = pMethodIL->GetILHeader();
2479 MethodTable * pExceptionHandlingClauseMT = MscorlibBinder::GetClass(CLASS__EH_CLAUSE);
2480 TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);
2482 MethodTable * pLocalVariableMT = MscorlibBinder::GetClass(CLASS__LOCAL_VARIABLE_INFO);
2483 TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);
2485 Module* pModule = pMethod->GetModule();
2486 COR_ILMETHOD_DECODER::DecoderStatus status;
2487 COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);
2489 if (status != COR_ILMETHOD_DECODER::SUCCESS)
2491 if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
2493 // Throw a verification HR
2494 COMPlusThrowHR(COR_E_VERIFICATION);
2498 COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
2502 gc.MethodBodyObj = (METHODBODYREF)AllocateObject(MscorlibBinder::GetClass(CLASS__METHOD_BODY));
2504 gc.MethodBodyObj->m_maxStackSize = header.GetMaxStack();
2505 gc.MethodBodyObj->m_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);
2508 gc.MethodBodyObj->m_localVarSigToken = header.GetLocalVarSigTok();
2510 gc.MethodBodyObj->m_localVarSigToken = 0;
2512 // Allocate the array of IL and fill it in from the method header.
2513 BYTE* pIL = const_cast<BYTE*>(header.Code);
2514 COUNT_T cIL = header.GetCodeSize();
2515 gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);
2517 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_IL, gc.U1Array, GetAppDomain());
2518 memcpyNoGCRefs(gc.MethodBodyObj->m_IL->GetDataPtr(), pIL, cIL);
2520 // Allocate the array of exception clauses.
2521 INT32 cEh = (INT32)header.EHCount();
2522 const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
2523 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thEHClauseArray, &cEh, 1);
2525 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_exceptionClauses, gc.TempArray, GetAppDomain());
2527 for (INT32 i = 0; i < cEh; i++)
2529 COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
2530 const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
2531 (const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);
2533 gc.EHClauseObj = (EXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);
2535 gc.EHClauseObj->m_flags = ehClause->GetFlags();
2536 gc.EHClauseObj->m_tryOffset = ehClause->GetTryOffset();
2537 gc.EHClauseObj->m_tryLength = ehClause->GetTryLength();
2538 gc.EHClauseObj->m_handlerOffset = ehClause->GetHandlerOffset();
2539 gc.EHClauseObj->m_handlerLength = ehClause->GetHandlerLength();
2541 if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
2542 gc.EHClauseObj->m_catchToken = ehClause->GetClassToken();
2544 gc.EHClauseObj->m_filterOffset = ehClause->GetFilterOffset();
2546 gc.MethodBodyObj->m_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
2547 SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->m_methodBody), (OBJECTREF)gc.MethodBodyObj, GetAppDomain());
2550 if (header.LocalVarSig != NULL)
2552 SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
2553 MetaSig metaSig(header.LocalVarSig,
2554 header.cbLocalVarSig,
2557 MetaSig::sigLocalVars);
2558 INT32 cLocals = metaSig.NumFixedArgs();
2559 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2560 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_localVariables, gc.TempArray, GetAppDomain());
2562 for (INT32 i = 0; i < cLocals; i ++)
2564 gc.LocalVariableInfoObj = (LOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);
2566 gc.LocalVariableInfoObj->m_localIndex = i;
2570 CorElementType eType;
2571 IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
2572 if (ELEMENT_TYPE_PINNED == eType)
2573 gc.LocalVariableInfoObj->m_bIsPinned = TRUE;
2575 TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
2576 OBJECTREF refLocalType = tempType.GetManagedClassObject();
2577 gc.LocalVariableInfoObj->SetType(refLocalType);
2578 gc.MethodBodyObj->m_localVariables->SetAt(i, (OBJECTREF) gc.LocalVariableInfoObj);
2584 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2585 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_localVariables, gc.TempArray, GetAppDomain());
2589 HELPER_METHOD_FRAME_END();
2591 return (MethodBody*)OBJECTREFToObject(gc.MethodBodyObj);
2595 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsConstructor, MethodDesc *pMethod)
2599 PRECONDITION(CheckPointer(pMethod));
2604 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2605 ret = (BOOL)pMethod->IsClassConstructorOrCtor();
2606 END_SO_INTOLERANT_CODE;
2607 FC_RETURN_BOOL(ret);
2611 FCIMPL1(Object*, RuntimeMethodHandle::GetLoaderAllocator, MethodDesc *pMethod)
2618 OBJECTREF loaderAllocator = NULL;
2621 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2623 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(loaderAllocator);
2625 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2626 loaderAllocator = pLoaderAllocator->GetExposedObject();
2628 HELPER_METHOD_FRAME_END();
2630 return OBJECTREFToObject(loaderAllocator);
2634 //*********************************************************************************************
2635 //*********************************************************************************************
2636 //*********************************************************************************************
2638 FCIMPL1(StringObject*, RuntimeFieldHandle::GetName, ReflectFieldObject *pFieldUNSAFE) {
2644 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2646 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2648 FieldDesc *pField = refField->GetField();
2650 STRINGREF refString = NULL;
2651 HELPER_METHOD_FRAME_BEGIN_RET_1(refField);
2653 refString = StringObject::NewString(pField->GetName());
2655 HELPER_METHOD_FRAME_END();
2656 return (StringObject*)OBJECTREFToObject(refString);
2660 FCIMPL1(LPCUTF8, RuntimeFieldHandle::GetUtf8Name, FieldDesc *pField) {
2663 PRECONDITION(CheckPointer(pField));
2667 LPCUTF8 szFieldName;
2669 if (FAILED(pField->GetName_NoThrow(&szFieldName)))
2671 FCThrow(kBadImageFormatException);
2677 FCIMPL2(FC_BOOL_RET, RuntimeFieldHandle::MatchesNameHash, FieldDesc * pField, ULONG hash)
2681 FC_RETURN_BOOL(pField->MightHaveName(hash));
2685 FCIMPL1(INT32, RuntimeFieldHandle::GetAttributes, FieldDesc *pField) {
2692 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2695 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2696 ret = (INT32)pField->GetAttributes();
2697 END_SO_INTOLERANT_CODE;
2702 FCIMPL1(ReflectClassBaseObject*, RuntimeFieldHandle::GetApproxDeclaringType, FieldDesc *pField) {
2709 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2711 TypeHandle th = TypeHandle(pField->GetApproxEnclosingMethodTable()); // <REVISIT_TODO> this needs to be checked - see bug 184355 </REVISIT_TODO>
2712 RETURN_CLASS_OBJECT(th, NULL);
2716 FCIMPL1(INT32, RuntimeFieldHandle::GetToken, ReflectFieldObject *pFieldUNSAFE) {
2722 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2724 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2726 FieldDesc *pField = refField->GetField();
2728 INT32 tkFieldDef = (INT32)pField->GetMemberDef();
2729 _ASSERTE(!IsNilToken(tkFieldDef) || tkFieldDef == mdFieldDefNil);
2734 FCIMPL2(FieldDesc*, RuntimeFieldHandle::GetStaticFieldForGenericType, FieldDesc *pField, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2741 REFLECTCLASSBASEREF refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2743 if ((refDeclaringType == NULL) || (pField == NULL))
2744 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2746 TypeHandle declaringType = refDeclaringType->GetType();
2749 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2750 if (declaringType.IsTypeDesc())
2751 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2752 MethodTable *pMT = declaringType.AsMethodTable();
2754 _ASSERTE(pField->IsStatic());
2755 if (pMT->HasGenericsStaticsInfo())
2756 pField = pMT->GetFieldDescByIndex(pField->GetApproxEnclosingMethodTable()->GetIndexForFieldDesc(pField));
2757 _ASSERTE(!pField->IsSharedByGenericInstantiations());
2758 _ASSERTE(pField->GetEnclosingMethodTable() == pMT);
2764 FCIMPL1(ReflectModuleBaseObject*, AssemblyHandle::GetManifestModule, AssemblyBaseObject* pAssemblyUNSAFE) {
2767 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2769 if (refAssembly == NULL)
2770 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2772 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2773 Assembly* currentAssembly = pAssembly->GetCurrentAssembly();
2775 if (currentAssembly == NULL)
2778 Module *pModule = currentAssembly->GetManifestModule();
2779 DomainFile * pDomainFile = pModule->FindDomainFile(GetAppDomain());
2784 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2785 orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2786 if (orModule == NULL)
2787 orModule = pModule->GetExposedObject();
2789 OBJECTREF orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2790 if (orModule != NULL)
2791 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2793 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2794 orModule = pModule->GetExposedObject();
2797 HELPER_METHOD_FRAME_END();
2798 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2803 FCIMPL1(INT32, AssemblyHandle::GetToken, AssemblyBaseObject* pAssemblyUNSAFE) {
2806 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2808 if (refAssembly == NULL)
2809 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2811 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2812 mdAssembly token = mdAssemblyNil;
2814 IMDInternalImport *mdImport = pAssembly->GetCurrentAssembly()->GetManifestImport();
2818 if (FAILED(mdImport->GetAssemblyFromScope(&token)))
2820 FCThrow(kBadImageFormatException);
2829 void QCALLTYPE ModuleHandle::GetPEKind(QCall::ModuleHandle pModule, DWORD* pdwPEKind, DWORD* pdwMachine)
2834 pModule->GetFile()->GetPEKindAndMachine(pdwPEKind, pdwMachine);
2838 FCIMPL1(INT32, ModuleHandle::GetMDStreamVersion, ReflectModuleBaseObject * pModuleUNSAFE)
2842 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2844 if (refModule == NULL)
2845 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2847 Module *pModule = refModule->GetModule();
2849 if (pModule->IsResource())
2852 return pModule->GetMDImport()->GetMetadataStreamVersion();
2856 void QCALLTYPE ModuleHandle::GetModuleType(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
2860 TypeHandle globalTypeHandle = TypeHandle();
2866 globalTypeHandle = TypeHandle(pModule->GetGlobalMethodTable());
2868 EX_SWALLOW_NONTRANSIENT;
2870 if (!globalTypeHandle.IsNull())
2873 retType.Set(globalTypeHandle.GetManagedClassObject());
2881 FCIMPL1(INT32, ModuleHandle::GetToken, ReflectModuleBaseObject * pModuleUNSAFE) {
2887 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2889 if (refModule == NULL)
2890 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2892 Module *pModule = refModule->GetModule();
2894 if (pModule->IsResource())
2897 return pModule->GetMDImport()->GetModuleFromScope();
2901 FCIMPL1(IMDInternalImport*, ModuleHandle::GetMetadataImport, ReflectModuleBaseObject * pModuleUNSAFE)
2905 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2907 if (refModule == NULL)
2908 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2910 Module *pModule = refModule->GetModule();
2912 if (pModule->IsResource())
2915 return pModule->GetMDImport();
2919 BOOL QCALLTYPE ModuleHandle::ContainsPropertyMatchingHash(QCall::ModuleHandle pModule, INT32 tkProperty, ULONG hash)
2923 BOOL fContains = TRUE;
2927 fContains = pModule->MightContainMatchingProperty(tkProperty, hash);
2934 void QCALLTYPE ModuleHandle::ResolveType(QCall::ModuleHandle pModule, INT32 tkType, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retType)
2938 TypeHandle typeHandle;
2942 _ASSERTE(!IsNilToken(tkType));
2944 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2945 typeHandle = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, tkType, &typeContext,
2946 ClassLoader::ThrowIfNotFound,
2947 ClassLoader::PermitUninstDefOrRef);
2950 retType.Set(typeHandle.GetManagedClassObject());
2957 MethodDesc *QCALLTYPE ModuleHandle::ResolveMethod(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount)
2961 MethodDesc* pMD = NULL;
2965 _ASSERTE(!IsNilToken(tkMemberRef));
2967 BOOL strictMetadataChecks = (TypeFromToken(tkMemberRef) == mdtMethodSpec);
2969 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2970 pMD = MemberLoader::GetMethodDescFromMemberDefOrRefOrSpec(pModule, tkMemberRef, &typeContext, strictMetadataChecks, FALSE);
2972 // This will get us the instantiating or unboxing stub if needed
2973 pMD = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMD, pMD->GetMethodTable(), pMD->GetMethodInstantiation());
2980 void QCALLTYPE ModuleHandle::ResolveField(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retField)
2984 FieldDesc* pField = NULL;
2988 _ASSERTE(!IsNilToken(tkMemberRef));
2990 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2991 pField = MemberLoader::GetFieldDescFromMemberDefOrRef(pModule, tkMemberRef, &typeContext, FALSE);
2993 retField.Set(pField->GetStubFieldInfo());
3000 void QCALLTYPE ModuleHandle::GetAssembly(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retAssembly)
3004 DomainAssembly *pAssembly = NULL;
3007 pAssembly = pModule->GetDomainAssembly();
3010 retAssembly.Set(pAssembly->GetExposedAssemblyObject());
3016 FCIMPL5(ReflectMethodObject*, ModuleHandle::GetDynamicMethod, ReflectMethodObject *pMethodUNSAFE, ReflectModuleBaseObject *pModuleUNSAFE, StringObject *name, U1Array *sig, Object *resolver) {
3019 PRECONDITION(CheckPointer(name));
3020 PRECONDITION(CheckPointer(sig));
3024 DynamicMethodDesc *pNewMD = NULL;
3029 OBJECTREF resolverRef;
3030 OBJECTREF methodRef;
3031 REFLECTMETHODREF retMethod;
3032 REFLECTMODULEBASEREF refModule;
3034 gc.nameRef = (STRINGREF)name;
3035 gc.resolverRef = (OBJECTREF)resolver;
3036 gc.methodRef = ObjectToOBJECTREF(pMethodUNSAFE);
3037 gc.retMethod = NULL;
3038 gc.refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
3040 if (gc.refModule == NULL)
3041 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
3043 Module *pModule = gc.refModule->GetModule();
3045 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
3047 DomainFile *pDomainModule = pModule->GetDomainFile();
3049 U1ARRAYREF dataArray = (U1ARRAYREF)sig;
3050 DWORD sigSize = dataArray->GetNumComponents();
3051 NewHolder<BYTE> pSig(new BYTE[sigSize]);
3052 memcpy(pSig, dataArray->GetDataPtr(), sigSize);
3054 DWORD length = gc.nameRef->GetStringLength();
3055 NewArrayHolder<char> pName(new char[(length + 1) * 2]);
3057 length = WszWideCharToMultiByte(CP_UTF8, 0, gc.nameRef->GetBuffer(), length, pName, (length + 1) * 2 - sizeof(char), NULL, NULL);
3059 pName[length / sizeof(char)] = '\0';
3061 DynamicMethodTable *pMTForDynamicMethods = pDomainModule->GetDynamicMethodTable();
3062 pNewMD = pMTForDynamicMethods->GetDynamicMethod(pSig, sigSize, pName);
3063 _ASSERTE(pNewMD != NULL);
3064 // pNewMD now owns pSig and pName.
3065 pSig.SuppressRelease();
3066 pName.SuppressRelease();
3068 // create a handle to hold the resolver objectref
3069 OBJECTHANDLE resolverHandle = pDomainModule->GetAppDomain()->CreateLongWeakHandle(gc.resolverRef);
3070 pNewMD->GetLCGMethodResolver()->SetManagedResolver(resolverHandle);
3071 gc.retMethod = pNewMD->GetStubMethodInfo();
3072 gc.retMethod->SetKeepAlive(gc.resolverRef);
3074 LoaderAllocator *pLoaderAllocator = pModule->GetLoaderAllocator();
3076 if (pLoaderAllocator->IsCollectible())
3077 pLoaderAllocator->AddReference();
3079 HELPER_METHOD_FRAME_END();
3081 return (ReflectMethodObject*)OBJECTREFToObject(gc.retMethod);
3085 void QCALLTYPE RuntimeMethodHandle::GetCallerType(QCall::StackCrawlMarkHandle pStackMark, QCall::ObjectHandleOnStack retType)
3091 MethodTable *pMT = NULL;
3093 pMT = SystemDomain::GetCallersType(pStackMark);
3096 retType.Set(pMT->GetManagedClassObject());