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 FCIMPL1(LPCUTF8, RuntimeMethodHandle::GetUtf8Name, MethodDesc *pMethod) {
1742 LPCUTF8 szName = NULL;
1745 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1747 szName = pMethod->GetName();
1749 _ASSERTE(CheckPointer(szName, NULL_OK));
1755 FCIMPL2(FC_BOOL_RET, RuntimeMethodHandle::MatchesNameHash, MethodDesc * pMethod, ULONG hash)
1759 FC_RETURN_BOOL(pMethod->MightHaveName(hash));
1763 FCIMPL1(StringObject*, RuntimeMethodHandle::GetName, MethodDesc *pMethod) {
1770 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1772 STRINGREF refName = NULL;
1774 HELPER_METHOD_FRAME_BEGIN_RET_0();
1775 refName = StringObject::NewString(pMethod->GetName());
1776 HELPER_METHOD_FRAME_END();
1778 return (StringObject*)OBJECTREFToObject(refName);
1782 FCIMPL1(INT32, RuntimeMethodHandle::GetAttributes, MethodDesc *pMethod) {
1789 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1792 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1793 retVal = (INT32)pMethod->GetAttrs();
1794 END_SO_INTOLERANT_CODE;
1799 FCIMPL1(INT32, RuntimeMethodHandle::GetImplAttributes, ReflectMethodObject *pMethodUNSAFE) {
1806 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1808 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
1809 INT32 attributes = 0;
1811 if (IsNilToken(pMethod->GetMemberDef()))
1814 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1816 attributes = (INT32)pMethod->GetImplAttrs();
1818 END_SO_INTOLERANT_CODE;
1825 FCIMPL1(ReflectClassBaseObject*, RuntimeMethodHandle::GetDeclaringType, MethodDesc *pMethod) {
1828 PRECONDITION(CheckPointer(pMethod));
1833 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1835 MethodTable *pMT = pMethod->GetMethodTable();
1836 TypeHandle declType(pMT);
1839 HELPER_METHOD_FRAME_BEGIN_RET_0();
1841 // Load the TypeDesc for the array type. Note the returned type is approximate, i.e.
1842 // if shared between reference array types then we will get object[] back.
1843 DWORD rank = pMT->GetRank();
1844 TypeHandle elemType = pMT->GetApproxArrayElementTypeHandle();
1845 declType = ClassLoader::LoadArrayTypeThrowing(elemType, pMT->GetInternalCorElementType(), rank);
1846 HELPER_METHOD_FRAME_END();
1848 RETURN_CLASS_OBJECT(declType, NULL);
1852 FCIMPL1(INT32, RuntimeMethodHandle::GetSlot, MethodDesc *pMethod) {
1859 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1861 return (INT32)pMethod->GetSlot();
1865 FCIMPL3(Object *, SignatureNative::GetCustomModifiers, SignatureNative* pSignatureUNSAFE,
1866 INT32 parameter, CLR_BOOL fRequired)
1875 SIGNATURENATIVEREF pSig;
1879 gc.pSig = (SIGNATURENATIVEREF)pSignatureUNSAFE;
1882 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
1885 BYTE callConv = *(BYTE*)gc.pSig->GetCorSig();
1886 SigTypeContext typeContext;
1887 gc.pSig->GetTypeContext(&typeContext);
1888 MetaSig sig(gc.pSig->GetCorSig(),
1889 gc.pSig->GetCorSigSize(),
1890 gc.pSig->GetModule(),
1892 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
1893 _ASSERTE(callConv == sig.GetCallingConventionInfo());
1895 SigPointer argument(NULL, 0);
1897 PRECONDITION(sig.GetCallingConvention() != IMAGE_CEE_CS_CALLCONV_FIELD || parameter == 1);
1901 argument = sig.GetReturnProps();
1905 for(INT32 i = 0; i < parameter; i++)
1908 argument = sig.GetArgProps();
1911 //if (parameter < 0 || parameter > (INT32)sig.NumFixedArgs())
1912 // FCThrowResVoid(kArgumentNullException, W("Arg_ArgumentOutOfRangeException"));
1914 SigPointer sp = argument;
1915 Module* pModule = sig.GetModule();
1917 CorElementType cmodType;
1919 CorElementType cmodTypeExpected = fRequired ? ELEMENT_TYPE_CMOD_REQD : ELEMENT_TYPE_CMOD_OPT;
1921 // Discover the number of required and optional custom modifiers.
1925 IfFailThrow(sp.GetByte(&data));
1926 cmodType = (CorElementType)data;
1928 if (cmodType == ELEMENT_TYPE_CMOD_REQD || cmodType == ELEMENT_TYPE_CMOD_OPT)
1930 if (cmodType == cmodTypeExpected)
1935 else if (cmodType != ELEMENT_TYPE_SENTINEL)
1940 IfFailThrow(sp.GetToken(NULL));
1943 // Reset sp and populate the arrays for the required and optional custom
1944 // modifiers now that we know how long they should be.
1947 MethodTable *pMT = MscorlibBinder::GetClass(CLASS__TYPE);
1948 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pMT), ELEMENT_TYPE_SZARRAY);
1950 gc.retVal = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &cMods, 1);
1955 IfFailThrow(sp.GetByte(&data));
1956 cmodType = (CorElementType)data;
1959 IfFailThrow(sp.GetToken(&token));
1961 if (cmodType == cmodTypeExpected)
1963 TypeHandle th = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, token,
1965 ClassLoader::ThrowIfNotFound,
1966 ClassLoader::FailIfUninstDefOrRef);
1968 OBJECTREF refType = th.GetManagedClassObject();
1969 gc.retVal->SetAt(--cMods, refType);
1973 HELPER_METHOD_FRAME_END();
1975 return OBJECTREFToObject(gc.retVal);
1979 FCIMPL1(INT32, RuntimeMethodHandle::GetMethodDef, ReflectMethodObject *pMethodUNSAFE) {
1986 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1988 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
1990 if (pMethod->HasMethodInstantiation())
1992 HELPER_METHOD_FRAME_BEGIN_RET_1(pMethodUNSAFE);
1994 pMethod = pMethod->StripMethodInstantiation();
1996 HELPER_METHOD_FRAME_END();
1999 INT32 tkMethodDef = (INT32)pMethod->GetMemberDef();
2000 _ASSERTE(TypeFromToken(tkMethodDef) == mdtMethodDef);
2002 if (IsNilToken(tkMethodDef) || TypeFromToken(tkMethodDef) != mdtMethodDef)
2003 return mdMethodDefNil;
2009 FCIMPL6(void, SignatureNative::GetSignature,
2010 SignatureNative* pSignatureNativeUNSAFE,
2011 PCCOR_SIGNATURE pCorSig, DWORD cCorSig,
2012 FieldDesc *pFieldDesc, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE) {
2015 PRECONDITION(pDeclaringTypeUNSAFE || pMethodUNSAFE->GetMethod()->IsDynamicMethod());
2016 PRECONDITION(CheckPointer(pCorSig, NULL_OK));
2017 PRECONDITION(CheckPointer(pMethodUNSAFE, NULL_OK));
2018 PRECONDITION(CheckPointer(pFieldDesc, NULL_OK));
2024 REFLECTCLASSBASEREF refDeclaringType;
2025 REFLECTMETHODREF refMethod;
2026 SIGNATURENATIVEREF pSig;
2029 gc.refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2030 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2031 gc.pSig = (SIGNATURENATIVEREF)pSignatureNativeUNSAFE;
2033 MethodDesc *pMethod;
2034 TypeHandle declType;
2036 if (gc.refDeclaringType == NULL)
2038 // for dynamic method, see precondition
2039 pMethod = gc.refMethod->GetMethod();
2040 declType = pMethod->GetMethodTable();
2044 pMethod = gc.refMethod != NULL ? gc.refMethod->GetMethod() : NULL;
2045 declType = gc.refDeclaringType->GetType();
2048 HELPER_METHOD_FRAME_BEGIN_PROTECT(gc);
2050 Module* pModule = declType.GetModule();
2054 pMethod->GetSig(&pCorSig, &cCorSig);
2055 if (pMethod->GetClassification() == mcInstantiated)
2057 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2058 if (pLoaderAllocator->IsCollectible())
2059 gc.pSig->SetKeepAlive(pLoaderAllocator->GetExposedObject());
2062 else if (pFieldDesc)
2063 pFieldDesc->GetSig(&pCorSig, &cCorSig);
2065 gc.pSig->m_sig = pCorSig;
2066 gc.pSig->m_cSig = cCorSig;
2067 gc.pSig->m_pMethod = pMethod;
2069 REFLECTCLASSBASEREF refDeclType = (REFLECTCLASSBASEREF)declType.GetManagedClassObject();
2070 gc.pSig->SetDeclaringType(refDeclType);
2072 PREFIX_ASSUME(pCorSig!= NULL);
2073 BYTE callConv = *(BYTE*)pCorSig;
2074 SigTypeContext typeContext;
2076 SigTypeContext::InitTypeContext(
2077 pMethod, declType.GetClassOrArrayInstantiation(), pMethod->LoadMethodInstantiation(), &typeContext);
2079 SigTypeContext::InitTypeContext(declType, &typeContext);
2080 MetaSig msig(pCorSig, cCorSig, pModule, &typeContext,
2081 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
2083 if (callConv == IMAGE_CEE_CS_CALLCONV_FIELD)
2085 msig.NextArgNormalized();
2087 OBJECTREF refRetType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2088 gc.pSig->SetReturnType(refRetType);
2092 gc.pSig->SetCallingConvention(msig.GetCallingConventionInfo());
2094 OBJECTREF refRetType = msig.GetRetTypeHandleThrowing().GetManagedClassObject();
2095 gc.pSig->SetReturnType(refRetType);
2097 INT32 nArgs = msig.NumFixedArgs();
2098 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(g_pRuntimeTypeClass), ELEMENT_TYPE_SZARRAY);
2100 PTRARRAYREF ptrArrayarguments = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &nArgs, 1);
2101 gc.pSig->SetArgumentArray(ptrArrayarguments);
2103 for (INT32 i = 0; i < nArgs; i++)
2107 OBJECTREF refArgType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2108 gc.pSig->SetArgument(i, refArgType);
2111 _ASSERTE(gc.pSig->m_returnType != NULL);
2114 HELPER_METHOD_FRAME_END();
2118 FCIMPL2(FC_BOOL_RET, SignatureNative::CompareSig, SignatureNative* pLhsUNSAFE, SignatureNative* pRhsUNSAFE)
2126 SIGNATURENATIVEREF pLhs;
2127 SIGNATURENATIVEREF pRhs;
2130 gc.pLhs = (SIGNATURENATIVEREF)pLhsUNSAFE;
2131 gc.pRhs = (SIGNATURENATIVEREF)pRhsUNSAFE;
2133 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2135 ret = MetaSig::CompareMethodSigs(
2136 gc.pLhs->GetCorSig(), gc.pLhs->GetCorSigSize(), gc.pLhs->GetModule(), NULL,
2137 gc.pRhs->GetCorSig(), gc.pRhs->GetCorSigSize(), gc.pRhs->GetModule(), NULL);
2139 HELPER_METHOD_FRAME_END();
2140 FC_RETURN_BOOL(ret);
2144 void QCALLTYPE RuntimeMethodHandle::GetMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
2149 Instantiation inst = pMethod->LoadMethodInstantiation();
2152 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
2158 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::HasMethodInstantiation, MethodDesc * pMethod)
2162 FC_RETURN_BOOL(pMethod->HasMethodInstantiation());
2166 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsGenericMethodDefinition, MethodDesc * pMethod)
2170 FC_RETURN_BOOL(pMethod->IsGenericMethodDefinition());
2174 FCIMPL1(INT32, RuntimeMethodHandle::GetGenericParameterCount, MethodDesc * pMethod)
2178 return pMethod->GetNumGenericMethodArgs();
2182 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsDynamicMethod, MethodDesc * pMethod)
2186 FC_RETURN_BOOL(pMethod->IsNoMetadata());
2190 FCIMPL1(Object*, RuntimeMethodHandle::GetResolver, MethodDesc * pMethod)
2195 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2197 OBJECTREF resolver = NULL;
2198 if (pMethod->IsLCGMethod())
2200 resolver = pMethod->AsDynamicMethodDesc()->GetLCGMethodResolver()->GetManagedResolver();
2202 return OBJECTREFToObject(resolver);
2206 void QCALLTYPE RuntimeMethodHandle::Destroy(MethodDesc * pMethod)
2212 if (pMethod == NULL)
2213 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2215 DynamicMethodDesc* pDynamicMethodDesc = pMethod->AsDynamicMethodDesc();
2219 // Destroy should be called only if the managed part is gone.
2220 _ASSERTE(OBJECTREFToObject(pDynamicMethodDesc->GetLCGMethodResolver()->GetManagedResolver()) == NULL);
2222 // Fire Unload Dynamic Method Event here
2223 ETW::MethodLog::DynamicMethodDestroyed(pMethod);
2225 BEGIN_PIN_PROFILER(CORProfilerIsMonitoringDynamicFunctionUnloads());
2226 g_profControlBlock.pProfInterface->DynamicMethodUnloaded((FunctionID)pMethod);
2229 pDynamicMethodDesc->Destroy();
2234 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsTypicalMethodDefinition, ReflectMethodObject *pMethodUNSAFE)
2239 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2241 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2243 FC_RETURN_BOOL(pMethod->IsTypicalMethodDefinition());
2247 void QCALLTYPE RuntimeMethodHandle::GetTypicalMethodDefinition(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2255 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2258 MethodDesc *pMethodTypical = pMethod->LoadTypicalMethodDefinition();
2259 if (pMethodTypical != pMethod)
2262 refMethod.Set(pMethodTypical->GetStubMethodInfo());
2269 void QCALLTYPE RuntimeMethodHandle::StripMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2276 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2281 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2284 MethodDesc *pMethodStripped = pMethod->StripMethodInstantiation();
2285 if (pMethodStripped != pMethod)
2288 refMethod.Set(pMethodStripped->GetStubMethodInfo());
2295 // In the VM there might be more than one MethodDescs for a "method"
2296 // examples are methods on generic types which may have additional instantiating stubs
2297 // and methods on value types which may have additional unboxing stubs.
2299 // For generic methods we always hand out an instantiating stub except for a generic method definition
2300 // For non-generic methods on generic types we need an instantiating stub if it's one of the following
2301 // - static method on a generic class
2302 // - static or instance method on a generic interface
2303 // - static or instance method on a generic value type
2304 // The Reflection policy is to always hand out instantiating stubs in these cases
2306 // For methods on non-generic value types we can use either the cannonical method or the unboxing stub
2307 // The Reflection policy is to always hand out unboxing stubs if the methods are virtual methods
2308 // The reason for this is that in the current implementation of the class loader, the v-table slots for
2309 // those methods point to unboxing stubs already. Note that this is just a implementation choice
2310 // that might change in the future. But we should always keep this Reflection policy an invariant.
2312 // For virtual methods on generic value types (intersection of the two cases), reflection will hand
2313 // out an unboxing instantiating stub
2315 // GetInstantiatingStub is called to:
2316 // 1. create an InstantiatedMethodDesc for a generic method when calling BindGenericArguments() on a generic
2317 // method. In this case instArray will not be null.
2318 // 2. create an InstantiatedMethodDesc for a method in a generic class. In this case instArray will be null.
2319 // 3. create an UnboxingStub for a method in a value type. In this case instArray will be null.
2320 // For case 2 and 3, an instantiating stub or unboxing stub might not be needed in which case the original
2321 // MethodDesc is returned.
2322 FCIMPL3(MethodDesc*, RuntimeMethodHandle::GetStubIfNeeded,
2323 MethodDesc *pMethod,
2324 ReflectClassBaseObject *pTypeUNSAFE,
2325 PtrArray* instArrayUNSAFE)
2332 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2333 PTRARRAYREF instArray = (PTRARRAYREF)ObjectToOBJECTREF(instArrayUNSAFE);
2335 if (refType == NULL)
2336 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2338 TypeHandle instType = refType->GetType();
2339 MethodDesc *pNewMethod = pMethod;
2343 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
2345 if (instType.IsNull())
2346 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2348 // Perf optimization: this logic is actually duplicated in FindOrCreateAssociatedMethodDescForReflection, but since it
2349 // is the more common case it's worth the duplicate check here to avoid the helper method frame
2350 if ( instArray == NULL &&
2351 ( pMethod->HasMethodInstantiation() ||
2352 ( !instType.IsValueType() &&
2353 ( !instType.HasInstantiation() || instType.IsGenericTypeDefinition() ) ) ) )
2358 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, instArray);
2360 TypeHandle *inst = NULL;
2363 if (instArray != NULL)
2365 ntypars = instArray->GetNumComponents();
2367 size_t size = ntypars * sizeof(TypeHandle);
2368 if ((size / sizeof(TypeHandle)) != ntypars) // uint over/underflow
2369 COMPlusThrow(kArgumentException);
2370 inst = (TypeHandle*) _alloca(size);
2372 for (DWORD i = 0; i < ntypars; i++)
2374 REFLECTCLASSBASEREF instRef = (REFLECTCLASSBASEREF)instArray->GetAt(i);
2376 if (instRef == NULL)
2377 COMPlusThrowArgumentNull(W("inst"), W("ArgumentNull_ArrayElement"));
2379 inst[i] = instRef->GetType();
2383 pNewMethod = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMethod, instType, Instantiation(inst, ntypars));
2385 HELPER_METHOD_FRAME_END();
2392 FCIMPL2(MethodDesc*, RuntimeMethodHandle::GetMethodFromCanonical, MethodDesc *pMethod, ReflectClassBaseObject *pTypeUNSAFE)
2396 PRECONDITION(CheckPointer(pMethod));
2400 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2402 TypeHandle instType = refType->GetType();
2403 MethodDesc* pMDescInCanonMT = instType.GetMethodTable()->GetParallelMethodDesc(pMethod);
2405 return pMDescInCanonMT;
2410 FCIMPL2(RuntimeMethodBody *, RuntimeMethodHandle::GetMethodBody, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2420 RUNTIMEMETHODBODYREF MethodBodyObj;
2421 RUNTIMEEXCEPTIONHANDLINGCLAUSEREF EHClauseObj;
2422 RUNTIMELOCALVARIABLEINFOREF RuntimeLocalVariableInfoObj;
2424 BASEARRAYREF TempArray;
2425 REFLECTCLASSBASEREF declaringType;
2426 REFLECTMETHODREF refMethod;
2429 gc.MethodBodyObj = NULL;
2430 gc.EHClauseObj = NULL;
2431 gc.RuntimeLocalVariableInfoObj = NULL;
2433 gc.TempArray = NULL;
2434 gc.declaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2435 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2439 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2441 MethodDesc* pMethod = gc.refMethod->GetMethod();
2443 TypeHandle declaringType = gc.declaringType == NULL ? TypeHandle() : gc.declaringType->GetType();
2445 if (!pMethod->IsIL())
2448 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2450 MethodDesc *pMethodIL = pMethod;
2451 if (pMethod->IsWrapperStub())
2452 pMethodIL = pMethod->GetWrappedMethodDesc();
2454 COR_ILMETHOD* pILHeader = pMethodIL->GetILHeader();
2458 MethodTable * pExceptionHandlingClauseMT = MscorlibBinder::GetClass(CLASS__RUNTIME_EH_CLAUSE);
2459 TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);
2461 MethodTable * pLocalVariableMT = MscorlibBinder::GetClass(CLASS__RUNTIME_LOCAL_VARIABLE_INFO);
2462 TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);
2464 Module* pModule = pMethod->GetModule();
2465 COR_ILMETHOD_DECODER::DecoderStatus status;
2466 COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);
2468 if (status != COR_ILMETHOD_DECODER::SUCCESS)
2470 if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
2472 // Throw a verification HR
2473 COMPlusThrowHR(COR_E_VERIFICATION);
2477 COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
2481 gc.MethodBodyObj = (RUNTIMEMETHODBODYREF)AllocateObject(MscorlibBinder::GetClass(CLASS__RUNTIME_METHOD_BODY));
2483 gc.MethodBodyObj->_maxStackSize = header.GetMaxStack();
2484 gc.MethodBodyObj->_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);
2487 gc.MethodBodyObj->_localVarSigToken = header.GetLocalVarSigTok();
2489 gc.MethodBodyObj->_localVarSigToken = 0;
2491 // Allocate the array of IL and fill it in from the method header.
2492 BYTE* pIL = const_cast<BYTE*>(header.Code);
2493 COUNT_T cIL = header.GetCodeSize();
2494 gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);
2496 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_IL, gc.U1Array, GetAppDomain());
2497 memcpyNoGCRefs(gc.MethodBodyObj->_IL->GetDataPtr(), pIL, cIL);
2499 // Allocate the array of exception clauses.
2500 INT32 cEh = (INT32)header.EHCount();
2501 const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
2502 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thEHClauseArray, &cEh, 1);
2504 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_exceptionClauses, gc.TempArray, GetAppDomain());
2506 for (INT32 i = 0; i < cEh; i++)
2508 COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
2509 const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
2510 (const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);
2512 gc.EHClauseObj = (RUNTIMEEXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);
2514 gc.EHClauseObj->_flags = ehClause->GetFlags();
2515 gc.EHClauseObj->_tryOffset = ehClause->GetTryOffset();
2516 gc.EHClauseObj->_tryLength = ehClause->GetTryLength();
2517 gc.EHClauseObj->_handlerOffset = ehClause->GetHandlerOffset();
2518 gc.EHClauseObj->_handlerLength = ehClause->GetHandlerLength();
2520 if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
2521 gc.EHClauseObj->_catchToken = ehClause->GetClassToken();
2523 gc.EHClauseObj->_filterOffset = ehClause->GetFilterOffset();
2525 gc.MethodBodyObj->_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
2526 SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->_methodBody), (OBJECTREF)gc.MethodBodyObj, GetAppDomain());
2529 if (header.LocalVarSig != NULL)
2531 SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
2532 MetaSig metaSig(header.LocalVarSig,
2533 header.cbLocalVarSig,
2536 MetaSig::sigLocalVars);
2537 INT32 cLocals = metaSig.NumFixedArgs();
2538 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2539 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray, GetAppDomain());
2541 for (INT32 i = 0; i < cLocals; i ++)
2543 gc.RuntimeLocalVariableInfoObj = (RUNTIMELOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);
2545 gc.RuntimeLocalVariableInfoObj->_localIndex = i;
2549 CorElementType eType;
2550 IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
2551 if (ELEMENT_TYPE_PINNED == eType)
2552 gc.RuntimeLocalVariableInfoObj->_isPinned = TRUE;
2554 TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
2555 OBJECTREF refLocalType = tempType.GetManagedClassObject();
2556 gc.RuntimeLocalVariableInfoObj->SetType(refLocalType);
2557 gc.MethodBodyObj->_localVariables->SetAt(i, (OBJECTREF) gc.RuntimeLocalVariableInfoObj);
2563 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2564 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray, GetAppDomain());
2568 HELPER_METHOD_FRAME_END();
2570 return (RuntimeMethodBody*)OBJECTREFToObject(gc.MethodBodyObj);
2574 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsConstructor, MethodDesc *pMethod)
2578 PRECONDITION(CheckPointer(pMethod));
2583 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2584 ret = (BOOL)pMethod->IsClassConstructorOrCtor();
2585 END_SO_INTOLERANT_CODE;
2586 FC_RETURN_BOOL(ret);
2590 FCIMPL1(Object*, RuntimeMethodHandle::GetLoaderAllocator, MethodDesc *pMethod)
2597 OBJECTREF loaderAllocator = NULL;
2600 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2602 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(loaderAllocator);
2604 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2605 loaderAllocator = pLoaderAllocator->GetExposedObject();
2607 HELPER_METHOD_FRAME_END();
2609 return OBJECTREFToObject(loaderAllocator);
2613 //*********************************************************************************************
2614 //*********************************************************************************************
2615 //*********************************************************************************************
2617 FCIMPL1(StringObject*, RuntimeFieldHandle::GetName, ReflectFieldObject *pFieldUNSAFE) {
2623 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2625 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2627 FieldDesc *pField = refField->GetField();
2629 STRINGREF refString = NULL;
2630 HELPER_METHOD_FRAME_BEGIN_RET_1(refField);
2632 refString = StringObject::NewString(pField->GetName());
2634 HELPER_METHOD_FRAME_END();
2635 return (StringObject*)OBJECTREFToObject(refString);
2639 FCIMPL1(LPCUTF8, RuntimeFieldHandle::GetUtf8Name, FieldDesc *pField) {
2642 PRECONDITION(CheckPointer(pField));
2646 LPCUTF8 szFieldName;
2648 if (FAILED(pField->GetName_NoThrow(&szFieldName)))
2650 FCThrow(kBadImageFormatException);
2656 FCIMPL2(FC_BOOL_RET, RuntimeFieldHandle::MatchesNameHash, FieldDesc * pField, ULONG hash)
2660 FC_RETURN_BOOL(pField->MightHaveName(hash));
2664 FCIMPL1(INT32, RuntimeFieldHandle::GetAttributes, FieldDesc *pField) {
2671 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2674 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2675 ret = (INT32)pField->GetAttributes();
2676 END_SO_INTOLERANT_CODE;
2681 FCIMPL1(ReflectClassBaseObject*, RuntimeFieldHandle::GetApproxDeclaringType, FieldDesc *pField) {
2688 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2690 TypeHandle th = TypeHandle(pField->GetApproxEnclosingMethodTable()); // <REVISIT_TODO> this needs to be checked - see bug 184355 </REVISIT_TODO>
2691 RETURN_CLASS_OBJECT(th, NULL);
2695 FCIMPL1(INT32, RuntimeFieldHandle::GetToken, ReflectFieldObject *pFieldUNSAFE) {
2701 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2703 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2705 FieldDesc *pField = refField->GetField();
2707 INT32 tkFieldDef = (INT32)pField->GetMemberDef();
2708 _ASSERTE(!IsNilToken(tkFieldDef) || tkFieldDef == mdFieldDefNil);
2713 FCIMPL2(FieldDesc*, RuntimeFieldHandle::GetStaticFieldForGenericType, FieldDesc *pField, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2720 REFLECTCLASSBASEREF refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2722 if ((refDeclaringType == NULL) || (pField == NULL))
2723 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2725 TypeHandle declaringType = refDeclaringType->GetType();
2728 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2729 if (declaringType.IsTypeDesc())
2730 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2731 MethodTable *pMT = declaringType.AsMethodTable();
2733 _ASSERTE(pField->IsStatic());
2734 if (pMT->HasGenericsStaticsInfo())
2735 pField = pMT->GetFieldDescByIndex(pField->GetApproxEnclosingMethodTable()->GetIndexForFieldDesc(pField));
2736 _ASSERTE(!pField->IsSharedByGenericInstantiations());
2737 _ASSERTE(pField->GetEnclosingMethodTable() == pMT);
2743 FCIMPL1(ReflectModuleBaseObject*, AssemblyHandle::GetManifestModule, AssemblyBaseObject* pAssemblyUNSAFE) {
2746 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2748 if (refAssembly == NULL)
2749 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2751 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2752 Assembly* currentAssembly = pAssembly->GetCurrentAssembly();
2754 if (currentAssembly == NULL)
2757 Module *pModule = currentAssembly->GetManifestModule();
2758 DomainFile * pDomainFile = pModule->FindDomainFile(GetAppDomain());
2763 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2764 orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2765 if (orModule == NULL)
2766 orModule = pModule->GetExposedObject();
2768 OBJECTREF orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2769 if (orModule != NULL)
2770 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2772 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2773 orModule = pModule->GetExposedObject();
2776 HELPER_METHOD_FRAME_END();
2777 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2782 FCIMPL1(INT32, AssemblyHandle::GetToken, AssemblyBaseObject* pAssemblyUNSAFE) {
2785 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2787 if (refAssembly == NULL)
2788 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2790 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2791 mdAssembly token = mdAssemblyNil;
2793 IMDInternalImport *mdImport = pAssembly->GetCurrentAssembly()->GetManifestImport();
2797 if (FAILED(mdImport->GetAssemblyFromScope(&token)))
2799 FCThrow(kBadImageFormatException);
2808 void QCALLTYPE ModuleHandle::GetPEKind(QCall::ModuleHandle pModule, DWORD* pdwPEKind, DWORD* pdwMachine)
2813 pModule->GetFile()->GetPEKindAndMachine(pdwPEKind, pdwMachine);
2817 FCIMPL1(INT32, ModuleHandle::GetMDStreamVersion, ReflectModuleBaseObject * pModuleUNSAFE)
2821 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2823 if (refModule == NULL)
2824 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2826 Module *pModule = refModule->GetModule();
2828 if (pModule->IsResource())
2831 return pModule->GetMDImport()->GetMetadataStreamVersion();
2835 void QCALLTYPE ModuleHandle::GetModuleType(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
2839 TypeHandle globalTypeHandle = TypeHandle();
2845 globalTypeHandle = TypeHandle(pModule->GetGlobalMethodTable());
2847 EX_SWALLOW_NONTRANSIENT;
2849 if (!globalTypeHandle.IsNull())
2852 retType.Set(globalTypeHandle.GetManagedClassObject());
2860 FCIMPL1(INT32, ModuleHandle::GetToken, ReflectModuleBaseObject * pModuleUNSAFE) {
2866 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2868 if (refModule == NULL)
2869 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2871 Module *pModule = refModule->GetModule();
2873 if (pModule->IsResource())
2876 return pModule->GetMDImport()->GetModuleFromScope();
2880 FCIMPL1(IMDInternalImport*, ModuleHandle::GetMetadataImport, ReflectModuleBaseObject * pModuleUNSAFE)
2884 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2886 if (refModule == NULL)
2887 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2889 Module *pModule = refModule->GetModule();
2891 if (pModule->IsResource())
2894 return pModule->GetMDImport();
2898 BOOL QCALLTYPE ModuleHandle::ContainsPropertyMatchingHash(QCall::ModuleHandle pModule, INT32 tkProperty, ULONG hash)
2902 BOOL fContains = TRUE;
2906 fContains = pModule->MightContainMatchingProperty(tkProperty, hash);
2913 void QCALLTYPE ModuleHandle::ResolveType(QCall::ModuleHandle pModule, INT32 tkType, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retType)
2917 TypeHandle typeHandle;
2921 _ASSERTE(!IsNilToken(tkType));
2923 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2924 typeHandle = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, tkType, &typeContext,
2925 ClassLoader::ThrowIfNotFound,
2926 ClassLoader::PermitUninstDefOrRef);
2929 retType.Set(typeHandle.GetManagedClassObject());
2936 MethodDesc *QCALLTYPE ModuleHandle::ResolveMethod(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount)
2940 MethodDesc* pMD = NULL;
2944 _ASSERTE(!IsNilToken(tkMemberRef));
2946 BOOL strictMetadataChecks = (TypeFromToken(tkMemberRef) == mdtMethodSpec);
2948 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2949 pMD = MemberLoader::GetMethodDescFromMemberDefOrRefOrSpec(pModule, tkMemberRef, &typeContext, strictMetadataChecks, FALSE);
2951 // This will get us the instantiating or unboxing stub if needed
2952 pMD = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMD, pMD->GetMethodTable(), pMD->GetMethodInstantiation());
2959 void QCALLTYPE ModuleHandle::ResolveField(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retField)
2963 FieldDesc* pField = NULL;
2967 _ASSERTE(!IsNilToken(tkMemberRef));
2969 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2970 pField = MemberLoader::GetFieldDescFromMemberDefOrRef(pModule, tkMemberRef, &typeContext, FALSE);
2972 retField.Set(pField->GetStubFieldInfo());
2979 void QCALLTYPE ModuleHandle::GetAssembly(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retAssembly)
2983 DomainAssembly *pAssembly = NULL;
2986 pAssembly = pModule->GetDomainAssembly();
2989 retAssembly.Set(pAssembly->GetExposedAssemblyObject());
2995 FCIMPL5(ReflectMethodObject*, ModuleHandle::GetDynamicMethod, ReflectMethodObject *pMethodUNSAFE, ReflectModuleBaseObject *pModuleUNSAFE, StringObject *name, U1Array *sig, Object *resolver) {
2998 PRECONDITION(CheckPointer(name));
2999 PRECONDITION(CheckPointer(sig));
3003 DynamicMethodDesc *pNewMD = NULL;
3008 OBJECTREF resolverRef;
3009 OBJECTREF methodRef;
3010 REFLECTMETHODREF retMethod;
3011 REFLECTMODULEBASEREF refModule;
3013 gc.nameRef = (STRINGREF)name;
3014 gc.resolverRef = (OBJECTREF)resolver;
3015 gc.methodRef = ObjectToOBJECTREF(pMethodUNSAFE);
3016 gc.retMethod = NULL;
3017 gc.refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
3019 if (gc.refModule == NULL)
3020 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
3022 Module *pModule = gc.refModule->GetModule();
3024 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
3026 DomainFile *pDomainModule = pModule->GetDomainFile();
3028 U1ARRAYREF dataArray = (U1ARRAYREF)sig;
3029 DWORD sigSize = dataArray->GetNumComponents();
3030 NewHolder<BYTE> pSig(new BYTE[sigSize]);
3031 memcpy(pSig, dataArray->GetDataPtr(), sigSize);
3033 DWORD length = gc.nameRef->GetStringLength();
3034 NewArrayHolder<char> pName(new char[(length + 1) * 2]);
3036 length = WszWideCharToMultiByte(CP_UTF8, 0, gc.nameRef->GetBuffer(), length, pName, (length + 1) * 2 - sizeof(char), NULL, NULL);
3038 pName[length / sizeof(char)] = '\0';
3040 DynamicMethodTable *pMTForDynamicMethods = pDomainModule->GetDynamicMethodTable();
3041 pNewMD = pMTForDynamicMethods->GetDynamicMethod(pSig, sigSize, pName);
3042 _ASSERTE(pNewMD != NULL);
3043 // pNewMD now owns pSig and pName.
3044 pSig.SuppressRelease();
3045 pName.SuppressRelease();
3047 // create a handle to hold the resolver objectref
3048 OBJECTHANDLE resolverHandle = pDomainModule->GetAppDomain()->CreateLongWeakHandle(gc.resolverRef);
3049 pNewMD->GetLCGMethodResolver()->SetManagedResolver(resolverHandle);
3050 gc.retMethod = pNewMD->GetStubMethodInfo();
3051 gc.retMethod->SetKeepAlive(gc.resolverRef);
3053 LoaderAllocator *pLoaderAllocator = pModule->GetLoaderAllocator();
3055 if (pLoaderAllocator->IsCollectible())
3056 pLoaderAllocator->AddReference();
3058 HELPER_METHOD_FRAME_END();
3060 return (ReflectMethodObject*)OBJECTREFToObject(gc.retMethod);
3064 void QCALLTYPE RuntimeMethodHandle::GetCallerType(QCall::StackCrawlMarkHandle pStackMark, QCall::ObjectHandleOnStack retType)
3070 MethodTable *pMT = NULL;
3072 pMT = SystemDomain::GetCallersType(pStackMark);
3075 retType.Set(pMT->GetManagedClassObject());