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, MdUtf8String::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 MdUtf8String::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 MdUtf8String::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 _ASSERTE(pCACtor->IsCtor());
119 dwAttr = pCACtor->GetAttrs();
122 StaticAccessCheckContext accessContext(NULL, pDecoratedMT, pDecoratedModule->GetAssembly());
124 return ClassLoader::CanAccess(
127 pCAMT->GetAssembly(),
131 *AccessCheckOptions::s_pNormalAccessChecks);
134 BOOL QCALLTYPE RuntimeMethodHandle::IsCAVisibleFromDecoratedType(
135 EnregisteredTypeHandle targetTypeHandle,
136 MethodDesc * pTargetCtor,
137 EnregisteredTypeHandle sourceTypeHandle,
138 QCall::ModuleHandle sourceModuleHandle)
145 TypeHandle sourceHandle = TypeHandle::FromPtr(sourceTypeHandle);
146 TypeHandle targetHandle = TypeHandle::FromPtr(targetTypeHandle);
148 _ASSERTE((sourceHandle.IsNull() || !sourceHandle.IsTypeDesc()) &&
149 !targetHandle.IsNull() &&
150 !targetHandle.IsTypeDesc());
152 if (sourceHandle.IsTypeDesc() ||
153 targetHandle.IsNull() ||
154 targetHandle.IsTypeDesc())
155 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
157 bResult = CheckCAVisibilityFromDecoratedType(targetHandle.AsMethodTable(), pTargetCtor, sourceHandle.AsMethodTable(), sourceModuleHandle);
164 NOINLINE static ReflectClassBaseObject* GetRuntimeTypeHelper(LPVOID __me, TypeHandle typeHandle, OBJECTREF keepAlive)
166 FC_INNER_PROLOG_NO_ME_SETUP();
167 if (typeHandle.AsPtr() == NULL)
170 // RuntimeTypeHandle::GetRuntimeType has picked off the most common case, but does not cover array types.
171 // Before we do the really heavy weight option of setting up a helper method frame, check if we have to.
172 OBJECTREF refType = typeHandle.GetManagedClassObjectFast();
174 return (ReflectClassBaseObject*)OBJECTREFToObject(refType);
176 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
177 refType = typeHandle.GetManagedClassObject();
178 HELPER_METHOD_FRAME_END();
181 return (ReflectClassBaseObject*)OBJECTREFToObject(refType);
184 #define RETURN_CLASS_OBJECT(typeHandle, keepAlive) FC_INNER_RETURN(ReflectClassBaseObject*, GetRuntimeTypeHelper(__me, typeHandle, keepAlive))
186 NOINLINE ReflectModuleBaseObject* GetRuntimeModuleHelper(LPVOID __me, Module *pModule, OBJECTREF keepAlive)
188 FC_INNER_PROLOG_NO_ME_SETUP();
192 DomainFile * pDomainFile = pModule->FindDomainFile(GetAppDomain());
194 OBJECTREF refModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
196 if(refModule != NULL)
197 return (ReflectModuleBaseObject*)OBJECTREFToObject(refModule);
199 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
200 refModule = pModule->GetExposedObject();
201 HELPER_METHOD_FRAME_END();
204 return (ReflectModuleBaseObject*)OBJECTREFToObject(refModule);
207 NOINLINE AssemblyBaseObject* GetRuntimeAssemblyHelper(LPVOID __me, DomainAssembly *pAssembly, OBJECTREF keepAlive)
209 FC_INNER_PROLOG_NO_ME_SETUP();
210 if (pAssembly == NULL)
213 OBJECTREF refAssembly = (pAssembly != NULL) ? pAssembly->GetExposedAssemblyObjectIfExists() : NULL;
215 if(refAssembly != NULL)
216 return (AssemblyBaseObject*)OBJECTREFToObject(refAssembly);
218 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
219 refAssembly = pAssembly->GetExposedAssemblyObject();
220 HELPER_METHOD_FRAME_END();
223 return (AssemblyBaseObject*)OBJECTREFToObject(refAssembly);
227 // This is the routine that is called by the 'typeof()' operator in C#. It is one of the most commonly used
228 // reflection operations. This call should be optimized away in nearly all situations
229 FCIMPL1_V(ReflectClassBaseObject*, RuntimeTypeHandle::GetTypeFromHandle, FCALLRuntimeTypeHandle th)
234 return FCALL_RTH_TO_REFLECTCLASS(th);
238 FCIMPL1(ReflectClassBaseObject*, RuntimeTypeHandle::GetRuntimeType, EnregisteredTypeHandle th)
242 TypeHandle typeHandle = TypeHandle::FromPtr(th);
243 _ASSERTE(CheckPointer(typeHandle.AsPtr(), NULL_OK));
244 if (typeHandle.AsPtr()!= NULL)
246 if (!typeHandle.IsTypeDesc())
248 OBJECTREF typePtr = typeHandle.AsMethodTable()->GetManagedClassObjectIfExists();
251 return (ReflectClassBaseObject*)OBJECTREFToObject(typePtr);
258 RETURN_CLASS_OBJECT(typeHandle, NULL);
262 FCIMPL1_V(EnregisteredTypeHandle, RuntimeTypeHandle::GetValueInternal, FCALLRuntimeTypeHandle RTH)
266 if (FCALL_RTH_TO_REFLECTCLASS(RTH) == NULL)
269 return FCALL_RTH_TO_REFLECTCLASS(RTH) ->GetType().AsPtr();
273 // TypeEqualsHelper and TypeNotEqualsHelper are almost identical.
274 // Unfortunately we cannot combime them because they need to hardcode the caller's name
275 NOINLINE static BOOL TypeEqualSlow(OBJECTREF refL, OBJECTREF refR, LPVOID __me)
279 FC_INNER_PROLOG_NO_ME_SETUP();
281 _ASSERTE(refL != NULL && refR != NULL);
283 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_2(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, refL, refR);
285 MethodDescCallSite TypeEqualsMethod(METHOD__OBJECT__EQUALS, &refL);
293 ret = TypeEqualsMethod.Call_RetBool(args);
295 HELPER_METHOD_FRAME_END();
304 #include <optsmallperfcritical.h>
306 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::TypeEQ, Object* left, Object* right)
310 OBJECTREF refL = (OBJECTREF)left;
311 OBJECTREF refR = (OBJECTREF)right;
315 FC_RETURN_BOOL(TRUE);
320 FC_RETURN_BOOL(FALSE);
323 if ((refL->GetMethodTable() == g_pRuntimeTypeClass || refR->GetMethodTable() == g_pRuntimeTypeClass))
325 // Quick path for negative common case
326 FC_RETURN_BOOL(FALSE);
329 // The fast path didn't get us the result
330 // Let's try the slow path: refL.Equals(refR);
331 FC_INNER_RETURN(FC_BOOL_RET, (FC_BOOL_RET)(!!TypeEqualSlow(refL, refR, __me)));
335 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::TypeNEQ, Object* left, Object* right)
339 OBJECTREF refL = (OBJECTREF)left;
340 OBJECTREF refR = (OBJECTREF)right;
344 FC_RETURN_BOOL(FALSE);
349 FC_RETURN_BOOL(TRUE);
352 if ((refL->GetMethodTable() == g_pRuntimeTypeClass || refR->GetMethodTable() == g_pRuntimeTypeClass))
354 // Quick path for negative common case
355 FC_RETURN_BOOL(TRUE);
358 // The fast path didn't get us the result
359 // Let's try the slow path: refL.Equals(refR);
360 FC_INNER_RETURN(FC_BOOL_RET, (FC_BOOL_RET)(!TypeEqualSlow(refL, refR, __me)));
364 #include <optdefault.h>
369 #ifdef FEATURE_COMINTEROP
370 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsWindowsRuntimeObjectType, ReflectClassBaseObject *rtTypeUNSAFE)
374 BOOL isWindowsRuntimeType = FALSE;
376 TypeHandle typeHandle = rtTypeUNSAFE->GetType();
377 MethodTable *pMT = typeHandle.GetMethodTable();
381 isWindowsRuntimeType = pMT->IsWinRTObjectType();
384 FC_RETURN_BOOL(isWindowsRuntimeType);
388 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsTypeExportedToWindowsRuntime, ReflectClassBaseObject *rtTypeUNSAFE)
392 BOOL isExportedToWinRT = FALSE;
394 TypeHandle typeHandle = rtTypeUNSAFE->GetType();
395 MethodTable *pMT = typeHandle.GetMethodTable();
399 isExportedToWinRT = pMT->IsExportedToWinRT();
402 FC_RETURN_BOOL(isExportedToWinRT);
405 #endif // FEATURE_COMINTEROP
407 NOINLINE static MethodDesc * RestoreMethodHelper(MethodDesc * pMethod, LPVOID __me)
409 FC_INNER_PROLOG_NO_ME_SETUP();
411 HELPER_METHOD_FRAME_BEGIN_RET_0();
412 pMethod->CheckRestore();
413 HELPER_METHOD_FRAME_END();
420 FCIMPL1(MethodDesc *, RuntimeTypeHandle::GetFirstIntroducedMethod, ReflectClassBaseObject *pTypeUNSAFE) {
423 PRECONDITION(CheckPointer(pTypeUNSAFE));
427 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
428 TypeHandle typeHandle = refType->GetType();
430 if (typeHandle.IsGenericVariable())
431 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
433 if (typeHandle.IsTypeDesc()) {
434 if (!typeHandle.IsArray())
438 MethodTable* pMT = typeHandle.GetMethodTable();
442 MethodDesc* pMethod = MethodTable::IntroducedMethodIterator::GetFirst(pMT);
444 // The only method that can show up here unrestored is instantiated methods. Check for it before performing the expensive IsRestored() check.
445 if (pMethod != NULL && pMethod->GetClassification() == mcInstantiated && !pMethod->IsRestored()) {
446 FC_INNER_RETURN(MethodDesc *, RestoreMethodHelper(pMethod, __me));
449 _ASSERTE(pMethod == NULL || pMethod->IsRestored());
454 #include <optsmallperfcritical.h>
455 FCIMPL1(void, RuntimeTypeHandle::GetNextIntroducedMethod, MethodDesc ** ppMethod) {
458 PRECONDITION(CheckPointer(ppMethod));
459 PRECONDITION(CheckPointer(*ppMethod));
463 MethodDesc *pMethod = MethodTable::IntroducedMethodIterator::GetNext(*ppMethod);
467 if (pMethod != NULL && pMethod->GetClassification() == mcInstantiated && !pMethod->IsRestored()) {
468 FC_INNER_RETURN_VOID(RestoreMethodHelper(pMethod, __me));
471 _ASSERTE(pMethod == NULL || pMethod->IsRestored());
474 #include <optdefault.h>
476 FCIMPL1(INT32, RuntimeTypeHandle::GetCorElementType, ReflectClassBaseObject *pTypeUNSAFE) {
482 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
485 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
487 return refType->GetType().GetSignatureCorElementType();
491 FCIMPL1(AssemblyBaseObject*, RuntimeTypeHandle::GetAssembly, ReflectClassBaseObject *pTypeUNSAFE) {
497 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
500 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
502 DomainFile *pDomainFile = NULL;
504 Module *pModule = refType->GetType().GetAssembly()->GetManifestModule();
506 pDomainFile = pModule->FindDomainFile(GetAppDomain());
508 FC_RETURN_ASSEMBLY_OBJECT((DomainAssembly *)pDomainFile, refType);
513 FCIMPL1(FC_BOOL_RET, RuntimeFieldHandle::AcquiresContextFromThis, FieldDesc *pField)
517 PRECONDITION(CheckPointer(pField));
521 FC_RETURN_BOOL(pField->IsSharedByGenericInstantiations());
526 FCIMPL1(ReflectModuleBaseObject*, RuntimeTypeHandle::GetModule, ReflectClassBaseObject *pTypeUNSAFE) {
534 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
537 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
539 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
541 result = refType->GetType().GetModule();
543 END_SO_INTOLERANT_CODE;
545 FC_RETURN_MODULE_OBJECT(result, refType);
549 FCIMPL1(ReflectClassBaseObject *, RuntimeTypeHandle::GetBaseType, ReflectClassBaseObject *pTypeUNSAFE) {
555 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
558 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
560 TypeHandle typeHandle = refType->GetType();
562 if (typeHandle.IsGenericVariable())
563 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
565 if (typeHandle.IsTypeDesc()) {
566 if (!typeHandle.IsArray())
570 RETURN_CLASS_OBJECT(typeHandle.GetParent(), refType);
574 FCIMPL1(ReflectClassBaseObject *, RuntimeTypeHandle::GetElementType, ReflectClassBaseObject *pTypeUNSAFE) {
580 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
583 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
585 TypeHandle typeHandle = refType->GetType();
587 if (!typeHandle.IsTypeDesc())
590 if (typeHandle.IsGenericVariable())
593 TypeHandle typeReturn;
595 if (typeHandle.IsArray())
596 typeReturn = typeHandle.AsArray()->GetArrayElementTypeHandle();
598 typeReturn = typeHandle.AsTypeDesc()->GetTypeParam();
600 RETURN_CLASS_OBJECT(typeReturn, refType);
604 FCIMPL1(INT32, RuntimeTypeHandle::GetArrayRank, ReflectClassBaseObject *pTypeUNSAFE) {
607 PRECONDITION(CheckPointer(pTypeUNSAFE));
608 PRECONDITION(pTypeUNSAFE->GetType().IsArray());
612 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
614 return (INT32)refType->GetType().AsArray()->GetRank();
618 FCIMPL1(INT32, RuntimeTypeHandle::GetNumVirtuals, ReflectClassBaseObject *pTypeUNSAFE) {
624 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
627 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
629 TypeHandle typeHandle = refType->GetType();
631 if (typeHandle.IsGenericVariable())
632 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
634 MethodTable *pMT = typeHandle.GetMethodTable();
637 return (INT32)pMT->GetNumVirtuals();
639 return 0; //REVIEW: should this return the number of methods in Object?
643 FCIMPL2(MethodDesc *, RuntimeTypeHandle::GetMethodAt, ReflectClassBaseObject *pTypeUNSAFE, INT32 slot) {
649 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
652 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
654 TypeHandle typeHandle = refType->GetType();
656 MethodDesc* pRetMethod = NULL;
658 if (typeHandle.IsGenericVariable())
659 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
661 if (slot < 0 || slot >= (INT32)typeHandle.GetMethodTable()->GetNumVirtuals())
662 FCThrowRes(kArgumentException, W("Arg_ArgumentOutOfRangeException"));
664 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
665 pRetMethod = typeHandle.GetMethodTable()->GetMethodDescForSlot((DWORD)slot);
666 HELPER_METHOD_FRAME_END();
673 FCIMPL3(FC_BOOL_RET, RuntimeTypeHandle::GetFields, ReflectClassBaseObject *pTypeUNSAFE, INT32 **result, INT32 *pCount) {
679 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
681 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
683 TypeHandle typeHandle = refType->GetType();
685 if (!pCount || !result)
686 FCThrow(kArgumentNullException);
688 if (typeHandle.IsGenericVariable())
689 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
691 if (typeHandle.IsTypeDesc()) {
693 FC_RETURN_BOOL(TRUE);
696 MethodTable *pMT= typeHandle.GetMethodTable();
698 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
701 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
702 // <TODO>Check this approximation - we may be losing exact type information </TODO>
703 ApproxFieldDescIterator fdIterator(pMT, ApproxFieldDescIterator::ALL_FIELDS);
704 INT32 count = (INT32)fdIterator.Count();
712 for(INT32 i = 0; i < count; i ++)
713 result[i] = (INT32*)fdIterator.Next();
718 HELPER_METHOD_FRAME_END();
719 FC_RETURN_BOOL(retVal);
723 void QCALLTYPE RuntimeMethodHandle::ConstructInstantiation(MethodDesc * pMethod, DWORD format, QCall::StringHandleOnStack retString)
730 TypeString::AppendInst(ss, pMethod->LoadMethodInstantiation(), format);
736 void QCALLTYPE RuntimeTypeHandle::ConstructName(EnregisteredTypeHandle pTypeHandle, DWORD format, QCall::StringHandleOnStack retString)
743 TypeString::AppendType(ss, TypeHandle::FromPtr(pTypeHandle), format);
749 PTRARRAYREF CopyRuntimeTypeHandles(TypeHandle * prgTH, FixupPointer<TypeHandle> * prgTH2, INT32 numTypeHandles, BinderClassID arrayElemType)
758 PTRARRAYREF refReturn = NULL;
759 PTRARRAYREF refArray = NULL;
761 if (numTypeHandles == 0)
764 _ASSERTE((prgTH != NULL) || (prgTH2 != NULL));
767 _ASSERTE(prgTH2 == NULL);
770 GCPROTECT_BEGIN(refArray);
771 TypeHandle thRuntimeType = TypeHandle(MscorlibBinder::GetClass(arrayElemType));
772 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(thRuntimeType, ELEMENT_TYPE_SZARRAY);
773 refArray = (PTRARRAYREF)AllocateArrayEx(arrayHandle, &numTypeHandles, 1);
775 for (INT32 i = 0; i < numTypeHandles; i++)
782 th = prgTH2[i].GetValue();
784 OBJECTREF refType = th.GetManagedClassObject();
785 refArray->SetAt(i, refType);
788 refReturn = refArray;
794 void QCALLTYPE RuntimeTypeHandle::GetConstraints(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retTypeArray)
798 TypeHandle* constraints = NULL;
802 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
804 if (!typeHandle.IsGenericVariable())
805 COMPlusThrow(kArgumentException, W("Arg_InvalidHandle"));
807 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
810 constraints = pGenericVariable->GetConstraints(&dwCount);
813 retTypeArray.Set(CopyRuntimeTypeHandles(constraints, NULL, dwCount, CLASS__TYPE));
820 FCIMPL1(PtrArray*, RuntimeTypeHandle::GetInterfaces, ReflectClassBaseObject *pTypeUNSAFE) {
826 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
829 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
831 TypeHandle typeHandle = refType->GetType();
833 if (typeHandle.IsGenericVariable())
834 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
836 INT32 ifaceCount = 0;
838 PTRARRAYREF refRetVal = NULL;
839 HELPER_METHOD_FRAME_BEGIN_RET_2(refRetVal, refType);
841 if (typeHandle.IsTypeDesc())
843 if (typeHandle.IsArray())
845 ifaceCount = typeHandle.GetMethodTable()->GetNumInterfaces();
854 ifaceCount = typeHandle.GetMethodTable()->GetNumInterfaces();
857 // Allocate the array
860 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(g_pRuntimeTypeClass), ELEMENT_TYPE_SZARRAY);
861 refRetVal = (PTRARRAYREF)AllocateArrayEx(arrayHandle, &ifaceCount, 1);
863 // populate type array
866 MethodTable::InterfaceMapIterator it = typeHandle.GetMethodTable()->IterateInterfaceMap();
869 OBJECTREF refInterface = it.GetInterface()->GetManagedClassObject();
870 refRetVal->SetAt(i, refInterface);
871 _ASSERTE(refRetVal->GetAt(i) != NULL);
876 HELPER_METHOD_FRAME_END();
878 return (PtrArray*)OBJECTREFToObject(refRetVal);
882 FCIMPL1(INT32, RuntimeTypeHandle::GetAttributes, ReflectClassBaseObject *pTypeUNSAFE) {
888 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
891 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
893 TypeHandle typeHandle = refType->GetType();
895 if (typeHandle.IsTypeDesc()) {
897 if (typeHandle.IsGenericVariable()) {
901 if (!typeHandle.IsArray())
905 #ifdef FEATURE_COMINTEROP
906 // __ComObject types are always public.
907 if (IsComObjectClass(typeHandle))
908 return (typeHandle.GetMethodTable()->GetAttrClass() & tdVisibilityMask) | tdPublic;
909 #endif // FEATURE_COMINTEROP
913 ret = (INT32)typeHandle.GetMethodTable()->GetAttrClass();
919 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsValueType, ReflectClassBaseObject *pTypeUNSAFE)
926 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
928 _ASSERTE(refType != NULL);
930 TypeHandle typeHandle = refType->GetType();
932 FC_RETURN_BOOL(typeHandle.IsValueType());
936 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsInterface, ReflectClassBaseObject *pTypeUNSAFE)
943 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
945 _ASSERTE(refType != NULL);
947 TypeHandle typeHandle = refType->GetType();
949 FC_RETURN_BOOL(typeHandle.IsInterface());
954 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsByRefLike, ReflectClassBaseObject *pTypeUNSAFE)
961 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
963 _ASSERTE(refType != NULL);
965 TypeHandle typeHandle = refType->GetType();
967 FC_RETURN_BOOL(typeHandle.IsByRefLike());
973 RuntimeTypeHandle::IsVisible(
974 EnregisteredTypeHandle pTypeHandle)
982 BOOL fIsExternallyVisible = FALSE;
986 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
988 _ASSERTE(!typeHandle.IsNull());
990 fIsExternallyVisible = typeHandle.IsExternallyVisible();
994 return fIsExternallyVisible;
995 } // RuntimeTypeHandle::IsVisible
997 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::IsComObject, ReflectClassBaseObject *pTypeUNSAFE, CLR_BOOL isGenericCOM) {
1005 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1007 if (refType == NULL)
1008 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1010 TypeHandle typeHandle = refType->GetType();
1012 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1015 ret = IsComObjectClass(typeHandle);
1017 ret = IsComWrapperClass(typeHandle);
1019 HELPER_METHOD_FRAME_END();
1021 FC_RETURN_BOOL(ret);
1025 FCIMPL1(LPCUTF8, RuntimeTypeHandle::GetUtf8Name, ReflectClassBaseObject* pTypeUNSAFE) {
1031 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1033 if (refType == NULL)
1034 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1036 TypeHandle typeHandle = refType->GetType();
1037 INT32 tkTypeDef = mdTypeDefNil;
1038 LPCUTF8 szName = NULL;
1040 if (typeHandle.IsGenericVariable())
1041 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1043 if (typeHandle.IsTypeDesc())
1044 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1046 MethodTable* pMT= typeHandle.GetMethodTable();
1049 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1051 tkTypeDef = (INT32)pMT->GetCl();
1053 if (IsNilToken(tkTypeDef))
1054 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1056 if (FAILED(pMT->GetMDImport()->GetNameOfTypeDef(tkTypeDef, &szName, NULL)))
1058 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1061 _ASSERTE(CheckPointer(szName, NULL_OK));
1067 FCIMPL1(INT32, RuntimeTypeHandle::GetToken, ReflectClassBaseObject *pTypeUNSAFE) {
1073 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1075 if (refType == NULL)
1076 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1078 TypeHandle typeHandle = refType->GetType();
1080 if (typeHandle.IsTypeDesc())
1082 if (typeHandle.IsGenericVariable())
1084 INT32 tkTypeDef = typeHandle.AsGenericVariable()->GetToken();
1086 _ASSERTE(!IsNilToken(tkTypeDef) && TypeFromToken(tkTypeDef) == mdtGenericParam);
1091 return mdTypeDefNil;
1094 return (INT32)typeHandle.AsMethodTable()->GetCl();
1098 PVOID QCALLTYPE RuntimeTypeHandle::GetGCHandle(EnregisteredTypeHandle pTypeHandle, INT32 handleType)
1102 OBJECTHANDLE objHandle = NULL;
1108 TypeHandle th = TypeHandle::FromPtr(pTypeHandle);
1109 assert(handleType >= HNDTYPE_WEAK_SHORT && handleType <= HNDTYPE_WEAK_WINRT);
1110 objHandle = AppDomain::GetCurrentDomain()->CreateTypedHandle(NULL, static_cast<HandleType>(handleType));
1111 th.GetLoaderAllocator()->RegisterHandleForCleanup(objHandle);
1118 void QCALLTYPE RuntimeTypeHandle::VerifyInterfaceIsImplemented(EnregisteredTypeHandle pTypeHandle, EnregisteredTypeHandle pIFaceHandle)
1124 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1125 TypeHandle ifaceHandle = TypeHandle::FromPtr(pIFaceHandle);
1127 if (typeHandle.IsGenericVariable())
1128 COMPlusThrow(kArgumentException, W("Arg_InvalidHandle"));
1130 if (typeHandle.IsTypeDesc()) {
1131 if (!typeHandle.IsArray())
1132 COMPlusThrow(kArgumentException, W("Arg_NotFoundIFace"));
1135 if (typeHandle.IsInterface())
1136 COMPlusThrow(kArgumentException, W("Argument_InterfaceMap"));
1138 if (!ifaceHandle.IsInterface())
1139 COMPlusThrow(kArgumentException, W("Arg_MustBeInterface"));
1141 // First try the cheap check, which amounts to iterating the interface map looking for
1142 // the ifaceHandle MethodTable.
1143 if (!typeHandle.GetMethodTable()->ImplementsInterface(ifaceHandle.AsMethodTable()))
1144 { // If the cheap check fails, try the more expensive but complete check.
1145 if (!typeHandle.CanCastTo(ifaceHandle))
1146 { // If the complete check fails, we're certain that this type
1147 // does not implement the interface specified.
1148 COMPlusThrow(kArgumentException, W("Arg_NotFoundIFace"));
1155 MethodDesc* QCALLTYPE RuntimeTypeHandle::GetInterfaceMethodImplementation(EnregisteredTypeHandle pTypeHandle, EnregisteredTypeHandle pOwner, MethodDesc * pMD)
1159 MethodDesc* pResult = nullptr;
1163 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1164 TypeHandle thOwnerOfMD = TypeHandle::FromPtr(pOwner);
1166 // Ok to have INVALID_SLOT in the case where abstract class does not implement an interface method.
1167 // This case can not be reproed using C# "implements" all interface methods
1168 // with at least an abstract method. b19897_GetInterfaceMap_Abstract.exe tests this case.
1169 //@TODO:STUBDISPATCH: Don't need to track down the implementation, just the declaration, and this can
1170 //@TODO: be done faster - just need to make a function FindDispatchDecl.
1171 DispatchSlot slot(typeHandle.GetMethodTable()->FindDispatchSlotForInterfaceMD(thOwnerOfMD, pMD, TRUE /* throwOnConflict */));
1173 pResult = slot.GetMethodDesc();
1180 void QCALLTYPE RuntimeTypeHandle::GetDefaultConstructor(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retMethod)
1186 MethodDesc* pCtor = NULL;
1188 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1190 if (!typeHandle.IsTypeDesc())
1192 MethodTable* pMethodTable = typeHandle.AsMethodTable();
1193 if (pMethodTable->HasDefaultConstructor())
1194 pCtor = pMethodTable->GetDefaultConstructor();
1200 retMethod.Set(pCtor->GetStubMethodInfo());
1207 FCIMPL1(ReflectMethodObject*, RuntimeTypeHandle::GetDeclaringMethod, ReflectClassBaseObject *pTypeUNSAFE) {
1213 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1215 if (refType == NULL)
1216 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1218 TypeHandle typeHandle = refType->GetType();;
1220 if (!typeHandle.IsTypeDesc())
1223 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
1224 mdToken defToken = pGenericVariable->GetTypeOrMethodDef();
1225 if (TypeFromToken(defToken) != mdtMethodDef)
1228 REFLECTMETHODREF pRet = NULL;
1229 HELPER_METHOD_FRAME_BEGIN_RET_0();
1230 MethodDesc * pMD = pGenericVariable->LoadOwnerMethod();
1231 pMD->CheckRestore();
1232 pRet = pMD->GetStubMethodInfo();
1233 HELPER_METHOD_FRAME_END();
1235 return (ReflectMethodObject*)OBJECTREFToObject(pRet);
1239 FCIMPL1(ReflectClassBaseObject*, RuntimeTypeHandle::GetDeclaringType, ReflectClassBaseObject *pTypeUNSAFE) {
1245 TypeHandle retTypeHandle;
1247 BOOL fThrowException = FALSE;
1248 LPCWSTR argName = W("Arg_InvalidHandle");
1249 RuntimeExceptionKind reKind = kArgumentNullException;
1251 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1253 if (refType == NULL)
1254 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1256 TypeHandle typeHandle = refType->GetType();
1258 MethodTable* pMT = NULL;
1259 mdTypeDef tkTypeDef = mdTokenNil;
1261 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1262 if (typeHandle.IsTypeDesc()) {
1264 if (typeHandle.IsGenericVariable()) {
1265 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
1266 mdToken defToken = pGenericVariable->GetTypeOrMethodDef();
1268 // Try the fast way first (if the declaring type has been loaded already).
1269 if (TypeFromToken(defToken) == mdtMethodDef)
1271 MethodDesc * retMethod = pGenericVariable->GetModule()->LookupMethodDef(defToken);
1272 if (retMethod != NULL)
1273 retTypeHandle = retMethod->GetMethodTable();
1277 retTypeHandle = pGenericVariable->GetModule()->LookupTypeDef(defToken);
1280 if (!retTypeHandle.IsNull() && retTypeHandle.IsFullyLoaded())
1283 // OK, need to go the slow way and load the type first.
1284 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1286 if (TypeFromToken(defToken) == mdtMethodDef)
1288 retTypeHandle = pGenericVariable->LoadOwnerMethod()->GetMethodTable();
1292 retTypeHandle = pGenericVariable->LoadOwnerType();
1294 retTypeHandle.CheckRestore();
1296 HELPER_METHOD_FRAME_END();
1299 if (!typeHandle.IsArray())
1301 retTypeHandle = TypeHandle();
1306 pMT = typeHandle.GetMethodTable();
1310 fThrowException = TRUE;
1314 if(!pMT->GetClass()->IsNested())
1316 retTypeHandle = TypeHandle();
1320 tkTypeDef = pMT->GetCl();
1322 if (FAILED(typeHandle.GetModule()->GetMDImport()->GetNestedClassProps(tkTypeDef, &tkTypeDef)))
1324 fThrowException = TRUE;
1325 reKind = kBadImageFormatException;
1330 // Try the fast way first (if the declaring type has been loaded already).
1331 retTypeHandle = typeHandle.GetModule()->LookupTypeDef(tkTypeDef);
1332 if (retTypeHandle.IsNull())
1334 // OK, need to go the slow way and load the type first.
1335 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1337 retTypeHandle = ClassLoader::LoadTypeDefThrowing(typeHandle.GetModule(), tkTypeDef,
1338 ClassLoader::ThrowIfNotFound,
1339 ClassLoader::PermitUninstDefOrRef);
1341 HELPER_METHOD_FRAME_END();
1345 END_SO_INTOLERANT_CODE;
1347 if (fThrowException)
1349 FCThrowRes(reKind, argName);
1352 RETURN_CLASS_OBJECT(retTypeHandle, refType);
1356 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CanCastTo, ReflectClassBaseObject *pTypeUNSAFE, ReflectClassBaseObject *pTargetUNSAFE) {
1363 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1364 REFLECTCLASSBASEREF refTarget = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTargetUNSAFE);
1366 if ((refType == NULL) || (refTarget == NULL))
1367 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1369 TypeHandle fromHandle = refType->GetType();
1370 TypeHandle toHandle = refTarget->GetType();
1374 TypeHandle::CastResult r = fromHandle.CanCastToNoGC(toHandle);
1375 if (r == TypeHandle::MaybeCast)
1377 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refTarget);
1378 iRetVal = fromHandle.CanCastTo(toHandle);
1379 HELPER_METHOD_FRAME_END();
1383 iRetVal = (r == TypeHandle::CanCast);
1386 // We allow T to be cast to Nullable<T>
1387 if (!iRetVal && Nullable::IsNullableType(toHandle) && !fromHandle.IsTypeDesc())
1389 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refTarget);
1390 if (Nullable::IsNullableForType(toHandle, fromHandle.AsMethodTable()))
1394 HELPER_METHOD_FRAME_END();
1397 FC_RETURN_BOOL(iRetVal);
1401 void QCALLTYPE RuntimeTypeHandle::GetTypeByNameUsingCARules(LPCWSTR pwzClassName, QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
1405 TypeHandle typeHandle;
1410 COMPlusThrowArgumentNull(W("className"),W("ArgumentNull_String"));
1412 typeHandle = TypeName::GetTypeUsingCASearchRules(pwzClassName, pModule->GetAssembly());
1415 retType.Set(typeHandle.GetManagedClassObject());
1422 void QCALLTYPE RuntimeTypeHandle::GetTypeByName(LPCWSTR pwzClassName, BOOL bThrowOnError, BOOL bIgnoreCase,
1423 QCall::StackCrawlMarkHandle pStackMark,
1424 ICLRPrivBinder * pPrivHostBinder,
1425 BOOL bLoadTypeFromPartialNameHack, QCall::ObjectHandleOnStack retType,
1426 QCall::ObjectHandleOnStack keepAlive)
1430 TypeHandle typeHandle;
1435 COMPlusThrowArgumentNull(W("className"),W("ArgumentNull_String"));
1438 typeHandle = TypeName::GetTypeManaged(pwzClassName, NULL, bThrowOnError, bIgnoreCase, /*bProhibitAsmQualifiedName =*/ FALSE, pStackMark,
1439 bLoadTypeFromPartialNameHack, (OBJECTREF*)keepAlive.m_ppObject,
1443 if (!typeHandle.IsNull())
1446 retType.Set(typeHandle.GetManagedClassObject());
1454 FCIMPL6(FC_BOOL_RET, RuntimeTypeHandle::SatisfiesConstraints, PTR_ReflectClassBaseObject pParamTypeUNSAFE, TypeHandle *typeContextArgs, INT32 typeContextCount, TypeHandle *methodContextArgs, INT32 methodContextCount, PTR_ReflectClassBaseObject pArgumentTypeUNSAFE);
1458 PRECONDITION(CheckPointer(typeContextArgs, NULL_OK));
1459 PRECONDITION(CheckPointer(methodContextArgs, NULL_OK));
1463 REFLECTCLASSBASEREF refParamType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pParamTypeUNSAFE);
1464 REFLECTCLASSBASEREF refArgumentType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pArgumentTypeUNSAFE);
1466 TypeHandle thGenericParameter = refParamType->GetType();
1467 TypeHandle thGenericArgument = refArgumentType->GetType();
1468 BOOL bResult = FALSE;
1469 SigTypeContext typeContext;
1471 Instantiation classInst;
1472 Instantiation methodInst;
1474 if (typeContextArgs != NULL)
1476 classInst = Instantiation(typeContextArgs, typeContextCount);
1479 if (methodContextArgs != NULL)
1481 methodInst = Instantiation(methodContextArgs, methodContextCount);
1484 SigTypeContext::InitTypeContext(classInst, methodInst, &typeContext);
1486 HELPER_METHOD_FRAME_BEGIN_RET_2(refParamType, refArgumentType);
1488 bResult = thGenericParameter.AsGenericVariable()->SatisfiesConstraints(&typeContext, thGenericArgument);
1490 HELPER_METHOD_FRAME_END();
1492 FC_RETURN_BOOL(bResult);
1496 void QCALLTYPE RuntimeTypeHandle::GetInstantiation(EnregisteredTypeHandle pType, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
1502 TypeHandle typeHandle = TypeHandle::FromPtr(pType);
1503 Instantiation inst = typeHandle.GetInstantiation();
1505 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
1511 void QCALLTYPE RuntimeTypeHandle::MakeArray(EnregisteredTypeHandle pTypeHandle, INT32 rank, QCall::ObjectHandleOnStack retType)
1515 TypeHandle arrayHandle;
1518 arrayHandle = TypeHandle::FromPtr(pTypeHandle).MakeArray(rank);
1520 retType.Set(arrayHandle.GetManagedClassObject());
1526 void QCALLTYPE RuntimeTypeHandle::MakeSZArray(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1530 TypeHandle arrayHandle;
1533 arrayHandle = TypeHandle::FromPtr(pTypeHandle).MakeSZArray();
1535 retType.Set(arrayHandle.GetManagedClassObject());
1541 void QCALLTYPE RuntimeTypeHandle::MakePointer(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1545 TypeHandle pointerHandle;
1548 pointerHandle = TypeHandle::FromPtr(pTypeHandle).MakePointer();
1550 retType.Set(pointerHandle.GetManagedClassObject());
1556 void QCALLTYPE RuntimeTypeHandle::MakeByRef(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1560 TypeHandle byRefHandle;
1563 byRefHandle = TypeHandle::FromPtr(pTypeHandle).MakeByRef();
1565 retType.Set(byRefHandle.GetManagedClassObject());
1571 BOOL QCALLTYPE RuntimeTypeHandle::IsCollectible(EnregisteredTypeHandle pTypeHandle)
1575 BOOL retVal = FALSE;
1578 retVal = TypeHandle::FromPtr(pTypeHandle).GetLoaderAllocator()->IsCollectible();
1584 void QCALLTYPE RuntimeTypeHandle::Instantiate(EnregisteredTypeHandle pTypeHandle, TypeHandle * pInstArray, INT32 cInstArray, QCall::ObjectHandleOnStack retType)
1591 type = TypeHandle::FromPtr(pTypeHandle).Instantiate(Instantiation(pInstArray, cInstArray));
1593 retType.Set(type.GetManagedClassObject());
1599 void QCALLTYPE RuntimeTypeHandle::GetGenericTypeDefinition(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1607 TypeHandle genericType = TypeHandle::FromPtr(pTypeHandle);
1609 typeDef = ClassLoader::LoadTypeDefThrowing(genericType.GetModule(),
1610 genericType.GetMethodTable()->GetCl(),
1611 ClassLoader::ThrowIfNotFound,
1612 ClassLoader::PermitUninstDefOrRef);
1615 retType.Set(typeDef.GetManagedClassObject());
1622 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CompareCanonicalHandles, ReflectClassBaseObject *pLeftUNSAFE, ReflectClassBaseObject *pRightUNSAFE)
1626 REFLECTCLASSBASEREF refLeft = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pLeftUNSAFE);
1627 REFLECTCLASSBASEREF refRight = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pRightUNSAFE);
1629 if ((refLeft == NULL) || (refRight == NULL))
1630 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1632 FC_RETURN_BOOL(refLeft->GetType().GetCanonicalMethodTable() == refRight->GetType().GetCanonicalMethodTable());
1636 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::HasInstantiation, PTR_ReflectClassBaseObject pTypeUNSAFE)
1640 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1642 if (refType == NULL)
1643 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1645 FC_RETURN_BOOL(refType->GetType().HasInstantiation());
1649 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericTypeDefinition, PTR_ReflectClassBaseObject pTypeUNSAFE)
1653 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1655 if (refType == NULL)
1656 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1658 FC_RETURN_BOOL(refType->GetType().IsGenericTypeDefinition());
1662 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericVariable, PTR_ReflectClassBaseObject pTypeUNSAFE)
1666 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1668 if (refType == NULL)
1669 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1671 FC_RETURN_BOOL(refType->GetType().IsGenericVariable());
1675 FCIMPL1(INT32, RuntimeTypeHandle::GetGenericVariableIndex, PTR_ReflectClassBaseObject pTypeUNSAFE)
1679 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1681 if (refType == NULL)
1682 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1684 return (INT32)refType->GetType().AsGenericVariable()->GetIndex();
1688 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::ContainsGenericVariables, PTR_ReflectClassBaseObject pTypeUNSAFE)
1692 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1694 if (refType == NULL)
1695 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1697 FC_RETURN_BOOL(refType->GetType().ContainsGenericVariables());
1701 FCIMPL1(IMDInternalImport*, RuntimeTypeHandle::GetMetadataImport, ReflectClassBaseObject * pTypeUNSAFE)
1705 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1707 if (refType == NULL)
1708 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1710 Module *pModule = refType->GetType().GetModule();
1712 return pModule->GetMDImport();
1717 //***********************************************************************************
1718 //***********************************************************************************
1719 //***********************************************************************************
1721 void * QCALLTYPE RuntimeMethodHandle::GetFunctionPointer(MethodDesc * pMethod)
1729 funcPtr = (void*)pMethod->GetMultiCallableAddrOfCode();
1736 BOOL QCALLTYPE RuntimeMethodHandle::GetIsCollectible(MethodDesc * pMethod)
1740 BOOL isCollectible = FALSE;
1744 isCollectible = pMethod->GetLoaderAllocator()->IsCollectible();
1748 return isCollectible;
1751 FCIMPL1(LPCUTF8, RuntimeMethodHandle::GetUtf8Name, MethodDesc *pMethod) {
1757 LPCUTF8 szName = NULL;
1760 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1762 szName = pMethod->GetName();
1764 _ASSERTE(CheckPointer(szName, NULL_OK));
1770 FCIMPL2(FC_BOOL_RET, RuntimeMethodHandle::MatchesNameHash, MethodDesc * pMethod, ULONG hash)
1774 FC_RETURN_BOOL(pMethod->MightHaveName(hash));
1778 FCIMPL1(StringObject*, RuntimeMethodHandle::GetName, MethodDesc *pMethod) {
1785 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1787 STRINGREF refName = NULL;
1789 HELPER_METHOD_FRAME_BEGIN_RET_0();
1790 refName = StringObject::NewString(pMethod->GetName());
1791 HELPER_METHOD_FRAME_END();
1793 return (StringObject*)OBJECTREFToObject(refName);
1797 FCIMPL1(INT32, RuntimeMethodHandle::GetAttributes, MethodDesc *pMethod) {
1804 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1807 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1808 retVal = (INT32)pMethod->GetAttrs();
1809 END_SO_INTOLERANT_CODE;
1814 FCIMPL1(INT32, RuntimeMethodHandle::GetImplAttributes, ReflectMethodObject *pMethodUNSAFE) {
1821 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1823 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
1824 INT32 attributes = 0;
1826 if (IsNilToken(pMethod->GetMemberDef()))
1829 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1831 attributes = (INT32)pMethod->GetImplAttrs();
1833 END_SO_INTOLERANT_CODE;
1840 FCIMPL1(ReflectClassBaseObject*, RuntimeMethodHandle::GetDeclaringType, MethodDesc *pMethod) {
1843 PRECONDITION(CheckPointer(pMethod));
1848 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1850 MethodTable *pMT = pMethod->GetMethodTable();
1851 TypeHandle declType(pMT);
1854 HELPER_METHOD_FRAME_BEGIN_RET_0();
1856 // Load the TypeDesc for the array type. Note the returned type is approximate, i.e.
1857 // if shared between reference array types then we will get object[] back.
1858 DWORD rank = pMT->GetRank();
1859 TypeHandle elemType = pMT->GetApproxArrayElementTypeHandle();
1860 declType = ClassLoader::LoadArrayTypeThrowing(elemType, pMT->GetInternalCorElementType(), rank);
1861 HELPER_METHOD_FRAME_END();
1863 RETURN_CLASS_OBJECT(declType, NULL);
1867 FCIMPL1(INT32, RuntimeMethodHandle::GetSlot, MethodDesc *pMethod) {
1874 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1876 return (INT32)pMethod->GetSlot();
1880 FCIMPL3(Object *, SignatureNative::GetCustomModifiers, SignatureNative* pSignatureUNSAFE,
1881 INT32 parameter, CLR_BOOL fRequired)
1890 SIGNATURENATIVEREF pSig;
1894 gc.pSig = (SIGNATURENATIVEREF)pSignatureUNSAFE;
1897 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
1900 BYTE callConv = *(BYTE*)gc.pSig->GetCorSig();
1901 SigTypeContext typeContext;
1902 gc.pSig->GetTypeContext(&typeContext);
1903 MetaSig sig(gc.pSig->GetCorSig(),
1904 gc.pSig->GetCorSigSize(),
1905 gc.pSig->GetModule(),
1907 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
1908 _ASSERTE(callConv == sig.GetCallingConventionInfo());
1910 SigPointer argument(NULL, 0);
1912 PRECONDITION(sig.GetCallingConvention() != IMAGE_CEE_CS_CALLCONV_FIELD || parameter == 1);
1916 argument = sig.GetReturnProps();
1920 for(INT32 i = 0; i < parameter; i++)
1923 argument = sig.GetArgProps();
1926 //if (parameter < 0 || parameter > (INT32)sig.NumFixedArgs())
1927 // FCThrowResVoid(kArgumentNullException, W("Arg_ArgumentOutOfRangeException"));
1929 SigPointer sp = argument;
1930 Module* pModule = sig.GetModule();
1932 CorElementType cmodType;
1934 CorElementType cmodTypeExpected = fRequired ? ELEMENT_TYPE_CMOD_REQD : ELEMENT_TYPE_CMOD_OPT;
1936 // Discover the number of required and optional custom modifiers.
1940 IfFailThrow(sp.GetByte(&data));
1941 cmodType = (CorElementType)data;
1943 if (cmodType == ELEMENT_TYPE_CMOD_REQD || cmodType == ELEMENT_TYPE_CMOD_OPT)
1945 if (cmodType == cmodTypeExpected)
1950 else if (cmodType != ELEMENT_TYPE_SENTINEL)
1955 IfFailThrow(sp.GetToken(NULL));
1958 // Reset sp and populate the arrays for the required and optional custom
1959 // modifiers now that we know how long they should be.
1962 MethodTable *pMT = MscorlibBinder::GetClass(CLASS__TYPE);
1963 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pMT), ELEMENT_TYPE_SZARRAY);
1965 gc.retVal = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &cMods, 1);
1970 IfFailThrow(sp.GetByte(&data));
1971 cmodType = (CorElementType)data;
1974 IfFailThrow(sp.GetToken(&token));
1976 if (cmodType == cmodTypeExpected)
1978 TypeHandle th = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, token,
1980 ClassLoader::ThrowIfNotFound,
1981 ClassLoader::FailIfUninstDefOrRef);
1983 OBJECTREF refType = th.GetManagedClassObject();
1984 gc.retVal->SetAt(--cMods, refType);
1988 HELPER_METHOD_FRAME_END();
1990 return OBJECTREFToObject(gc.retVal);
1994 FCIMPL1(INT32, RuntimeMethodHandle::GetMethodDef, ReflectMethodObject *pMethodUNSAFE) {
2001 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2003 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2005 if (pMethod->HasMethodInstantiation())
2007 HELPER_METHOD_FRAME_BEGIN_RET_1(pMethodUNSAFE);
2009 pMethod = pMethod->StripMethodInstantiation();
2011 HELPER_METHOD_FRAME_END();
2014 INT32 tkMethodDef = (INT32)pMethod->GetMemberDef();
2015 _ASSERTE(TypeFromToken(tkMethodDef) == mdtMethodDef);
2017 if (IsNilToken(tkMethodDef) || TypeFromToken(tkMethodDef) != mdtMethodDef)
2018 return mdMethodDefNil;
2024 FCIMPL6(void, SignatureNative::GetSignature,
2025 SignatureNative* pSignatureNativeUNSAFE,
2026 PCCOR_SIGNATURE pCorSig, DWORD cCorSig,
2027 FieldDesc *pFieldDesc, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE) {
2030 PRECONDITION(pDeclaringTypeUNSAFE || pMethodUNSAFE->GetMethod()->IsDynamicMethod());
2031 PRECONDITION(CheckPointer(pCorSig, NULL_OK));
2032 PRECONDITION(CheckPointer(pMethodUNSAFE, NULL_OK));
2033 PRECONDITION(CheckPointer(pFieldDesc, NULL_OK));
2039 REFLECTCLASSBASEREF refDeclaringType;
2040 REFLECTMETHODREF refMethod;
2041 SIGNATURENATIVEREF pSig;
2044 gc.refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2045 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2046 gc.pSig = (SIGNATURENATIVEREF)pSignatureNativeUNSAFE;
2048 MethodDesc *pMethod;
2049 TypeHandle declType;
2051 if (gc.refDeclaringType == NULL)
2053 // for dynamic method, see precondition
2054 pMethod = gc.refMethod->GetMethod();
2055 declType = pMethod->GetMethodTable();
2059 pMethod = gc.refMethod != NULL ? gc.refMethod->GetMethod() : NULL;
2060 declType = gc.refDeclaringType->GetType();
2063 HELPER_METHOD_FRAME_BEGIN_PROTECT(gc);
2065 Module* pModule = declType.GetModule();
2069 pMethod->GetSig(&pCorSig, &cCorSig);
2070 if (pMethod->GetClassification() == mcInstantiated)
2072 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2073 if (pLoaderAllocator->IsCollectible())
2074 gc.pSig->SetKeepAlive(pLoaderAllocator->GetExposedObject());
2077 else if (pFieldDesc)
2078 pFieldDesc->GetSig(&pCorSig, &cCorSig);
2080 gc.pSig->m_sig = pCorSig;
2081 gc.pSig->m_cSig = cCorSig;
2082 gc.pSig->m_pMethod = pMethod;
2084 REFLECTCLASSBASEREF refDeclType = (REFLECTCLASSBASEREF)declType.GetManagedClassObject();
2085 gc.pSig->SetDeclaringType(refDeclType);
2087 PREFIX_ASSUME(pCorSig!= NULL);
2088 BYTE callConv = *(BYTE*)pCorSig;
2089 SigTypeContext typeContext;
2091 SigTypeContext::InitTypeContext(
2092 pMethod, declType.GetClassOrArrayInstantiation(), pMethod->LoadMethodInstantiation(), &typeContext);
2094 SigTypeContext::InitTypeContext(declType, &typeContext);
2095 MetaSig msig(pCorSig, cCorSig, pModule, &typeContext,
2096 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
2098 if (callConv == IMAGE_CEE_CS_CALLCONV_FIELD)
2100 msig.NextArgNormalized();
2102 OBJECTREF refRetType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2103 gc.pSig->SetReturnType(refRetType);
2107 gc.pSig->SetCallingConvention(msig.GetCallingConventionInfo());
2109 OBJECTREF refRetType = msig.GetRetTypeHandleThrowing().GetManagedClassObject();
2110 gc.pSig->SetReturnType(refRetType);
2112 INT32 nArgs = msig.NumFixedArgs();
2113 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(g_pRuntimeTypeClass), ELEMENT_TYPE_SZARRAY);
2115 PTRARRAYREF ptrArrayarguments = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &nArgs, 1);
2116 gc.pSig->SetArgumentArray(ptrArrayarguments);
2118 for (INT32 i = 0; i < nArgs; i++)
2122 OBJECTREF refArgType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2123 gc.pSig->SetArgument(i, refArgType);
2126 _ASSERTE(gc.pSig->m_returnType != NULL);
2129 HELPER_METHOD_FRAME_END();
2133 FCIMPL2(FC_BOOL_RET, SignatureNative::CompareSig, SignatureNative* pLhsUNSAFE, SignatureNative* pRhsUNSAFE)
2141 SIGNATURENATIVEREF pLhs;
2142 SIGNATURENATIVEREF pRhs;
2145 gc.pLhs = (SIGNATURENATIVEREF)pLhsUNSAFE;
2146 gc.pRhs = (SIGNATURENATIVEREF)pRhsUNSAFE;
2148 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2150 ret = MetaSig::CompareMethodSigs(
2151 gc.pLhs->GetCorSig(), gc.pLhs->GetCorSigSize(), gc.pLhs->GetModule(), NULL,
2152 gc.pRhs->GetCorSig(), gc.pRhs->GetCorSigSize(), gc.pRhs->GetModule(), NULL);
2154 HELPER_METHOD_FRAME_END();
2155 FC_RETURN_BOOL(ret);
2159 void QCALLTYPE RuntimeMethodHandle::GetMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
2164 Instantiation inst = pMethod->LoadMethodInstantiation();
2167 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
2173 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::HasMethodInstantiation, MethodDesc * pMethod)
2177 FC_RETURN_BOOL(pMethod->HasMethodInstantiation());
2181 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsGenericMethodDefinition, MethodDesc * pMethod)
2185 FC_RETURN_BOOL(pMethod->IsGenericMethodDefinition());
2189 FCIMPL1(INT32, RuntimeMethodHandle::GetGenericParameterCount, MethodDesc * pMethod)
2193 return pMethod->GetNumGenericMethodArgs();
2197 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsDynamicMethod, MethodDesc * pMethod)
2201 FC_RETURN_BOOL(pMethod->IsNoMetadata());
2205 FCIMPL1(Object*, RuntimeMethodHandle::GetResolver, MethodDesc * pMethod)
2210 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2212 OBJECTREF resolver = NULL;
2213 if (pMethod->IsLCGMethod())
2215 resolver = pMethod->AsDynamicMethodDesc()->GetLCGMethodResolver()->GetManagedResolver();
2217 return OBJECTREFToObject(resolver);
2221 void QCALLTYPE RuntimeMethodHandle::Destroy(MethodDesc * pMethod)
2227 if (pMethod == NULL)
2228 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2230 DynamicMethodDesc* pDynamicMethodDesc = pMethod->AsDynamicMethodDesc();
2234 // Destroy should be called only if the managed part is gone.
2235 _ASSERTE(OBJECTREFToObject(pDynamicMethodDesc->GetLCGMethodResolver()->GetManagedResolver()) == NULL);
2237 // Fire Unload Dynamic Method Event here
2238 ETW::MethodLog::DynamicMethodDestroyed(pMethod);
2240 BEGIN_PIN_PROFILER(CORProfilerIsMonitoringDynamicFunctionUnloads());
2241 g_profControlBlock.pProfInterface->DynamicMethodUnloaded((FunctionID)pMethod);
2244 pDynamicMethodDesc->Destroy();
2249 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsTypicalMethodDefinition, ReflectMethodObject *pMethodUNSAFE)
2254 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2256 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2258 FC_RETURN_BOOL(pMethod->IsTypicalMethodDefinition());
2262 void QCALLTYPE RuntimeMethodHandle::GetTypicalMethodDefinition(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2270 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2273 MethodDesc *pMethodTypical = pMethod->LoadTypicalMethodDefinition();
2274 if (pMethodTypical != pMethod)
2277 refMethod.Set(pMethodTypical->GetStubMethodInfo());
2284 void QCALLTYPE RuntimeMethodHandle::StripMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2291 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2296 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2299 MethodDesc *pMethodStripped = pMethod->StripMethodInstantiation();
2300 if (pMethodStripped != pMethod)
2303 refMethod.Set(pMethodStripped->GetStubMethodInfo());
2310 // In the VM there might be more than one MethodDescs for a "method"
2311 // examples are methods on generic types which may have additional instantiating stubs
2312 // and methods on value types which may have additional unboxing stubs.
2314 // For generic methods we always hand out an instantiating stub except for a generic method definition
2315 // For non-generic methods on generic types we need an instantiating stub if it's one of the following
2316 // - static method on a generic class
2317 // - static or instance method on a generic interface
2318 // - static or instance method on a generic value type
2319 // The Reflection policy is to always hand out instantiating stubs in these cases
2321 // For methods on non-generic value types we can use either the cannonical method or the unboxing stub
2322 // The Reflection policy is to always hand out unboxing stubs if the methods are virtual methods
2323 // The reason for this is that in the current implementation of the class loader, the v-table slots for
2324 // those methods point to unboxing stubs already. Note that this is just a implementation choice
2325 // that might change in the future. But we should always keep this Reflection policy an invariant.
2327 // For virtual methods on generic value types (intersection of the two cases), reflection will hand
2328 // out an unboxing instantiating stub
2330 // GetInstantiatingStub is called to:
2331 // 1. create an InstantiatedMethodDesc for a generic method when calling BindGenericArguments() on a generic
2332 // method. In this case instArray will not be null.
2333 // 2. create an InstantiatedMethodDesc for a method in a generic class. In this case instArray will be null.
2334 // 3. create an UnboxingStub for a method in a value type. In this case instArray will be null.
2335 // For case 2 and 3, an instantiating stub or unboxing stub might not be needed in which case the original
2336 // MethodDesc is returned.
2337 FCIMPL3(MethodDesc*, RuntimeMethodHandle::GetStubIfNeeded,
2338 MethodDesc *pMethod,
2339 ReflectClassBaseObject *pTypeUNSAFE,
2340 PtrArray* instArrayUNSAFE)
2347 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2348 PTRARRAYREF instArray = (PTRARRAYREF)ObjectToOBJECTREF(instArrayUNSAFE);
2350 if (refType == NULL)
2351 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2353 TypeHandle instType = refType->GetType();
2354 MethodDesc *pNewMethod = pMethod;
2358 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
2360 if (instType.IsNull())
2361 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2363 // Perf optimization: this logic is actually duplicated in FindOrCreateAssociatedMethodDescForReflection, but since it
2364 // is the more common case it's worth the duplicate check here to avoid the helper method frame
2365 if ( instArray == NULL &&
2366 ( pMethod->HasMethodInstantiation() ||
2367 ( !instType.IsValueType() &&
2368 ( !instType.HasInstantiation() || instType.IsGenericTypeDefinition() ) ) ) )
2373 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, instArray);
2375 TypeHandle *inst = NULL;
2378 if (instArray != NULL)
2380 ntypars = instArray->GetNumComponents();
2382 size_t size = ntypars * sizeof(TypeHandle);
2383 if ((size / sizeof(TypeHandle)) != ntypars) // uint over/underflow
2384 COMPlusThrow(kArgumentException);
2385 inst = (TypeHandle*) _alloca(size);
2387 for (DWORD i = 0; i < ntypars; i++)
2389 REFLECTCLASSBASEREF instRef = (REFLECTCLASSBASEREF)instArray->GetAt(i);
2391 if (instRef == NULL)
2392 COMPlusThrowArgumentNull(W("inst"), W("ArgumentNull_ArrayElement"));
2394 inst[i] = instRef->GetType();
2398 pNewMethod = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMethod, instType, Instantiation(inst, ntypars));
2400 HELPER_METHOD_FRAME_END();
2407 FCIMPL2(MethodDesc*, RuntimeMethodHandle::GetMethodFromCanonical, MethodDesc *pMethod, ReflectClassBaseObject *pTypeUNSAFE)
2411 PRECONDITION(CheckPointer(pMethod));
2415 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2417 TypeHandle instType = refType->GetType();
2418 MethodDesc* pMDescInCanonMT = instType.GetMethodTable()->GetParallelMethodDesc(pMethod);
2420 return pMDescInCanonMT;
2425 FCIMPL2(RuntimeMethodBody *, RuntimeMethodHandle::GetMethodBody, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2435 RUNTIMEMETHODBODYREF MethodBodyObj;
2436 RUNTIMEEXCEPTIONHANDLINGCLAUSEREF EHClauseObj;
2437 RUNTIMELOCALVARIABLEINFOREF RuntimeLocalVariableInfoObj;
2439 BASEARRAYREF TempArray;
2440 REFLECTCLASSBASEREF declaringType;
2441 REFLECTMETHODREF refMethod;
2444 gc.MethodBodyObj = NULL;
2445 gc.EHClauseObj = NULL;
2446 gc.RuntimeLocalVariableInfoObj = NULL;
2448 gc.TempArray = NULL;
2449 gc.declaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2450 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2454 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2456 MethodDesc* pMethod = gc.refMethod->GetMethod();
2458 TypeHandle declaringType = gc.declaringType == NULL ? TypeHandle() : gc.declaringType->GetType();
2460 if (!pMethod->IsIL())
2463 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2465 MethodDesc *pMethodIL = pMethod;
2466 if (pMethod->IsWrapperStub())
2467 pMethodIL = pMethod->GetWrappedMethodDesc();
2469 COR_ILMETHOD* pILHeader = pMethodIL->GetILHeader();
2473 MethodTable * pExceptionHandlingClauseMT = MscorlibBinder::GetClass(CLASS__RUNTIME_EH_CLAUSE);
2474 TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);
2476 MethodTable * pLocalVariableMT = MscorlibBinder::GetClass(CLASS__RUNTIME_LOCAL_VARIABLE_INFO);
2477 TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);
2479 Module* pModule = pMethod->GetModule();
2480 COR_ILMETHOD_DECODER::DecoderStatus status;
2481 COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);
2483 if (status != COR_ILMETHOD_DECODER::SUCCESS)
2485 if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
2487 // Throw a verification HR
2488 COMPlusThrowHR(COR_E_VERIFICATION);
2492 COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
2496 gc.MethodBodyObj = (RUNTIMEMETHODBODYREF)AllocateObject(MscorlibBinder::GetClass(CLASS__RUNTIME_METHOD_BODY));
2498 gc.MethodBodyObj->_maxStackSize = header.GetMaxStack();
2499 gc.MethodBodyObj->_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);
2502 gc.MethodBodyObj->_localVarSigToken = header.GetLocalVarSigTok();
2504 gc.MethodBodyObj->_localVarSigToken = 0;
2506 // Allocate the array of IL and fill it in from the method header.
2507 BYTE* pIL = const_cast<BYTE*>(header.Code);
2508 COUNT_T cIL = header.GetCodeSize();
2509 gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);
2511 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_IL, gc.U1Array, GetAppDomain());
2512 memcpyNoGCRefs(gc.MethodBodyObj->_IL->GetDataPtr(), pIL, cIL);
2514 // Allocate the array of exception clauses.
2515 INT32 cEh = (INT32)header.EHCount();
2516 const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
2517 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thEHClauseArray, &cEh, 1);
2519 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_exceptionClauses, gc.TempArray, GetAppDomain());
2521 for (INT32 i = 0; i < cEh; i++)
2523 COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
2524 const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
2525 (const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);
2527 gc.EHClauseObj = (RUNTIMEEXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);
2529 gc.EHClauseObj->_flags = ehClause->GetFlags();
2530 gc.EHClauseObj->_tryOffset = ehClause->GetTryOffset();
2531 gc.EHClauseObj->_tryLength = ehClause->GetTryLength();
2532 gc.EHClauseObj->_handlerOffset = ehClause->GetHandlerOffset();
2533 gc.EHClauseObj->_handlerLength = ehClause->GetHandlerLength();
2535 if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
2536 gc.EHClauseObj->_catchToken = ehClause->GetClassToken();
2538 gc.EHClauseObj->_filterOffset = ehClause->GetFilterOffset();
2540 gc.MethodBodyObj->_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
2541 SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->_methodBody), (OBJECTREF)gc.MethodBodyObj, GetAppDomain());
2544 if (header.LocalVarSig != NULL)
2546 SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
2547 MetaSig metaSig(header.LocalVarSig,
2548 header.cbLocalVarSig,
2551 MetaSig::sigLocalVars);
2552 INT32 cLocals = metaSig.NumFixedArgs();
2553 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2554 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray, GetAppDomain());
2556 for (INT32 i = 0; i < cLocals; i ++)
2558 gc.RuntimeLocalVariableInfoObj = (RUNTIMELOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);
2560 gc.RuntimeLocalVariableInfoObj->_localIndex = i;
2564 CorElementType eType;
2565 IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
2566 if (ELEMENT_TYPE_PINNED == eType)
2567 gc.RuntimeLocalVariableInfoObj->_isPinned = TRUE;
2569 TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
2570 OBJECTREF refLocalType = tempType.GetManagedClassObject();
2571 gc.RuntimeLocalVariableInfoObj->SetType(refLocalType);
2572 gc.MethodBodyObj->_localVariables->SetAt(i, (OBJECTREF) gc.RuntimeLocalVariableInfoObj);
2578 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2579 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray, GetAppDomain());
2583 HELPER_METHOD_FRAME_END();
2585 return (RuntimeMethodBody*)OBJECTREFToObject(gc.MethodBodyObj);
2589 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsConstructor, MethodDesc *pMethod)
2593 PRECONDITION(CheckPointer(pMethod));
2598 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2599 ret = (BOOL)pMethod->IsClassConstructorOrCtor();
2600 END_SO_INTOLERANT_CODE;
2601 FC_RETURN_BOOL(ret);
2605 FCIMPL1(Object*, RuntimeMethodHandle::GetLoaderAllocator, MethodDesc *pMethod)
2612 OBJECTREF loaderAllocator = NULL;
2615 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2617 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(loaderAllocator);
2619 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2620 loaderAllocator = pLoaderAllocator->GetExposedObject();
2622 HELPER_METHOD_FRAME_END();
2624 return OBJECTREFToObject(loaderAllocator);
2628 //*********************************************************************************************
2629 //*********************************************************************************************
2630 //*********************************************************************************************
2632 FCIMPL1(StringObject*, RuntimeFieldHandle::GetName, ReflectFieldObject *pFieldUNSAFE) {
2638 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2640 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2642 FieldDesc *pField = refField->GetField();
2644 STRINGREF refString = NULL;
2645 HELPER_METHOD_FRAME_BEGIN_RET_1(refField);
2647 refString = StringObject::NewString(pField->GetName());
2649 HELPER_METHOD_FRAME_END();
2650 return (StringObject*)OBJECTREFToObject(refString);
2654 FCIMPL1(LPCUTF8, RuntimeFieldHandle::GetUtf8Name, FieldDesc *pField) {
2657 PRECONDITION(CheckPointer(pField));
2661 LPCUTF8 szFieldName;
2663 if (FAILED(pField->GetName_NoThrow(&szFieldName)))
2665 FCThrow(kBadImageFormatException);
2671 FCIMPL2(FC_BOOL_RET, RuntimeFieldHandle::MatchesNameHash, FieldDesc * pField, ULONG hash)
2675 FC_RETURN_BOOL(pField->MightHaveName(hash));
2679 FCIMPL1(INT32, RuntimeFieldHandle::GetAttributes, FieldDesc *pField) {
2686 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2689 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2690 ret = (INT32)pField->GetAttributes();
2691 END_SO_INTOLERANT_CODE;
2696 FCIMPL1(ReflectClassBaseObject*, RuntimeFieldHandle::GetApproxDeclaringType, FieldDesc *pField) {
2703 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2705 TypeHandle th = TypeHandle(pField->GetApproxEnclosingMethodTable()); // <REVISIT_TODO> this needs to be checked - see bug 184355 </REVISIT_TODO>
2706 RETURN_CLASS_OBJECT(th, NULL);
2710 FCIMPL1(INT32, RuntimeFieldHandle::GetToken, ReflectFieldObject *pFieldUNSAFE) {
2716 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2718 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2720 FieldDesc *pField = refField->GetField();
2722 INT32 tkFieldDef = (INT32)pField->GetMemberDef();
2723 _ASSERTE(!IsNilToken(tkFieldDef) || tkFieldDef == mdFieldDefNil);
2728 FCIMPL2(FieldDesc*, RuntimeFieldHandle::GetStaticFieldForGenericType, FieldDesc *pField, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2735 REFLECTCLASSBASEREF refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2737 if ((refDeclaringType == NULL) || (pField == NULL))
2738 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2740 TypeHandle declaringType = refDeclaringType->GetType();
2743 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2744 if (declaringType.IsTypeDesc())
2745 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2746 MethodTable *pMT = declaringType.AsMethodTable();
2748 _ASSERTE(pField->IsStatic());
2749 if (pMT->HasGenericsStaticsInfo())
2750 pField = pMT->GetFieldDescByIndex(pField->GetApproxEnclosingMethodTable()->GetIndexForFieldDesc(pField));
2751 _ASSERTE(!pField->IsSharedByGenericInstantiations());
2752 _ASSERTE(pField->GetEnclosingMethodTable() == pMT);
2758 FCIMPL1(ReflectModuleBaseObject*, AssemblyHandle::GetManifestModule, AssemblyBaseObject* pAssemblyUNSAFE) {
2761 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2763 if (refAssembly == NULL)
2764 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2766 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2767 Assembly* currentAssembly = pAssembly->GetCurrentAssembly();
2769 if (currentAssembly == NULL)
2772 Module *pModule = currentAssembly->GetManifestModule();
2773 DomainFile * pDomainFile = pModule->FindDomainFile(GetAppDomain());
2778 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2779 orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2780 if (orModule == NULL)
2781 orModule = pModule->GetExposedObject();
2783 OBJECTREF orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2784 if (orModule != NULL)
2785 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2787 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2788 orModule = pModule->GetExposedObject();
2791 HELPER_METHOD_FRAME_END();
2792 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2797 FCIMPL1(INT32, AssemblyHandle::GetToken, AssemblyBaseObject* pAssemblyUNSAFE) {
2800 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2802 if (refAssembly == NULL)
2803 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2805 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2806 mdAssembly token = mdAssemblyNil;
2808 IMDInternalImport *mdImport = pAssembly->GetCurrentAssembly()->GetManifestImport();
2812 if (FAILED(mdImport->GetAssemblyFromScope(&token)))
2814 FCThrow(kBadImageFormatException);
2823 void QCALLTYPE ModuleHandle::GetPEKind(QCall::ModuleHandle pModule, DWORD* pdwPEKind, DWORD* pdwMachine)
2828 pModule->GetFile()->GetPEKindAndMachine(pdwPEKind, pdwMachine);
2832 FCIMPL1(INT32, ModuleHandle::GetMDStreamVersion, ReflectModuleBaseObject * pModuleUNSAFE)
2836 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2838 if (refModule == NULL)
2839 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2841 Module *pModule = refModule->GetModule();
2843 if (pModule->IsResource())
2846 return pModule->GetMDImport()->GetMetadataStreamVersion();
2850 void QCALLTYPE ModuleHandle::GetModuleType(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
2854 TypeHandle globalTypeHandle = TypeHandle();
2860 globalTypeHandle = TypeHandle(pModule->GetGlobalMethodTable());
2862 EX_SWALLOW_NONTRANSIENT;
2864 if (!globalTypeHandle.IsNull())
2867 retType.Set(globalTypeHandle.GetManagedClassObject());
2875 FCIMPL1(INT32, ModuleHandle::GetToken, ReflectModuleBaseObject * pModuleUNSAFE) {
2881 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2883 if (refModule == NULL)
2884 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2886 Module *pModule = refModule->GetModule();
2888 if (pModule->IsResource())
2891 return pModule->GetMDImport()->GetModuleFromScope();
2895 FCIMPL1(IMDInternalImport*, ModuleHandle::GetMetadataImport, ReflectModuleBaseObject * pModuleUNSAFE)
2899 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2901 if (refModule == NULL)
2902 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2904 Module *pModule = refModule->GetModule();
2906 if (pModule->IsResource())
2909 return pModule->GetMDImport();
2913 BOOL QCALLTYPE ModuleHandle::ContainsPropertyMatchingHash(QCall::ModuleHandle pModule, INT32 tkProperty, ULONG hash)
2917 BOOL fContains = TRUE;
2921 fContains = pModule->MightContainMatchingProperty(tkProperty, hash);
2928 void QCALLTYPE ModuleHandle::ResolveType(QCall::ModuleHandle pModule, INT32 tkType, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retType)
2932 TypeHandle typeHandle;
2936 _ASSERTE(!IsNilToken(tkType));
2938 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2939 typeHandle = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, tkType, &typeContext,
2940 ClassLoader::ThrowIfNotFound,
2941 ClassLoader::PermitUninstDefOrRef);
2944 retType.Set(typeHandle.GetManagedClassObject());
2951 MethodDesc *QCALLTYPE ModuleHandle::ResolveMethod(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount)
2955 MethodDesc* pMD = NULL;
2959 _ASSERTE(!IsNilToken(tkMemberRef));
2961 BOOL strictMetadataChecks = (TypeFromToken(tkMemberRef) == mdtMethodSpec);
2963 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2964 pMD = MemberLoader::GetMethodDescFromMemberDefOrRefOrSpec(pModule, tkMemberRef, &typeContext, strictMetadataChecks, FALSE);
2966 // This will get us the instantiating or unboxing stub if needed
2967 pMD = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMD, pMD->GetMethodTable(), pMD->GetMethodInstantiation());
2974 void QCALLTYPE ModuleHandle::ResolveField(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retField)
2978 FieldDesc* pField = NULL;
2982 _ASSERTE(!IsNilToken(tkMemberRef));
2984 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2985 pField = MemberLoader::GetFieldDescFromMemberDefOrRef(pModule, tkMemberRef, &typeContext, FALSE);
2987 retField.Set(pField->GetStubFieldInfo());
2994 void QCALLTYPE ModuleHandle::GetAssembly(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retAssembly)
2998 DomainAssembly *pAssembly = NULL;
3001 pAssembly = pModule->GetDomainAssembly();
3004 retAssembly.Set(pAssembly->GetExposedAssemblyObject());
3010 FCIMPL5(ReflectMethodObject*, ModuleHandle::GetDynamicMethod, ReflectMethodObject *pMethodUNSAFE, ReflectModuleBaseObject *pModuleUNSAFE, StringObject *name, U1Array *sig, Object *resolver) {
3013 PRECONDITION(CheckPointer(name));
3014 PRECONDITION(CheckPointer(sig));
3018 DynamicMethodDesc *pNewMD = NULL;
3023 OBJECTREF resolverRef;
3024 OBJECTREF methodRef;
3025 REFLECTMETHODREF retMethod;
3026 REFLECTMODULEBASEREF refModule;
3028 gc.nameRef = (STRINGREF)name;
3029 gc.resolverRef = (OBJECTREF)resolver;
3030 gc.methodRef = ObjectToOBJECTREF(pMethodUNSAFE);
3031 gc.retMethod = NULL;
3032 gc.refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
3034 if (gc.refModule == NULL)
3035 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
3037 Module *pModule = gc.refModule->GetModule();
3039 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
3041 DomainFile *pDomainModule = pModule->GetDomainFile();
3043 U1ARRAYREF dataArray = (U1ARRAYREF)sig;
3044 DWORD sigSize = dataArray->GetNumComponents();
3045 NewHolder<BYTE> pSig(new BYTE[sigSize]);
3046 memcpy(pSig, dataArray->GetDataPtr(), sigSize);
3048 DWORD length = gc.nameRef->GetStringLength();
3049 NewArrayHolder<char> pName(new char[(length + 1) * 2]);
3051 length = WszWideCharToMultiByte(CP_UTF8, 0, gc.nameRef->GetBuffer(), length, pName, (length + 1) * 2 - sizeof(char), NULL, NULL);
3053 pName[length / sizeof(char)] = '\0';
3055 DynamicMethodTable *pMTForDynamicMethods = pDomainModule->GetDynamicMethodTable();
3056 pNewMD = pMTForDynamicMethods->GetDynamicMethod(pSig, sigSize, pName);
3057 _ASSERTE(pNewMD != NULL);
3058 // pNewMD now owns pSig and pName.
3059 pSig.SuppressRelease();
3060 pName.SuppressRelease();
3062 // create a handle to hold the resolver objectref
3063 OBJECTHANDLE resolverHandle = pDomainModule->GetAppDomain()->CreateLongWeakHandle(gc.resolverRef);
3064 pNewMD->GetLCGMethodResolver()->SetManagedResolver(resolverHandle);
3065 gc.retMethod = pNewMD->GetStubMethodInfo();
3066 gc.retMethod->SetKeepAlive(gc.resolverRef);
3068 LoaderAllocator *pLoaderAllocator = pModule->GetLoaderAllocator();
3070 if (pLoaderAllocator->IsCollectible())
3071 pLoaderAllocator->AddReference();
3073 HELPER_METHOD_FRAME_END();
3075 return (ReflectMethodObject*)OBJECTREFToObject(gc.retMethod);
3079 void QCALLTYPE RuntimeMethodHandle::GetCallerType(QCall::StackCrawlMarkHandle pStackMark, QCall::ObjectHandleOnStack retType)
3085 MethodTable *pMT = NULL;
3087 pMT = SystemDomain::GetCallersType(pStackMark);
3090 retType.Set(pMT->GetManagedClassObject());