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) {
1022 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1024 if (refType == NULL)
1025 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1027 TypeHandle typeHandle = refType->GetType();
1029 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1032 ret = IsComObjectClass(typeHandle);
1034 ret = IsComWrapperClass(typeHandle);
1036 HELPER_METHOD_FRAME_END();
1038 FC_RETURN_BOOL(ret);
1042 FCIMPL1(LPCUTF8, RuntimeTypeHandle::GetUtf8Name, ReflectClassBaseObject* pTypeUNSAFE) {
1048 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1050 if (refType == NULL)
1051 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1053 TypeHandle typeHandle = refType->GetType();
1054 INT32 tkTypeDef = mdTypeDefNil;
1055 LPCUTF8 szName = NULL;
1057 if (typeHandle.IsGenericVariable())
1058 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1060 if (typeHandle.IsTypeDesc())
1061 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1063 MethodTable* pMT= typeHandle.GetMethodTable();
1066 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1068 tkTypeDef = (INT32)pMT->GetCl();
1070 if (IsNilToken(tkTypeDef))
1071 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1073 if (FAILED(pMT->GetMDImport()->GetNameOfTypeDef(tkTypeDef, &szName, NULL)))
1075 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1078 _ASSERTE(CheckPointer(szName, NULL_OK));
1084 FCIMPL1(INT32, RuntimeTypeHandle::GetToken, ReflectClassBaseObject *pTypeUNSAFE) {
1090 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1092 if (refType == NULL)
1093 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1095 TypeHandle typeHandle = refType->GetType();
1097 if (typeHandle.IsTypeDesc())
1099 if (typeHandle.IsGenericVariable())
1101 INT32 tkTypeDef = typeHandle.AsGenericVariable()->GetToken();
1103 _ASSERTE(!IsNilToken(tkTypeDef) && TypeFromToken(tkTypeDef) == mdtGenericParam);
1108 return mdTypeDefNil;
1111 return (INT32)typeHandle.AsMethodTable()->GetCl();
1115 PVOID QCALLTYPE RuntimeTypeHandle::GetGCHandle(EnregisteredTypeHandle pTypeHandle, INT32 handleType)
1119 OBJECTHANDLE objHandle = NULL;
1125 TypeHandle th = TypeHandle::FromPtr(pTypeHandle);
1126 assert(handleType >= HNDTYPE_WEAK_SHORT && handleType <= HNDTYPE_WEAK_WINRT);
1127 objHandle = th.GetDomain()->CreateTypedHandle(NULL, static_cast<HandleType>(handleType));
1128 th.GetLoaderAllocator()->RegisterHandleForCleanup(objHandle);
1135 void QCALLTYPE RuntimeTypeHandle::VerifyInterfaceIsImplemented(EnregisteredTypeHandle pTypeHandle, EnregisteredTypeHandle pIFaceHandle)
1141 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1142 TypeHandle ifaceHandle = TypeHandle::FromPtr(pIFaceHandle);
1144 if (typeHandle.IsGenericVariable())
1145 COMPlusThrow(kArgumentException, W("Arg_InvalidHandle"));
1147 if (typeHandle.IsTypeDesc()) {
1148 if (!typeHandle.IsArray())
1149 COMPlusThrow(kArgumentException, W("Arg_NotFoundIFace"));
1152 if (typeHandle.IsInterface())
1153 COMPlusThrow(kArgumentException, W("Argument_InterfaceMap"));
1155 if (!ifaceHandle.IsInterface())
1156 COMPlusThrow(kArgumentException, W("Arg_MustBeInterface"));
1158 // First try the cheap check, which amounts to iterating the interface map looking for
1159 // the ifaceHandle MethodTable.
1160 if (!typeHandle.GetMethodTable()->ImplementsInterface(ifaceHandle.AsMethodTable()))
1161 { // If the cheap check fails, try the more expensive but complete check.
1162 if (!typeHandle.CanCastTo(ifaceHandle))
1163 { // If the complete check fails, we're certain that this type
1164 // does not implement the interface specified.
1165 COMPlusThrow(kArgumentException, W("Arg_NotFoundIFace"));
1172 MethodDesc* QCALLTYPE RuntimeTypeHandle::GetInterfaceMethodImplementation(EnregisteredTypeHandle pTypeHandle, EnregisteredTypeHandle pOwner, MethodDesc * pMD)
1176 MethodDesc* pResult = nullptr;
1180 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1181 TypeHandle thOwnerOfMD = TypeHandle::FromPtr(pOwner);
1183 // Ok to have INVALID_SLOT in the case where abstract class does not implement an interface method.
1184 // This case can not be reproed using C# "implements" all interface methods
1185 // with at least an abstract method. b19897_GetInterfaceMap_Abstract.exe tests this case.
1186 //@TODO:STUBDISPATCH: Don't need to track down the implementation, just the declaration, and this can
1187 //@TODO: be done faster - just need to make a function FindDispatchDecl.
1188 DispatchSlot slot(typeHandle.GetMethodTable()->FindDispatchSlotForInterfaceMD(thOwnerOfMD, pMD));
1190 pResult = slot.GetMethodDesc();
1197 void QCALLTYPE RuntimeTypeHandle::GetDefaultConstructor(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retMethod)
1203 MethodDesc* pCtor = NULL;
1205 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1207 if (!typeHandle.IsTypeDesc())
1209 MethodTable* pMethodTable = typeHandle.AsMethodTable();
1210 if (pMethodTable->HasDefaultConstructor())
1211 pCtor = pMethodTable->GetDefaultConstructor();
1217 retMethod.Set(pCtor->GetStubMethodInfo());
1224 FCIMPL1(ReflectMethodObject*, RuntimeTypeHandle::GetDeclaringMethod, ReflectClassBaseObject *pTypeUNSAFE) {
1230 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1232 if (refType == NULL)
1233 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1235 TypeHandle typeHandle = refType->GetType();;
1237 if (!typeHandle.IsTypeDesc())
1240 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
1241 mdToken defToken = pGenericVariable->GetTypeOrMethodDef();
1242 if (TypeFromToken(defToken) != mdtMethodDef)
1245 REFLECTMETHODREF pRet = NULL;
1246 HELPER_METHOD_FRAME_BEGIN_RET_0();
1247 MethodDesc * pMD = pGenericVariable->LoadOwnerMethod();
1248 pMD->CheckRestore();
1249 pRet = pMD->GetStubMethodInfo();
1250 HELPER_METHOD_FRAME_END();
1252 return (ReflectMethodObject*)OBJECTREFToObject(pRet);
1256 FCIMPL1(ReflectClassBaseObject*, RuntimeTypeHandle::GetDeclaringType, ReflectClassBaseObject *pTypeUNSAFE) {
1262 TypeHandle retTypeHandle;
1264 BOOL fThrowException = FALSE;
1265 LPCWSTR argName = W("Arg_InvalidHandle");
1266 RuntimeExceptionKind reKind = kArgumentNullException;
1268 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1270 if (refType == NULL)
1271 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1273 TypeHandle typeHandle = refType->GetType();
1275 MethodTable* pMT = NULL;
1276 mdTypeDef tkTypeDef = mdTokenNil;
1278 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1279 if (typeHandle.IsTypeDesc()) {
1281 if (typeHandle.IsGenericVariable()) {
1282 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
1283 mdToken defToken = pGenericVariable->GetTypeOrMethodDef();
1285 // Try the fast way first (if the declaring type has been loaded already).
1286 if (TypeFromToken(defToken) == mdtMethodDef)
1288 MethodDesc * retMethod = pGenericVariable->GetModule()->LookupMethodDef(defToken);
1289 if (retMethod != NULL)
1290 retTypeHandle = retMethod->GetMethodTable();
1294 retTypeHandle = pGenericVariable->GetModule()->LookupTypeDef(defToken);
1297 if (!retTypeHandle.IsNull() && retTypeHandle.IsFullyLoaded())
1300 // OK, need to go the slow way and load the type first.
1301 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1303 if (TypeFromToken(defToken) == mdtMethodDef)
1305 retTypeHandle = pGenericVariable->LoadOwnerMethod()->GetMethodTable();
1309 retTypeHandle = pGenericVariable->LoadOwnerType();
1311 retTypeHandle.CheckRestore();
1313 HELPER_METHOD_FRAME_END();
1316 if (!typeHandle.IsArray())
1318 retTypeHandle = TypeHandle();
1323 pMT = typeHandle.GetMethodTable();
1327 fThrowException = TRUE;
1331 if(!pMT->GetClass()->IsNested())
1333 retTypeHandle = TypeHandle();
1337 tkTypeDef = pMT->GetCl();
1339 if (FAILED(typeHandle.GetModule()->GetMDImport()->GetNestedClassProps(tkTypeDef, &tkTypeDef)))
1341 fThrowException = TRUE;
1342 reKind = kBadImageFormatException;
1347 // Try the fast way first (if the declaring type has been loaded already).
1348 retTypeHandle = typeHandle.GetModule()->LookupTypeDef(tkTypeDef);
1349 if (retTypeHandle.IsNull())
1351 // OK, need to go the slow way and load the type first.
1352 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1354 retTypeHandle = ClassLoader::LoadTypeDefThrowing(typeHandle.GetModule(), tkTypeDef,
1355 ClassLoader::ThrowIfNotFound,
1356 ClassLoader::PermitUninstDefOrRef);
1358 HELPER_METHOD_FRAME_END();
1362 END_SO_INTOLERANT_CODE;
1364 if (fThrowException)
1366 FCThrowRes(reKind, argName);
1369 RETURN_CLASS_OBJECT(retTypeHandle, refType);
1373 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CanCastTo, ReflectClassBaseObject *pTypeUNSAFE, ReflectClassBaseObject *pTargetUNSAFE) {
1380 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1381 REFLECTCLASSBASEREF refTarget = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTargetUNSAFE);
1383 if ((refType == NULL) || (refTarget == NULL))
1384 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1386 TypeHandle fromHandle = refType->GetType();
1387 TypeHandle toHandle = refTarget->GetType();
1391 TypeHandle::CastResult r = fromHandle.CanCastToNoGC(toHandle);
1392 if (r == TypeHandle::MaybeCast)
1394 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refTarget);
1395 iRetVal = fromHandle.CanCastTo(toHandle);
1396 HELPER_METHOD_FRAME_END();
1400 iRetVal = (r == TypeHandle::CanCast);
1403 // We allow T to be cast to Nullable<T>
1404 if (!iRetVal && Nullable::IsNullableType(toHandle) && !fromHandle.IsTypeDesc())
1406 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refTarget);
1407 if (Nullable::IsNullableForType(toHandle, fromHandle.AsMethodTable()))
1411 HELPER_METHOD_FRAME_END();
1414 FC_RETURN_BOOL(iRetVal);
1418 void QCALLTYPE RuntimeTypeHandle::GetTypeByNameUsingCARules(LPCWSTR pwzClassName, QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
1422 TypeHandle typeHandle;
1427 COMPlusThrowArgumentNull(W("className"),W("ArgumentNull_String"));
1429 typeHandle = TypeName::GetTypeUsingCASearchRules(pwzClassName, pModule->GetAssembly());
1432 retType.Set(typeHandle.GetManagedClassObject());
1439 void QCALLTYPE RuntimeTypeHandle::GetTypeByName(LPCWSTR pwzClassName, BOOL bThrowOnError, BOOL bIgnoreCase, BOOL bReflectionOnly,
1440 QCall::StackCrawlMarkHandle pStackMark,
1441 ICLRPrivBinder * pPrivHostBinder,
1442 BOOL bLoadTypeFromPartialNameHack, QCall::ObjectHandleOnStack retType,
1443 QCall::ObjectHandleOnStack keepAlive)
1447 TypeHandle typeHandle;
1452 COMPlusThrowArgumentNull(W("className"),W("ArgumentNull_String"));
1455 typeHandle = TypeName::GetTypeManaged(pwzClassName, NULL, bThrowOnError, bIgnoreCase, bReflectionOnly, /*bProhibitAsmQualifiedName =*/ FALSE, pStackMark,
1456 bLoadTypeFromPartialNameHack, (OBJECTREF*)keepAlive.m_ppObject,
1460 if (!typeHandle.IsNull())
1463 retType.Set(typeHandle.GetManagedClassObject());
1471 FCIMPL6(FC_BOOL_RET, RuntimeTypeHandle::SatisfiesConstraints, PTR_ReflectClassBaseObject pParamTypeUNSAFE, TypeHandle *typeContextArgs, INT32 typeContextCount, TypeHandle *methodContextArgs, INT32 methodContextCount, PTR_ReflectClassBaseObject pArgumentTypeUNSAFE);
1475 PRECONDITION(CheckPointer(typeContextArgs, NULL_OK));
1476 PRECONDITION(CheckPointer(methodContextArgs, NULL_OK));
1480 REFLECTCLASSBASEREF refParamType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pParamTypeUNSAFE);
1481 REFLECTCLASSBASEREF refArgumentType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pArgumentTypeUNSAFE);
1483 TypeHandle thGenericParameter = refParamType->GetType();
1484 TypeHandle thGenericArgument = refArgumentType->GetType();
1485 BOOL bResult = FALSE;
1486 SigTypeContext typeContext;
1488 Instantiation classInst;
1489 Instantiation methodInst;
1491 if (typeContextArgs != NULL)
1493 classInst = Instantiation(typeContextArgs, typeContextCount);
1496 if (methodContextArgs != NULL)
1498 methodInst = Instantiation(methodContextArgs, methodContextCount);
1501 SigTypeContext::InitTypeContext(classInst, methodInst, &typeContext);
1503 HELPER_METHOD_FRAME_BEGIN_RET_2(refParamType, refArgumentType);
1505 bResult = thGenericParameter.AsGenericVariable()->SatisfiesConstraints(&typeContext, thGenericArgument);
1507 HELPER_METHOD_FRAME_END();
1509 FC_RETURN_BOOL(bResult);
1513 void QCALLTYPE RuntimeTypeHandle::GetInstantiation(EnregisteredTypeHandle pType, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
1519 TypeHandle typeHandle = TypeHandle::FromPtr(pType);
1520 Instantiation inst = typeHandle.GetInstantiation();
1522 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
1528 void QCALLTYPE RuntimeTypeHandle::MakeArray(EnregisteredTypeHandle pTypeHandle, INT32 rank, QCall::ObjectHandleOnStack retType)
1532 TypeHandle arrayHandle;
1535 arrayHandle = TypeHandle::FromPtr(pTypeHandle).MakeArray(rank);
1537 retType.Set(arrayHandle.GetManagedClassObject());
1543 void QCALLTYPE RuntimeTypeHandle::MakeSZArray(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1547 TypeHandle arrayHandle;
1550 arrayHandle = TypeHandle::FromPtr(pTypeHandle).MakeSZArray();
1552 retType.Set(arrayHandle.GetManagedClassObject());
1558 void QCALLTYPE RuntimeTypeHandle::MakePointer(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1562 TypeHandle pointerHandle;
1565 pointerHandle = TypeHandle::FromPtr(pTypeHandle).MakePointer();
1567 retType.Set(pointerHandle.GetManagedClassObject());
1573 void QCALLTYPE RuntimeTypeHandle::MakeByRef(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1577 TypeHandle byRefHandle;
1580 byRefHandle = TypeHandle::FromPtr(pTypeHandle).MakeByRef();
1582 retType.Set(byRefHandle.GetManagedClassObject());
1588 BOOL QCALLTYPE RuntimeTypeHandle::IsCollectible(EnregisteredTypeHandle pTypeHandle)
1592 BOOL retVal = FALSE;
1595 retVal = TypeHandle::FromPtr(pTypeHandle).GetLoaderAllocator()->IsCollectible();
1601 void QCALLTYPE RuntimeTypeHandle::Instantiate(EnregisteredTypeHandle pTypeHandle, TypeHandle * pInstArray, INT32 cInstArray, QCall::ObjectHandleOnStack retType)
1608 type = TypeHandle::FromPtr(pTypeHandle).Instantiate(Instantiation(pInstArray, cInstArray));
1610 retType.Set(type.GetManagedClassObject());
1616 void QCALLTYPE RuntimeTypeHandle::GetGenericTypeDefinition(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1624 TypeHandle genericType = TypeHandle::FromPtr(pTypeHandle);
1626 typeDef = ClassLoader::LoadTypeDefThrowing(genericType.GetModule(),
1627 genericType.GetMethodTable()->GetCl(),
1628 ClassLoader::ThrowIfNotFound,
1629 ClassLoader::PermitUninstDefOrRef);
1632 retType.Set(typeDef.GetManagedClassObject());
1639 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CompareCanonicalHandles, ReflectClassBaseObject *pLeftUNSAFE, ReflectClassBaseObject *pRightUNSAFE)
1643 REFLECTCLASSBASEREF refLeft = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pLeftUNSAFE);
1644 REFLECTCLASSBASEREF refRight = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pRightUNSAFE);
1646 if ((refLeft == NULL) || (refRight == NULL))
1647 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1649 FC_RETURN_BOOL(refLeft->GetType().GetCanonicalMethodTable() == refRight->GetType().GetCanonicalMethodTable());
1653 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::HasInstantiation, PTR_ReflectClassBaseObject pTypeUNSAFE)
1657 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1659 if (refType == NULL)
1660 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1662 FC_RETURN_BOOL(refType->GetType().HasInstantiation());
1666 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericTypeDefinition, PTR_ReflectClassBaseObject pTypeUNSAFE)
1670 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1672 if (refType == NULL)
1673 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1675 FC_RETURN_BOOL(refType->GetType().IsGenericTypeDefinition());
1679 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericVariable, PTR_ReflectClassBaseObject pTypeUNSAFE)
1683 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1685 if (refType == NULL)
1686 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1688 FC_RETURN_BOOL(refType->GetType().IsGenericVariable());
1692 FCIMPL1(INT32, RuntimeTypeHandle::GetGenericVariableIndex, PTR_ReflectClassBaseObject pTypeUNSAFE)
1696 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1698 if (refType == NULL)
1699 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1701 return (INT32)refType->GetType().AsGenericVariable()->GetIndex();
1705 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::ContainsGenericVariables, PTR_ReflectClassBaseObject pTypeUNSAFE)
1709 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1711 if (refType == NULL)
1712 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1714 FC_RETURN_BOOL(refType->GetType().ContainsGenericVariables());
1718 FCIMPL1(IMDInternalImport*, RuntimeTypeHandle::GetMetadataImport, ReflectClassBaseObject * pTypeUNSAFE)
1722 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1724 if (refType == NULL)
1725 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1727 Module *pModule = refType->GetType().GetModule();
1729 return pModule->GetMDImport();
1734 //***********************************************************************************
1735 //***********************************************************************************
1736 //***********************************************************************************
1738 void * QCALLTYPE RuntimeMethodHandle::GetFunctionPointer(MethodDesc * pMethod)
1746 funcPtr = (void*)pMethod->GetMultiCallableAddrOfCode();
1753 FCIMPL1(LPCUTF8, RuntimeMethodHandle::GetUtf8Name, MethodDesc *pMethod) {
1759 LPCUTF8 szName = NULL;
1762 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1764 szName = pMethod->GetName();
1766 _ASSERTE(CheckPointer(szName, NULL_OK));
1772 FCIMPL2(FC_BOOL_RET, RuntimeMethodHandle::MatchesNameHash, MethodDesc * pMethod, ULONG hash)
1776 FC_RETURN_BOOL(pMethod->MightHaveName(hash));
1780 FCIMPL1(StringObject*, RuntimeMethodHandle::GetName, MethodDesc *pMethod) {
1787 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1789 STRINGREF refName = NULL;
1791 HELPER_METHOD_FRAME_BEGIN_RET_0();
1792 refName = StringObject::NewString(pMethod->GetName());
1793 HELPER_METHOD_FRAME_END();
1795 return (StringObject*)OBJECTREFToObject(refName);
1799 FCIMPL1(INT32, RuntimeMethodHandle::GetAttributes, MethodDesc *pMethod) {
1806 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1809 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1810 retVal = (INT32)pMethod->GetAttrs();
1811 END_SO_INTOLERANT_CODE;
1816 FCIMPL1(INT32, RuntimeMethodHandle::GetImplAttributes, ReflectMethodObject *pMethodUNSAFE) {
1823 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1825 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
1826 INT32 attributes = 0;
1828 if (IsNilToken(pMethod->GetMemberDef()))
1831 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1833 attributes = (INT32)pMethod->GetImplAttrs();
1835 END_SO_INTOLERANT_CODE;
1842 FCIMPL1(ReflectClassBaseObject*, RuntimeMethodHandle::GetDeclaringType, MethodDesc *pMethod) {
1845 PRECONDITION(CheckPointer(pMethod));
1850 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1852 MethodTable *pMT = pMethod->GetMethodTable();
1853 TypeHandle declType(pMT);
1856 HELPER_METHOD_FRAME_BEGIN_RET_0();
1858 // Load the TypeDesc for the array type. Note the returned type is approximate, i.e.
1859 // if shared between reference array types then we will get object[] back.
1860 DWORD rank = pMT->GetRank();
1861 TypeHandle elemType = pMT->GetApproxArrayElementTypeHandle();
1862 declType = ClassLoader::LoadArrayTypeThrowing(elemType, pMT->GetInternalCorElementType(), rank);
1863 HELPER_METHOD_FRAME_END();
1865 RETURN_CLASS_OBJECT(declType, NULL);
1869 FCIMPL1(INT32, RuntimeMethodHandle::GetSlot, MethodDesc *pMethod) {
1876 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1878 return (INT32)pMethod->GetSlot();
1882 FCIMPL3(Object *, SignatureNative::GetCustomModifiers, SignatureNative* pSignatureUNSAFE,
1883 INT32 parameter, CLR_BOOL fRequired)
1892 SIGNATURENATIVEREF pSig;
1896 gc.pSig = (SIGNATURENATIVEREF)pSignatureUNSAFE;
1899 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
1902 BYTE callConv = *(BYTE*)gc.pSig->GetCorSig();
1903 SigTypeContext typeContext;
1904 gc.pSig->GetTypeContext(&typeContext);
1905 MetaSig sig(gc.pSig->GetCorSig(),
1906 gc.pSig->GetCorSigSize(),
1907 gc.pSig->GetModule(),
1909 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
1910 _ASSERTE(callConv == sig.GetCallingConventionInfo());
1912 SigPointer argument(NULL, 0);
1914 PRECONDITION(sig.GetCallingConvention() != IMAGE_CEE_CS_CALLCONV_FIELD || parameter == 1);
1918 argument = sig.GetReturnProps();
1922 for(INT32 i = 0; i < parameter; i++)
1925 argument = sig.GetArgProps();
1928 //if (parameter < 0 || parameter > (INT32)sig.NumFixedArgs())
1929 // FCThrowResVoid(kArgumentNullException, W("Arg_ArgumentOutOfRangeException"));
1931 SigPointer sp = argument;
1932 Module* pModule = sig.GetModule();
1934 CorElementType cmodType;
1936 CorElementType cmodTypeExpected = fRequired ? ELEMENT_TYPE_CMOD_REQD : ELEMENT_TYPE_CMOD_OPT;
1938 // Discover the number of required and optional custom modifiers.
1942 IfFailThrow(sp.GetByte(&data));
1943 cmodType = (CorElementType)data;
1945 if (cmodType == ELEMENT_TYPE_CMOD_REQD || cmodType == ELEMENT_TYPE_CMOD_OPT)
1947 if (cmodType == cmodTypeExpected)
1952 else if (cmodType != ELEMENT_TYPE_SENTINEL)
1957 IfFailThrow(sp.GetToken(NULL));
1960 // Reset sp and populate the arrays for the required and optional custom
1961 // modifiers now that we know how long they should be.
1964 MethodTable *pMT = MscorlibBinder::GetClass(CLASS__TYPE);
1965 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pMT), ELEMENT_TYPE_SZARRAY);
1967 gc.retVal = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &cMods, 1);
1972 IfFailThrow(sp.GetByte(&data));
1973 cmodType = (CorElementType)data;
1976 IfFailThrow(sp.GetToken(&token));
1978 if (cmodType == cmodTypeExpected)
1980 TypeHandle th = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, token,
1982 ClassLoader::ThrowIfNotFound,
1983 ClassLoader::FailIfUninstDefOrRef);
1985 OBJECTREF refType = th.GetManagedClassObject();
1986 gc.retVal->SetAt(--cMods, refType);
1990 HELPER_METHOD_FRAME_END();
1992 return OBJECTREFToObject(gc.retVal);
1996 FCIMPL1(INT32, RuntimeMethodHandle::GetMethodDef, ReflectMethodObject *pMethodUNSAFE) {
2003 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2005 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2007 if (pMethod->HasMethodInstantiation())
2009 HELPER_METHOD_FRAME_BEGIN_RET_1(pMethodUNSAFE);
2011 pMethod = pMethod->StripMethodInstantiation();
2013 HELPER_METHOD_FRAME_END();
2016 INT32 tkMethodDef = (INT32)pMethod->GetMemberDef();
2017 _ASSERTE(TypeFromToken(tkMethodDef) == mdtMethodDef);
2019 if (IsNilToken(tkMethodDef) || TypeFromToken(tkMethodDef) != mdtMethodDef)
2020 return mdMethodDefNil;
2026 FCIMPL6(void, SignatureNative::GetSignature,
2027 SignatureNative* pSignatureNativeUNSAFE,
2028 PCCOR_SIGNATURE pCorSig, DWORD cCorSig,
2029 FieldDesc *pFieldDesc, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE) {
2032 PRECONDITION(pDeclaringTypeUNSAFE || pMethodUNSAFE->GetMethod()->IsDynamicMethod());
2033 PRECONDITION(CheckPointer(pCorSig, NULL_OK));
2034 PRECONDITION(CheckPointer(pMethodUNSAFE, NULL_OK));
2035 PRECONDITION(CheckPointer(pFieldDesc, NULL_OK));
2041 REFLECTCLASSBASEREF refDeclaringType;
2042 REFLECTMETHODREF refMethod;
2043 SIGNATURENATIVEREF pSig;
2046 gc.refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2047 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2048 gc.pSig = (SIGNATURENATIVEREF)pSignatureNativeUNSAFE;
2050 MethodDesc *pMethod;
2051 TypeHandle declType;
2053 if (gc.refDeclaringType == NULL)
2055 // for dynamic method, see precondition
2056 pMethod = gc.refMethod->GetMethod();
2057 declType = pMethod->GetMethodTable();
2061 pMethod = gc.refMethod != NULL ? gc.refMethod->GetMethod() : NULL;
2062 declType = gc.refDeclaringType->GetType();
2065 HELPER_METHOD_FRAME_BEGIN_PROTECT(gc);
2067 Module* pModule = declType.GetModule();
2071 pMethod->GetSig(&pCorSig, &cCorSig);
2072 if (pMethod->GetClassification() == mcInstantiated)
2074 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2075 if (pLoaderAllocator->IsCollectible())
2076 gc.pSig->SetKeepAlive(pLoaderAllocator->GetExposedObject());
2079 else if (pFieldDesc)
2080 pFieldDesc->GetSig(&pCorSig, &cCorSig);
2082 gc.pSig->m_sig = pCorSig;
2083 gc.pSig->m_cSig = cCorSig;
2084 gc.pSig->m_pMethod = pMethod;
2086 REFLECTCLASSBASEREF refDeclType = (REFLECTCLASSBASEREF)declType.GetManagedClassObject();
2087 gc.pSig->SetDeclaringType(refDeclType);
2089 PREFIX_ASSUME(pCorSig!= NULL);
2090 BYTE callConv = *(BYTE*)pCorSig;
2091 SigTypeContext typeContext;
2093 SigTypeContext::InitTypeContext(
2094 pMethod, declType.GetClassOrArrayInstantiation(), pMethod->LoadMethodInstantiation(), &typeContext);
2096 SigTypeContext::InitTypeContext(declType, &typeContext);
2097 MetaSig msig(pCorSig, cCorSig, pModule, &typeContext,
2098 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
2100 if (callConv == IMAGE_CEE_CS_CALLCONV_FIELD)
2102 msig.NextArgNormalized();
2104 OBJECTREF refRetType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2105 gc.pSig->SetReturnType(refRetType);
2109 gc.pSig->SetCallingConvention(msig.GetCallingConventionInfo());
2111 OBJECTREF refRetType = msig.GetRetTypeHandleThrowing().GetManagedClassObject();
2112 gc.pSig->SetReturnType(refRetType);
2114 INT32 nArgs = msig.NumFixedArgs();
2115 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(g_pRuntimeTypeClass), ELEMENT_TYPE_SZARRAY);
2117 PTRARRAYREF ptrArrayarguments = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &nArgs, 1);
2118 gc.pSig->SetArgumentArray(ptrArrayarguments);
2120 for (INT32 i = 0; i < nArgs; i++)
2124 OBJECTREF refArgType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2125 gc.pSig->SetArgument(i, refArgType);
2128 _ASSERTE(gc.pSig->m_returnType != NULL);
2131 HELPER_METHOD_FRAME_END();
2135 FCIMPL2(FC_BOOL_RET, SignatureNative::CompareSig, SignatureNative* pLhsUNSAFE, SignatureNative* pRhsUNSAFE)
2143 SIGNATURENATIVEREF pLhs;
2144 SIGNATURENATIVEREF pRhs;
2147 gc.pLhs = (SIGNATURENATIVEREF)pLhsUNSAFE;
2148 gc.pRhs = (SIGNATURENATIVEREF)pRhsUNSAFE;
2150 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2152 ret = MetaSig::CompareMethodSigs(
2153 gc.pLhs->GetCorSig(), gc.pLhs->GetCorSigSize(), gc.pLhs->GetModule(), NULL,
2154 gc.pRhs->GetCorSig(), gc.pRhs->GetCorSigSize(), gc.pRhs->GetModule(), NULL);
2156 HELPER_METHOD_FRAME_END();
2157 FC_RETURN_BOOL(ret);
2161 void QCALLTYPE RuntimeMethodHandle::GetMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
2166 Instantiation inst = pMethod->LoadMethodInstantiation();
2169 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
2175 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::HasMethodInstantiation, MethodDesc * pMethod)
2179 FC_RETURN_BOOL(pMethod->HasMethodInstantiation());
2183 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsGenericMethodDefinition, MethodDesc * pMethod)
2187 FC_RETURN_BOOL(pMethod->IsGenericMethodDefinition());
2191 FCIMPL1(INT32, RuntimeMethodHandle::GetGenericParameterCount, MethodDesc * pMethod)
2195 return pMethod->GetNumGenericMethodArgs();
2199 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsDynamicMethod, MethodDesc * pMethod)
2203 FC_RETURN_BOOL(pMethod->IsNoMetadata());
2207 FCIMPL1(Object*, RuntimeMethodHandle::GetResolver, MethodDesc * pMethod)
2212 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2214 OBJECTREF resolver = NULL;
2215 if (pMethod->IsLCGMethod())
2217 resolver = pMethod->AsDynamicMethodDesc()->GetLCGMethodResolver()->GetManagedResolver();
2219 return OBJECTREFToObject(resolver);
2223 void QCALLTYPE RuntimeMethodHandle::Destroy(MethodDesc * pMethod)
2229 if (pMethod == NULL)
2230 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2232 DynamicMethodDesc* pDynamicMethodDesc = pMethod->AsDynamicMethodDesc();
2236 // Destroy should be called only if the managed part is gone.
2237 _ASSERTE(OBJECTREFToObject(pDynamicMethodDesc->GetLCGMethodResolver()->GetManagedResolver()) == NULL);
2239 // Fire Unload Dynamic Method Event here
2240 ETW::MethodLog::DynamicMethodDestroyed(pMethod);
2242 BEGIN_PIN_PROFILER(CORProfilerIsMonitoringDynamicFunctionUnloads());
2243 g_profControlBlock.pProfInterface->DynamicMethodUnloaded((FunctionID)pMethod);
2246 pDynamicMethodDesc->Destroy();
2251 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsTypicalMethodDefinition, ReflectMethodObject *pMethodUNSAFE)
2256 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2258 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2260 FC_RETURN_BOOL(pMethod->IsTypicalMethodDefinition());
2264 void QCALLTYPE RuntimeMethodHandle::GetTypicalMethodDefinition(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2272 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2275 MethodDesc *pMethodTypical = pMethod->LoadTypicalMethodDefinition();
2276 if (pMethodTypical != pMethod)
2279 refMethod.Set(pMethodTypical->GetStubMethodInfo());
2286 void QCALLTYPE RuntimeMethodHandle::StripMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2293 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2298 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2301 MethodDesc *pMethodStripped = pMethod->StripMethodInstantiation();
2302 if (pMethodStripped != pMethod)
2305 refMethod.Set(pMethodStripped->GetStubMethodInfo());
2312 // In the VM there might be more than one MethodDescs for a "method"
2313 // examples are methods on generic types which may have additional instantiating stubs
2314 // and methods on value types which may have additional unboxing stubs.
2316 // For generic methods we always hand out an instantiating stub except for a generic method definition
2317 // For non-generic methods on generic types we need an instantiating stub if it's one of the following
2318 // - static method on a generic class
2319 // - static or instance method on a generic interface
2320 // - static or instance method on a generic value type
2321 // The Reflection policy is to always hand out instantiating stubs in these cases
2323 // For methods on non-generic value types we can use either the cannonical method or the unboxing stub
2324 // The Reflection policy is to always hand out unboxing stubs if the methods are virtual methods
2325 // The reason for this is that in the current implementation of the class loader, the v-table slots for
2326 // those methods point to unboxing stubs already. Note that this is just a implementation choice
2327 // that might change in the future. But we should always keep this Reflection policy an invariant.
2329 // For virtual methods on generic value types (intersection of the two cases), reflection will hand
2330 // out an unboxing instantiating stub
2332 // GetInstantiatingStub is called to:
2333 // 1. create an InstantiatedMethodDesc for a generic method when calling BindGenericArguments() on a generic
2334 // method. In this case instArray will not be null.
2335 // 2. create an InstantiatedMethodDesc for a method in a generic class. In this case instArray will be null.
2336 // 3. create an UnboxingStub for a method in a value type. In this case instArray will be null.
2337 // For case 2 and 3, an instantiating stub or unboxing stub might not be needed in which case the original
2338 // MethodDesc is returned.
2339 FCIMPL3(MethodDesc*, RuntimeMethodHandle::GetStubIfNeeded,
2340 MethodDesc *pMethod,
2341 ReflectClassBaseObject *pTypeUNSAFE,
2342 PtrArray* instArrayUNSAFE)
2349 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2350 PTRARRAYREF instArray = (PTRARRAYREF)ObjectToOBJECTREF(instArrayUNSAFE);
2352 if (refType == NULL)
2353 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2355 TypeHandle instType = refType->GetType();
2356 MethodDesc *pNewMethod = pMethod;
2360 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
2362 if (instType.IsNull())
2363 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2365 // Perf optimization: this logic is actually duplicated in FindOrCreateAssociatedMethodDescForReflection, but since it
2366 // is the more common case it's worth the duplicate check here to avoid the helper method frame
2367 if ( instArray == NULL &&
2368 ( pMethod->HasMethodInstantiation() ||
2369 ( !instType.IsValueType() &&
2370 ( !instType.HasInstantiation() || instType.IsGenericTypeDefinition() ) ) ) )
2375 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, instArray);
2377 TypeHandle *inst = NULL;
2380 if (instArray != NULL)
2382 ntypars = instArray->GetNumComponents();
2384 size_t size = ntypars * sizeof(TypeHandle);
2385 if ((size / sizeof(TypeHandle)) != ntypars) // uint over/underflow
2386 COMPlusThrow(kArgumentException);
2387 inst = (TypeHandle*) _alloca(size);
2389 for (DWORD i = 0; i < ntypars; i++)
2391 REFLECTCLASSBASEREF instRef = (REFLECTCLASSBASEREF)instArray->GetAt(i);
2393 if (instRef == NULL)
2394 COMPlusThrowArgumentNull(W("inst"), W("ArgumentNull_ArrayElement"));
2396 inst[i] = instRef->GetType();
2400 pNewMethod = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMethod, instType, Instantiation(inst, ntypars));
2402 HELPER_METHOD_FRAME_END();
2409 FCIMPL2(MethodDesc*, RuntimeMethodHandle::GetMethodFromCanonical, MethodDesc *pMethod, ReflectClassBaseObject *pTypeUNSAFE)
2413 PRECONDITION(CheckPointer(pMethod));
2417 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2419 TypeHandle instType = refType->GetType();
2420 MethodDesc* pMDescInCanonMT = instType.GetMethodTable()->GetParallelMethodDesc(pMethod);
2422 return pMDescInCanonMT;
2427 FCIMPL2(MethodBody *, RuntimeMethodHandle::GetMethodBody, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2437 METHODBODYREF MethodBodyObj;
2438 EXCEPTIONHANDLINGCLAUSEREF EHClauseObj;
2439 LOCALVARIABLEINFOREF LocalVariableInfoObj;
2441 BASEARRAYREF TempArray;
2442 REFLECTCLASSBASEREF declaringType;
2443 REFLECTMETHODREF refMethod;
2446 gc.MethodBodyObj = NULL;
2447 gc.EHClauseObj = NULL;
2448 gc.LocalVariableInfoObj = NULL;
2450 gc.TempArray = NULL;
2451 gc.declaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2452 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2456 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2458 MethodDesc* pMethod = gc.refMethod->GetMethod();
2460 TypeHandle declaringType = gc.declaringType == NULL ? TypeHandle() : gc.declaringType->GetType();
2462 if (!pMethod->IsIL())
2465 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2467 MethodDesc *pMethodIL = pMethod;
2468 if (pMethod->IsWrapperStub())
2469 pMethodIL = pMethod->GetWrappedMethodDesc();
2471 COR_ILMETHOD* pILHeader = pMethodIL->GetILHeader();
2475 MethodTable * pExceptionHandlingClauseMT = MscorlibBinder::GetClass(CLASS__EH_CLAUSE);
2476 TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);
2478 MethodTable * pLocalVariableMT = MscorlibBinder::GetClass(CLASS__LOCAL_VARIABLE_INFO);
2479 TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);
2481 Module* pModule = pMethod->GetModule();
2482 COR_ILMETHOD_DECODER::DecoderStatus status;
2483 COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);
2485 if (status != COR_ILMETHOD_DECODER::SUCCESS)
2487 if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
2489 // Throw a verification HR
2490 COMPlusThrowHR(COR_E_VERIFICATION);
2494 COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
2498 gc.MethodBodyObj = (METHODBODYREF)AllocateObject(MscorlibBinder::GetClass(CLASS__METHOD_BODY));
2500 gc.MethodBodyObj->m_maxStackSize = header.GetMaxStack();
2501 gc.MethodBodyObj->m_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);
2504 gc.MethodBodyObj->m_localVarSigToken = header.GetLocalVarSigTok();
2506 gc.MethodBodyObj->m_localVarSigToken = 0;
2508 // Allocate the array of IL and fill it in from the method header.
2509 BYTE* pIL = const_cast<BYTE*>(header.Code);
2510 COUNT_T cIL = header.GetCodeSize();
2511 gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);
2513 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_IL, gc.U1Array, GetAppDomain());
2514 memcpyNoGCRefs(gc.MethodBodyObj->m_IL->GetDataPtr(), pIL, cIL);
2516 // Allocate the array of exception clauses.
2517 INT32 cEh = (INT32)header.EHCount();
2518 const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
2519 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thEHClauseArray, &cEh, 1);
2521 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_exceptionClauses, gc.TempArray, GetAppDomain());
2523 for (INT32 i = 0; i < cEh; i++)
2525 COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
2526 const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
2527 (const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);
2529 gc.EHClauseObj = (EXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);
2531 gc.EHClauseObj->m_flags = ehClause->GetFlags();
2532 gc.EHClauseObj->m_tryOffset = ehClause->GetTryOffset();
2533 gc.EHClauseObj->m_tryLength = ehClause->GetTryLength();
2534 gc.EHClauseObj->m_handlerOffset = ehClause->GetHandlerOffset();
2535 gc.EHClauseObj->m_handlerLength = ehClause->GetHandlerLength();
2537 if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
2538 gc.EHClauseObj->m_catchToken = ehClause->GetClassToken();
2540 gc.EHClauseObj->m_filterOffset = ehClause->GetFilterOffset();
2542 gc.MethodBodyObj->m_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
2543 SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->m_methodBody), (OBJECTREF)gc.MethodBodyObj, GetAppDomain());
2546 if (header.LocalVarSig != NULL)
2548 SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
2549 MetaSig metaSig(header.LocalVarSig,
2550 header.cbLocalVarSig,
2553 MetaSig::sigLocalVars);
2554 INT32 cLocals = metaSig.NumFixedArgs();
2555 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2556 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_localVariables, gc.TempArray, GetAppDomain());
2558 for (INT32 i = 0; i < cLocals; i ++)
2560 gc.LocalVariableInfoObj = (LOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);
2562 gc.LocalVariableInfoObj->m_localIndex = i;
2566 CorElementType eType;
2567 IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
2568 if (ELEMENT_TYPE_PINNED == eType)
2569 gc.LocalVariableInfoObj->m_bIsPinned = TRUE;
2571 TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
2572 OBJECTREF refLocalType = tempType.GetManagedClassObject();
2573 gc.LocalVariableInfoObj->SetType(refLocalType);
2574 gc.MethodBodyObj->m_localVariables->SetAt(i, (OBJECTREF) gc.LocalVariableInfoObj);
2580 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2581 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_localVariables, gc.TempArray, GetAppDomain());
2585 HELPER_METHOD_FRAME_END();
2587 return (MethodBody*)OBJECTREFToObject(gc.MethodBodyObj);
2591 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsConstructor, MethodDesc *pMethod)
2595 PRECONDITION(CheckPointer(pMethod));
2600 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2601 ret = (BOOL)pMethod->IsClassConstructorOrCtor();
2602 END_SO_INTOLERANT_CODE;
2603 FC_RETURN_BOOL(ret);
2607 FCIMPL1(Object*, RuntimeMethodHandle::GetLoaderAllocator, MethodDesc *pMethod)
2614 OBJECTREF loaderAllocator = NULL;
2617 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2619 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(loaderAllocator);
2621 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2622 loaderAllocator = pLoaderAllocator->GetExposedObject();
2624 HELPER_METHOD_FRAME_END();
2626 return OBJECTREFToObject(loaderAllocator);
2630 //*********************************************************************************************
2631 //*********************************************************************************************
2632 //*********************************************************************************************
2634 FCIMPL1(StringObject*, RuntimeFieldHandle::GetName, ReflectFieldObject *pFieldUNSAFE) {
2640 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2642 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2644 FieldDesc *pField = refField->GetField();
2646 STRINGREF refString = NULL;
2647 HELPER_METHOD_FRAME_BEGIN_RET_1(refField);
2649 refString = StringObject::NewString(pField->GetName());
2651 HELPER_METHOD_FRAME_END();
2652 return (StringObject*)OBJECTREFToObject(refString);
2656 FCIMPL1(LPCUTF8, RuntimeFieldHandle::GetUtf8Name, FieldDesc *pField) {
2659 PRECONDITION(CheckPointer(pField));
2663 LPCUTF8 szFieldName;
2665 if (FAILED(pField->GetName_NoThrow(&szFieldName)))
2667 FCThrow(kBadImageFormatException);
2673 FCIMPL2(FC_BOOL_RET, RuntimeFieldHandle::MatchesNameHash, FieldDesc * pField, ULONG hash)
2677 FC_RETURN_BOOL(pField->MightHaveName(hash));
2681 FCIMPL1(INT32, RuntimeFieldHandle::GetAttributes, FieldDesc *pField) {
2688 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2691 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2692 ret = (INT32)pField->GetAttributes();
2693 END_SO_INTOLERANT_CODE;
2698 FCIMPL1(ReflectClassBaseObject*, RuntimeFieldHandle::GetApproxDeclaringType, FieldDesc *pField) {
2705 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2707 TypeHandle th = TypeHandle(pField->GetApproxEnclosingMethodTable()); // <REVISIT_TODO> this needs to be checked - see bug 184355 </REVISIT_TODO>
2708 RETURN_CLASS_OBJECT(th, NULL);
2712 FCIMPL1(INT32, RuntimeFieldHandle::GetToken, ReflectFieldObject *pFieldUNSAFE) {
2718 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2720 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2722 FieldDesc *pField = refField->GetField();
2724 INT32 tkFieldDef = (INT32)pField->GetMemberDef();
2725 _ASSERTE(!IsNilToken(tkFieldDef) || tkFieldDef == mdFieldDefNil);
2730 FCIMPL2(FieldDesc*, RuntimeFieldHandle::GetStaticFieldForGenericType, FieldDesc *pField, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2737 REFLECTCLASSBASEREF refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2739 if ((refDeclaringType == NULL) || (pField == NULL))
2740 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2742 TypeHandle declaringType = refDeclaringType->GetType();
2745 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2746 if (declaringType.IsTypeDesc())
2747 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2748 MethodTable *pMT = declaringType.AsMethodTable();
2750 _ASSERTE(pField->IsStatic());
2751 if (pMT->HasGenericsStaticsInfo())
2752 pField = pMT->GetFieldDescByIndex(pField->GetApproxEnclosingMethodTable()->GetIndexForFieldDesc(pField));
2753 _ASSERTE(!pField->IsSharedByGenericInstantiations());
2754 _ASSERTE(pField->GetEnclosingMethodTable() == pMT);
2760 FCIMPL1(ReflectModuleBaseObject*, AssemblyHandle::GetManifestModule, AssemblyBaseObject* pAssemblyUNSAFE) {
2763 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2765 if (refAssembly == NULL)
2766 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2768 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2769 Assembly* currentAssembly = pAssembly->GetCurrentAssembly();
2771 if (currentAssembly == NULL)
2774 Module *pModule = currentAssembly->GetManifestModule();
2775 DomainFile * pDomainFile = pModule->FindDomainFile(GetAppDomain());
2780 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2781 orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2782 if (orModule == NULL)
2783 orModule = pModule->GetExposedObject();
2785 OBJECTREF orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2786 if (orModule != NULL)
2787 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2789 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2790 orModule = pModule->GetExposedObject();
2793 HELPER_METHOD_FRAME_END();
2794 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2799 FCIMPL1(INT32, AssemblyHandle::GetToken, AssemblyBaseObject* pAssemblyUNSAFE) {
2802 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2804 if (refAssembly == NULL)
2805 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2807 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2808 mdAssembly token = mdAssemblyNil;
2810 IMDInternalImport *mdImport = pAssembly->GetCurrentAssembly()->GetManifestImport();
2814 if (FAILED(mdImport->GetAssemblyFromScope(&token)))
2816 FCThrow(kBadImageFormatException);
2825 void QCALLTYPE ModuleHandle::GetPEKind(QCall::ModuleHandle pModule, DWORD* pdwPEKind, DWORD* pdwMachine)
2830 pModule->GetFile()->GetPEKindAndMachine(pdwPEKind, pdwMachine);
2834 FCIMPL1(INT32, ModuleHandle::GetMDStreamVersion, ReflectModuleBaseObject * pModuleUNSAFE)
2838 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2840 if (refModule == NULL)
2841 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2843 Module *pModule = refModule->GetModule();
2845 if (pModule->IsResource())
2848 return pModule->GetMDImport()->GetMetadataStreamVersion();
2852 void QCALLTYPE ModuleHandle::GetModuleType(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
2856 TypeHandle globalTypeHandle = TypeHandle();
2862 globalTypeHandle = TypeHandle(pModule->GetGlobalMethodTable());
2864 EX_SWALLOW_NONTRANSIENT;
2866 if (!globalTypeHandle.IsNull())
2869 retType.Set(globalTypeHandle.GetManagedClassObject());
2877 FCIMPL1(INT32, ModuleHandle::GetToken, ReflectModuleBaseObject * pModuleUNSAFE) {
2883 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2885 if (refModule == NULL)
2886 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2888 Module *pModule = refModule->GetModule();
2890 if (pModule->IsResource())
2893 return pModule->GetMDImport()->GetModuleFromScope();
2897 FCIMPL1(IMDInternalImport*, ModuleHandle::GetMetadataImport, ReflectModuleBaseObject * pModuleUNSAFE)
2901 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2903 if (refModule == NULL)
2904 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2906 Module *pModule = refModule->GetModule();
2908 if (pModule->IsResource())
2911 return pModule->GetMDImport();
2915 BOOL QCALLTYPE ModuleHandle::ContainsPropertyMatchingHash(QCall::ModuleHandle pModule, INT32 tkProperty, ULONG hash)
2919 BOOL fContains = TRUE;
2923 fContains = pModule->MightContainMatchingProperty(tkProperty, hash);
2930 void QCALLTYPE ModuleHandle::ResolveType(QCall::ModuleHandle pModule, INT32 tkType, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retType)
2934 TypeHandle typeHandle;
2938 _ASSERTE(!IsNilToken(tkType));
2940 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2941 typeHandle = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, tkType, &typeContext,
2942 ClassLoader::ThrowIfNotFound,
2943 ClassLoader::PermitUninstDefOrRef);
2946 retType.Set(typeHandle.GetManagedClassObject());
2953 MethodDesc *QCALLTYPE ModuleHandle::ResolveMethod(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount)
2957 MethodDesc* pMD = NULL;
2961 _ASSERTE(!IsNilToken(tkMemberRef));
2963 BOOL strictMetadataChecks = (TypeFromToken(tkMemberRef) == mdtMethodSpec);
2965 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2966 pMD = MemberLoader::GetMethodDescFromMemberDefOrRefOrSpec(pModule, tkMemberRef, &typeContext, strictMetadataChecks, FALSE);
2968 // This will get us the instantiating or unboxing stub if needed
2969 pMD = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMD, pMD->GetMethodTable(), pMD->GetMethodInstantiation());
2976 void QCALLTYPE ModuleHandle::ResolveField(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retField)
2980 FieldDesc* pField = NULL;
2984 _ASSERTE(!IsNilToken(tkMemberRef));
2986 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2987 pField = MemberLoader::GetFieldDescFromMemberDefOrRef(pModule, tkMemberRef, &typeContext, FALSE);
2989 retField.Set(pField->GetStubFieldInfo());
2996 void QCALLTYPE ModuleHandle::GetAssembly(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retAssembly)
3000 DomainAssembly *pAssembly = NULL;
3003 pAssembly = pModule->GetDomainAssembly();
3006 retAssembly.Set(pAssembly->GetExposedAssemblyObject());
3012 FCIMPL5(ReflectMethodObject*, ModuleHandle::GetDynamicMethod, ReflectMethodObject *pMethodUNSAFE, ReflectModuleBaseObject *pModuleUNSAFE, StringObject *name, U1Array *sig, Object *resolver) {
3015 PRECONDITION(CheckPointer(name));
3016 PRECONDITION(CheckPointer(sig));
3020 DynamicMethodDesc *pNewMD = NULL;
3025 OBJECTREF resolverRef;
3026 OBJECTREF methodRef;
3027 REFLECTMETHODREF retMethod;
3028 REFLECTMODULEBASEREF refModule;
3030 gc.nameRef = (STRINGREF)name;
3031 gc.resolverRef = (OBJECTREF)resolver;
3032 gc.methodRef = ObjectToOBJECTREF(pMethodUNSAFE);
3033 gc.retMethod = NULL;
3034 gc.refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
3036 if (gc.refModule == NULL)
3037 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
3039 Module *pModule = gc.refModule->GetModule();
3041 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
3043 DomainFile *pDomainModule = pModule->GetDomainFile();
3045 U1ARRAYREF dataArray = (U1ARRAYREF)sig;
3046 DWORD sigSize = dataArray->GetNumComponents();
3047 NewHolder<BYTE> pSig(new BYTE[sigSize]);
3048 memcpy(pSig, dataArray->GetDataPtr(), sigSize);
3050 DWORD length = gc.nameRef->GetStringLength();
3051 NewArrayHolder<char> pName(new char[(length + 1) * 2]);
3053 length = WszWideCharToMultiByte(CP_UTF8, 0, gc.nameRef->GetBuffer(), length, pName, (length + 1) * 2 - sizeof(char), NULL, NULL);
3055 pName[length / sizeof(char)] = '\0';
3057 DynamicMethodTable *pMTForDynamicMethods = pDomainModule->GetDynamicMethodTable();
3058 pNewMD = pMTForDynamicMethods->GetDynamicMethod(pSig, sigSize, pName);
3059 _ASSERTE(pNewMD != NULL);
3060 // pNewMD now owns pSig and pName.
3061 pSig.SuppressRelease();
3062 pName.SuppressRelease();
3064 // create a handle to hold the resolver objectref
3065 OBJECTHANDLE resolverHandle = pDomainModule->GetAppDomain()->CreateLongWeakHandle(gc.resolverRef);
3066 pNewMD->GetLCGMethodResolver()->SetManagedResolver(resolverHandle);
3067 gc.retMethod = pNewMD->GetStubMethodInfo();
3068 gc.retMethod->SetKeepAlive(gc.resolverRef);
3070 LoaderAllocator *pLoaderAllocator = pModule->GetLoaderAllocator();
3072 if (pLoaderAllocator->IsCollectible())
3073 pLoaderAllocator->AddReference();
3075 HELPER_METHOD_FRAME_END();
3077 return (ReflectMethodObject*)OBJECTREFToObject(gc.retMethod);
3081 void QCALLTYPE RuntimeMethodHandle::GetCallerType(QCall::StackCrawlMarkHandle pStackMark, QCall::ObjectHandleOnStack retType)
3087 MethodTable *pMT = NULL;
3089 pMT = SystemDomain::GetCallersType(pStackMark);
3092 retType.Set(pMT->GetManagedClassObject());