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 "objecthandle.h"
26 #include "interoputil.h"
28 #include "virtualcallstub.h"
29 #include "contractimpl.h"
30 #include "dynamicmethod.h"
31 #include "peimagelayout.inl"
33 #include "eventtrace.h"
34 #include "invokeutil.h"
37 FCIMPL3(FC_BOOL_RET, Utf8String::EqualsCaseSensitive, LPCUTF8 szLhs, LPCUTF8 szRhs, INT32 stringNumBytes)
41 PRECONDITION(CheckPointer(szLhs));
42 PRECONDITION(CheckPointer(szRhs));
46 // Important: the string in pSsz isn't null terminated so the length must be used
47 // when performing operations on the string.
49 // At this point, both the left and right strings are guaranteed to have the
51 FC_RETURN_BOOL(strncmp(szLhs, szRhs, stringNumBytes) == 0);
55 BOOL QCALLTYPE Utf8String::EqualsCaseInsensitive(LPCUTF8 szLhs, LPCUTF8 szRhs, INT32 stringNumBytes)
59 // Important: the string in pSsz isn't null terminated so the length must be used
60 // when performing operations on the string.
62 BOOL fStringsEqual = FALSE;
66 _ASSERTE(CheckPointer(szLhs));
67 _ASSERTE(CheckPointer(szRhs));
69 // At this point, both the left and right strings are guaranteed to have the
71 StackSString lhs(SString::Utf8, szLhs, stringNumBytes);
72 StackSString rhs(SString::Utf8, szRhs, stringNumBytes);
74 // We can use SString for simple case insensitive compares
75 fStringsEqual = lhs.EqualsCaseInsensitive(rhs);
82 ULONG QCALLTYPE Utf8String::HashCaseInsensitive(LPCUTF8 sz, INT32 stringNumBytes)
86 // Important: the string in pSsz isn't null terminated so the length must be used
87 // when performing operations on the string.
93 StackSString str(SString::Utf8, sz, stringNumBytes);
94 hashValue = str.HashCaseInsensitive();
101 static BOOL CheckCAVisibilityFromDecoratedType(MethodTable* pCAMT, MethodDesc* pCACtor, MethodTable* pDecoratedMT, Module* pDecoratedModule)
108 PRECONDITION(CheckPointer(pCAMT));
109 PRECONDITION(CheckPointer(pCACtor, NULL_OK));
110 PRECONDITION(CheckPointer(pDecoratedMT, NULL_OK));
111 PRECONDITION(CheckPointer(pDecoratedModule));
115 DWORD dwAttr = mdPublic;
119 // Allowing a dangerous method to be called in custom attribute instantiation is, well, dangerous.
120 // E.g. a malicious user can craft a custom attribute record that fools us into creating a DynamicMethod
121 // object attached to typeof(System.Reflection.CustomAttribute) and thus gain access to mscorlib internals.
122 if (InvokeUtil::IsDangerousMethod(pCACtor))
125 _ASSERTE(pCACtor->IsCtor());
127 dwAttr = pCACtor->GetAttrs();
130 StaticAccessCheckContext accessContext(NULL, pDecoratedMT, pDecoratedModule->GetAssembly());
132 return ClassLoader::CanAccess(
135 pCAMT->GetAssembly(),
139 *AccessCheckOptions::s_pNormalAccessChecks,
144 BOOL QCALLTYPE RuntimeMethodHandle::IsCAVisibleFromDecoratedType(
145 EnregisteredTypeHandle targetTypeHandle,
146 MethodDesc * pTargetCtor,
147 EnregisteredTypeHandle sourceTypeHandle,
148 QCall::ModuleHandle sourceModuleHandle)
155 TypeHandle sourceHandle = TypeHandle::FromPtr(sourceTypeHandle);
156 TypeHandle targetHandle = TypeHandle::FromPtr(targetTypeHandle);
158 _ASSERTE((sourceHandle.IsNull() || !sourceHandle.IsTypeDesc()) &&
159 !targetHandle.IsNull() &&
160 !targetHandle.IsTypeDesc());
162 if (sourceHandle.IsTypeDesc() ||
163 targetHandle.IsNull() ||
164 targetHandle.IsTypeDesc())
165 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
167 bResult = CheckCAVisibilityFromDecoratedType(targetHandle.AsMethodTable(), pTargetCtor, sourceHandle.AsMethodTable(), sourceModuleHandle);
174 NOINLINE static ReflectClassBaseObject* GetRuntimeTypeHelper(LPVOID __me, TypeHandle typeHandle, OBJECTREF keepAlive)
176 FC_INNER_PROLOG_NO_ME_SETUP();
177 if (typeHandle.AsPtr() == NULL)
180 // RuntimeTypeHandle::GetRuntimeType has picked off the most common case, but does not cover array types.
181 // Before we do the really heavy weight option of setting up a helper method frame, check if we have to.
182 OBJECTREF refType = typeHandle.GetManagedClassObjectFast();
184 return (ReflectClassBaseObject*)OBJECTREFToObject(refType);
186 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
187 refType = typeHandle.GetManagedClassObject();
188 HELPER_METHOD_FRAME_END();
191 return (ReflectClassBaseObject*)OBJECTREFToObject(refType);
194 #define RETURN_CLASS_OBJECT(typeHandle, keepAlive) FC_INNER_RETURN(ReflectClassBaseObject*, GetRuntimeTypeHelper(__me, typeHandle, keepAlive))
196 NOINLINE ReflectModuleBaseObject* GetRuntimeModuleHelper(LPVOID __me, Module *pModule, OBJECTREF keepAlive)
198 FC_INNER_PROLOG_NO_ME_SETUP();
202 DomainFile * pDomainFile = pModule->FindDomainFile(GetAppDomain());
204 OBJECTREF refModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
206 if(refModule != NULL)
207 return (ReflectModuleBaseObject*)OBJECTREFToObject(refModule);
209 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
210 refModule = pModule->GetExposedObject();
211 HELPER_METHOD_FRAME_END();
214 return (ReflectModuleBaseObject*)OBJECTREFToObject(refModule);
217 NOINLINE AssemblyBaseObject* GetRuntimeAssemblyHelper(LPVOID __me, DomainAssembly *pAssembly, OBJECTREF keepAlive)
219 FC_INNER_PROLOG_NO_ME_SETUP();
220 if (pAssembly == NULL)
223 OBJECTREF refAssembly = (pAssembly != NULL) ? pAssembly->GetExposedAssemblyObjectIfExists() : NULL;
225 if(refAssembly != NULL)
226 return (AssemblyBaseObject*)OBJECTREFToObject(refAssembly);
228 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, keepAlive);
229 refAssembly = pAssembly->GetExposedAssemblyObject();
230 HELPER_METHOD_FRAME_END();
233 return (AssemblyBaseObject*)OBJECTREFToObject(refAssembly);
237 // This is the routine that is called by the 'typeof()' operator in C#. It is one of the most commonly used
238 // reflection operations. This call should be optimized away in nearly all situations
239 FCIMPL1_V(ReflectClassBaseObject*, RuntimeTypeHandle::GetTypeFromHandle, FCALLRuntimeTypeHandle th)
244 return FCALL_RTH_TO_REFLECTCLASS(th);
248 FCIMPL1(ReflectClassBaseObject*, RuntimeTypeHandle::GetRuntimeType, EnregisteredTypeHandle th)
252 TypeHandle typeHandle = TypeHandle::FromPtr(th);
253 _ASSERTE(CheckPointer(typeHandle.AsPtr(), NULL_OK));
254 if (typeHandle.AsPtr()!= NULL)
256 if (!typeHandle.IsTypeDesc())
258 OBJECTREF typePtr = typeHandle.AsMethodTable()->GetManagedClassObjectIfExists();
261 return (ReflectClassBaseObject*)OBJECTREFToObject(typePtr);
268 RETURN_CLASS_OBJECT(typeHandle, NULL);
272 FCIMPL1_V(EnregisteredTypeHandle, RuntimeTypeHandle::GetValueInternal, FCALLRuntimeTypeHandle RTH)
276 if (FCALL_RTH_TO_REFLECTCLASS(RTH) == NULL)
279 return FCALL_RTH_TO_REFLECTCLASS(RTH) ->GetType().AsPtr();
283 // TypeEqualsHelper and TypeNotEqualsHelper are almost identical.
284 // Unfortunately we cannot combime them because they need to hardcode the caller's name
285 NOINLINE static BOOL TypeEqualSlow(OBJECTREF refL, OBJECTREF refR, LPVOID __me)
289 FC_INNER_PROLOG_NO_ME_SETUP();
291 _ASSERTE(refL != NULL && refR != NULL);
293 HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_2(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2, refL, refR);
295 MethodDescCallSite TypeEqualsMethod(METHOD__OBJECT__EQUALS, &refL);
303 ret = TypeEqualsMethod.Call_RetBool(args);
305 HELPER_METHOD_FRAME_END();
314 #include <optsmallperfcritical.h>
316 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::TypeEQ, Object* left, Object* right)
320 OBJECTREF refL = (OBJECTREF)left;
321 OBJECTREF refR = (OBJECTREF)right;
325 FC_RETURN_BOOL(TRUE);
330 FC_RETURN_BOOL(FALSE);
333 if ((refL->GetMethodTable() == g_pRuntimeTypeClass || refR->GetMethodTable() == g_pRuntimeTypeClass))
335 // Quick path for negative common case
336 FC_RETURN_BOOL(FALSE);
339 // The fast path didn't get us the result
340 // Let's try the slow path: refL.Equals(refR);
341 FC_INNER_RETURN(FC_BOOL_RET, (FC_BOOL_RET)(!!TypeEqualSlow(refL, refR, __me)));
345 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::TypeNEQ, Object* left, Object* right)
349 OBJECTREF refL = (OBJECTREF)left;
350 OBJECTREF refR = (OBJECTREF)right;
354 FC_RETURN_BOOL(FALSE);
359 FC_RETURN_BOOL(TRUE);
362 if ((refL->GetMethodTable() == g_pRuntimeTypeClass || refR->GetMethodTable() == g_pRuntimeTypeClass))
364 // Quick path for negative common case
365 FC_RETURN_BOOL(TRUE);
368 // The fast path didn't get us the result
369 // Let's try the slow path: refL.Equals(refR);
370 FC_INNER_RETURN(FC_BOOL_RET, (FC_BOOL_RET)(!TypeEqualSlow(refL, refR, __me)));
374 #include <optdefault.h>
379 #ifdef FEATURE_COMINTEROP
380 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsWindowsRuntimeObjectType, ReflectClassBaseObject *rtTypeUNSAFE)
384 BOOL isWindowsRuntimeType = FALSE;
386 TypeHandle typeHandle = rtTypeUNSAFE->GetType();
387 MethodTable *pMT = typeHandle.GetMethodTable();
391 isWindowsRuntimeType = pMT->IsWinRTObjectType();
394 FC_RETURN_BOOL(isWindowsRuntimeType);
398 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsTypeExportedToWindowsRuntime, ReflectClassBaseObject *rtTypeUNSAFE)
402 BOOL isExportedToWinRT = FALSE;
404 TypeHandle typeHandle = rtTypeUNSAFE->GetType();
405 MethodTable *pMT = typeHandle.GetMethodTable();
409 isExportedToWinRT = pMT->IsExportedToWinRT();
412 FC_RETURN_BOOL(isExportedToWinRT);
415 #endif // FEATURE_COMINTEROP
417 NOINLINE static MethodDesc * RestoreMethodHelper(MethodDesc * pMethod, LPVOID __me)
419 FC_INNER_PROLOG_NO_ME_SETUP();
421 HELPER_METHOD_FRAME_BEGIN_RET_0();
422 pMethod->CheckRestore();
423 HELPER_METHOD_FRAME_END();
430 FCIMPL1(MethodDesc *, RuntimeTypeHandle::GetFirstIntroducedMethod, ReflectClassBaseObject *pTypeUNSAFE) {
433 PRECONDITION(CheckPointer(pTypeUNSAFE));
437 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
438 TypeHandle typeHandle = refType->GetType();
440 if (typeHandle.IsGenericVariable())
441 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
443 if (typeHandle.IsTypeDesc()) {
444 if (!typeHandle.IsArray())
448 MethodTable* pMT = typeHandle.GetMethodTable();
452 MethodDesc* pMethod = MethodTable::IntroducedMethodIterator::GetFirst(pMT);
454 // The only method that can show up here unrestored is instantiated methods. Check for it before performing the expensive IsRestored() check.
455 if (pMethod != NULL && pMethod->GetClassification() == mcInstantiated && !pMethod->IsRestored()) {
456 FC_INNER_RETURN(MethodDesc *, RestoreMethodHelper(pMethod, __me));
459 _ASSERTE(pMethod == NULL || pMethod->IsRestored());
464 #include <optsmallperfcritical.h>
465 FCIMPL1(void, RuntimeTypeHandle::GetNextIntroducedMethod, MethodDesc ** ppMethod) {
468 PRECONDITION(CheckPointer(ppMethod));
469 PRECONDITION(CheckPointer(*ppMethod));
473 MethodDesc *pMethod = MethodTable::IntroducedMethodIterator::GetNext(*ppMethod);
477 if (pMethod != NULL && pMethod->GetClassification() == mcInstantiated && !pMethod->IsRestored()) {
478 FC_INNER_RETURN_VOID(RestoreMethodHelper(pMethod, __me));
481 _ASSERTE(pMethod == NULL || pMethod->IsRestored());
484 #include <optdefault.h>
486 FCIMPL1(INT32, RuntimeTypeHandle::GetCorElementType, ReflectClassBaseObject *pTypeUNSAFE) {
492 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
495 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
497 return refType->GetType().GetSignatureCorElementType();
501 FCIMPL1(AssemblyBaseObject*, RuntimeTypeHandle::GetAssembly, ReflectClassBaseObject *pTypeUNSAFE) {
507 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
510 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
512 DomainFile *pDomainFile = NULL;
514 Module *pModule = refType->GetType().GetAssembly()->GetManifestModule();
516 pDomainFile = pModule->FindDomainFile(GetAppDomain());
517 #ifdef FEATURE_LOADER_OPTIMIZATION
518 if (pDomainFile == NULL)
520 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
522 pDomainFile = GetAppDomain()->LoadDomainNeutralModuleDependency(pModule, FILE_LOADED);
524 HELPER_METHOD_FRAME_END();
526 #endif // FEATURE_LOADER_OPTIMIZATION
529 FC_RETURN_ASSEMBLY_OBJECT((DomainAssembly *)pDomainFile, refType);
534 FCIMPL1(FC_BOOL_RET, RuntimeFieldHandle::AcquiresContextFromThis, FieldDesc *pField)
538 PRECONDITION(CheckPointer(pField));
542 FC_RETURN_BOOL(pField->IsSharedByGenericInstantiations());
547 FCIMPL1(ReflectModuleBaseObject*, RuntimeTypeHandle::GetModule, ReflectClassBaseObject *pTypeUNSAFE) {
555 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
558 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
560 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
562 result = refType->GetType().GetModule();
564 END_SO_INTOLERANT_CODE;
566 FC_RETURN_MODULE_OBJECT(result, refType);
570 FCIMPL1(ReflectClassBaseObject *, RuntimeTypeHandle::GetBaseType, ReflectClassBaseObject *pTypeUNSAFE) {
576 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
579 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
581 TypeHandle typeHandle = refType->GetType();
583 if (typeHandle.IsGenericVariable())
584 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
586 if (typeHandle.IsTypeDesc()) {
587 if (!typeHandle.IsArray())
591 RETURN_CLASS_OBJECT(typeHandle.GetParent(), refType);
595 FCIMPL1(ReflectClassBaseObject *, RuntimeTypeHandle::GetElementType, ReflectClassBaseObject *pTypeUNSAFE) {
601 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
604 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
606 TypeHandle typeHandle = refType->GetType();
608 if (!typeHandle.IsTypeDesc())
611 if (typeHandle.IsGenericVariable())
614 TypeHandle typeReturn;
616 if (typeHandle.IsArray())
617 typeReturn = typeHandle.AsArray()->GetArrayElementTypeHandle();
619 typeReturn = typeHandle.AsTypeDesc()->GetTypeParam();
621 RETURN_CLASS_OBJECT(typeReturn, refType);
625 FCIMPL1(INT32, RuntimeTypeHandle::GetArrayRank, ReflectClassBaseObject *pTypeUNSAFE) {
628 PRECONDITION(CheckPointer(pTypeUNSAFE));
629 PRECONDITION(pTypeUNSAFE->GetType().IsArray());
633 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
635 return (INT32)refType->GetType().AsArray()->GetRank();
639 FCIMPL1(INT32, RuntimeTypeHandle::GetNumVirtuals, ReflectClassBaseObject *pTypeUNSAFE) {
645 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
648 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
650 TypeHandle typeHandle = refType->GetType();
652 if (typeHandle.IsGenericVariable())
653 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
655 MethodTable *pMT = typeHandle.GetMethodTable();
658 return (INT32)pMT->GetNumVirtuals();
660 return 0; //REVIEW: should this return the number of methods in Object?
664 FCIMPL2(MethodDesc *, RuntimeTypeHandle::GetMethodAt, ReflectClassBaseObject *pTypeUNSAFE, INT32 slot) {
670 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
673 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
675 TypeHandle typeHandle = refType->GetType();
677 MethodDesc* pRetMethod = NULL;
679 if (typeHandle.IsGenericVariable())
680 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
682 if (slot < 0 || slot >= (INT32)typeHandle.GetMethodTable()->GetNumVirtuals())
683 FCThrowRes(kArgumentException, W("Arg_ArgumentOutOfRangeException"));
685 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
686 pRetMethod = typeHandle.GetMethodTable()->GetMethodDescForSlot((DWORD)slot);
687 HELPER_METHOD_FRAME_END();
694 FCIMPL3(FC_BOOL_RET, RuntimeTypeHandle::GetFields, ReflectClassBaseObject *pTypeUNSAFE, INT32 **result, INT32 *pCount) {
700 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
702 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
704 TypeHandle typeHandle = refType->GetType();
706 if (!pCount || !result)
707 FCThrow(kArgumentNullException);
709 if (typeHandle.IsGenericVariable())
710 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
712 if (typeHandle.IsTypeDesc()) {
714 FC_RETURN_BOOL(TRUE);
717 MethodTable *pMT= typeHandle.GetMethodTable();
719 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
722 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
723 // <TODO>Check this approximation - we may be losing exact type information </TODO>
724 ApproxFieldDescIterator fdIterator(pMT, ApproxFieldDescIterator::ALL_FIELDS);
725 INT32 count = (INT32)fdIterator.Count();
733 for(INT32 i = 0; i < count; i ++)
734 result[i] = (INT32*)fdIterator.Next();
739 HELPER_METHOD_FRAME_END();
740 FC_RETURN_BOOL(retVal);
744 void QCALLTYPE RuntimeMethodHandle::ConstructInstantiation(MethodDesc * pMethod, DWORD format, QCall::StringHandleOnStack retString)
751 TypeString::AppendInst(ss, pMethod->LoadMethodInstantiation(), format);
757 void QCALLTYPE RuntimeTypeHandle::ConstructName(EnregisteredTypeHandle pTypeHandle, DWORD format, QCall::StringHandleOnStack retString)
764 TypeString::AppendType(ss, TypeHandle::FromPtr(pTypeHandle), format);
770 PTRARRAYREF CopyRuntimeTypeHandles(TypeHandle * prgTH, FixupPointer<TypeHandle> * prgTH2, INT32 numTypeHandles, BinderClassID arrayElemType)
779 PTRARRAYREF refReturn = NULL;
780 PTRARRAYREF refArray = NULL;
782 if (numTypeHandles == 0)
785 _ASSERTE((prgTH != NULL) || (prgTH2 != NULL));
788 _ASSERTE(prgTH2 == NULL);
791 GCPROTECT_BEGIN(refArray);
792 TypeHandle thRuntimeType = TypeHandle(MscorlibBinder::GetClass(arrayElemType));
793 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(thRuntimeType, ELEMENT_TYPE_SZARRAY);
794 refArray = (PTRARRAYREF)AllocateArrayEx(arrayHandle, &numTypeHandles, 1);
796 for (INT32 i = 0; i < numTypeHandles; i++)
803 th = prgTH2[i].GetValue();
805 OBJECTREF refType = th.GetManagedClassObject();
806 refArray->SetAt(i, refType);
809 refReturn = refArray;
815 void QCALLTYPE RuntimeTypeHandle::GetConstraints(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retTypeArray)
819 TypeHandle* constraints = NULL;
823 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
825 if (!typeHandle.IsGenericVariable())
826 COMPlusThrow(kArgumentException, W("Arg_InvalidHandle"));
828 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
831 constraints = pGenericVariable->GetConstraints(&dwCount);
834 retTypeArray.Set(CopyRuntimeTypeHandles(constraints, NULL, dwCount, CLASS__TYPE));
841 FCIMPL1(PtrArray*, RuntimeTypeHandle::GetInterfaces, ReflectClassBaseObject *pTypeUNSAFE) {
847 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
850 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
852 TypeHandle typeHandle = refType->GetType();
854 if (typeHandle.IsGenericVariable())
855 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
857 INT32 ifaceCount = 0;
859 PTRARRAYREF refRetVal = NULL;
860 HELPER_METHOD_FRAME_BEGIN_RET_2(refRetVal, refType);
862 if (typeHandle.IsTypeDesc())
864 if (typeHandle.IsArray())
866 ifaceCount = typeHandle.GetMethodTable()->GetNumInterfaces();
875 ifaceCount = typeHandle.GetMethodTable()->GetNumInterfaces();
878 // Allocate the array
881 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(g_pRuntimeTypeClass), ELEMENT_TYPE_SZARRAY);
882 refRetVal = (PTRARRAYREF)AllocateArrayEx(arrayHandle, &ifaceCount, 1);
884 // populate type array
887 MethodTable::InterfaceMapIterator it = typeHandle.GetMethodTable()->IterateInterfaceMap();
890 OBJECTREF refInterface = it.GetInterface()->GetManagedClassObject();
891 refRetVal->SetAt(i, refInterface);
892 _ASSERTE(refRetVal->GetAt(i) != NULL);
897 HELPER_METHOD_FRAME_END();
899 return (PtrArray*)OBJECTREFToObject(refRetVal);
903 FCIMPL1(INT32, RuntimeTypeHandle::GetAttributes, ReflectClassBaseObject *pTypeUNSAFE) {
909 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
912 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
914 TypeHandle typeHandle = refType->GetType();
916 if (typeHandle.IsTypeDesc()) {
918 if (typeHandle.IsGenericVariable()) {
922 if (!typeHandle.IsArray())
926 #ifdef FEATURE_COMINTEROP
927 // __ComObject types are always public.
928 if (IsComObjectClass(typeHandle))
929 return (typeHandle.GetMethodTable()->GetAttrClass() & tdVisibilityMask) | tdPublic;
930 #endif // FEATURE_COMINTEROP
934 ret = (INT32)typeHandle.GetMethodTable()->GetAttrClass();
940 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsValueType, ReflectClassBaseObject *pTypeUNSAFE)
947 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
949 _ASSERTE(refType != NULL);
951 TypeHandle typeHandle = refType->GetType();
953 FC_RETURN_BOOL(typeHandle.IsValueType());
957 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsInterface, ReflectClassBaseObject *pTypeUNSAFE)
964 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
966 _ASSERTE(refType != NULL);
968 TypeHandle typeHandle = refType->GetType();
970 FC_RETURN_BOOL(typeHandle.IsInterface());
976 RuntimeTypeHandle::IsVisible(
977 EnregisteredTypeHandle pTypeHandle)
985 BOOL fIsExternallyVisible = FALSE;
989 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
991 _ASSERTE(!typeHandle.IsNull());
993 fIsExternallyVisible = typeHandle.IsExternallyVisible();
997 return fIsExternallyVisible;
998 } // RuntimeTypeHandle::IsVisible
1000 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::HasProxyAttribute, ReflectClassBaseObject *pTypeUNSAFE) {
1006 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1008 if (refType == NULL)
1009 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1011 TypeHandle typeHandle = refType->GetType();
1013 // TODO: Justify this
1014 if (typeHandle.IsGenericVariable())
1015 FC_RETURN_BOOL(FALSE);
1017 if (typeHandle.IsTypeDesc()) {
1018 if (!typeHandle.IsArray())
1019 FC_RETURN_BOOL(FALSE);
1022 MethodTable* pMT= typeHandle.GetMethodTable();
1025 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1027 FC_RETURN_BOOL(pMT->GetClass()->HasRemotingProxyAttribute());
1031 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::IsComObject, ReflectClassBaseObject *pTypeUNSAFE, CLR_BOOL isGenericCOM) {
1032 #ifdef FEATURE_COMINTEROP
1040 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1042 if (refType == NULL)
1043 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1045 TypeHandle typeHandle = refType->GetType();
1047 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1050 ret = IsComObjectClass(typeHandle);
1052 ret = IsComWrapperClass(typeHandle);
1054 HELPER_METHOD_FRAME_END();
1056 FC_RETURN_BOOL(ret);
1062 PRECONDITION(CheckPointer(pTypeUNSAFE));
1066 FC_RETURN_BOOL(FALSE);
1071 FCIMPL1(LPCUTF8, RuntimeTypeHandle::GetUtf8Name, ReflectClassBaseObject* pTypeUNSAFE) {
1077 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1079 if (refType == NULL)
1080 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1082 TypeHandle typeHandle = refType->GetType();
1083 INT32 tkTypeDef = mdTypeDefNil;
1084 LPCUTF8 szName = NULL;
1086 if (typeHandle.IsGenericVariable())
1087 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1089 if (typeHandle.IsTypeDesc())
1090 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1092 MethodTable* pMT= typeHandle.GetMethodTable();
1095 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1097 tkTypeDef = (INT32)pMT->GetCl();
1099 if (IsNilToken(tkTypeDef))
1100 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1102 if (FAILED(pMT->GetMDImport()->GetNameOfTypeDef(tkTypeDef, &szName, NULL)))
1104 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
1107 _ASSERTE(CheckPointer(szName, NULL_OK));
1113 FCIMPL1(INT32, RuntimeTypeHandle::GetToken, ReflectClassBaseObject *pTypeUNSAFE) {
1119 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1121 if (refType == NULL)
1122 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1124 TypeHandle typeHandle = refType->GetType();
1126 if (typeHandle.IsTypeDesc())
1128 if (typeHandle.IsGenericVariable())
1130 INT32 tkTypeDef = typeHandle.AsGenericVariable()->GetToken();
1132 _ASSERTE(!IsNilToken(tkTypeDef) && TypeFromToken(tkTypeDef) == mdtGenericParam);
1137 return mdTypeDefNil;
1140 return (INT32)typeHandle.AsMethodTable()->GetCl();
1144 PVOID QCALLTYPE RuntimeTypeHandle::GetGCHandle(EnregisteredTypeHandle pTypeHandle, INT32 handleType)
1148 OBJECTHANDLE objHandle = NULL;
1154 TypeHandle th = TypeHandle::FromPtr(pTypeHandle);
1155 objHandle = th.GetDomain()->CreateTypedHandle(NULL, handleType);
1156 th.GetLoaderAllocator()->RegisterHandleForCleanup(objHandle);
1163 void QCALLTYPE RuntimeTypeHandle::VerifyInterfaceIsImplemented(EnregisteredTypeHandle pTypeHandle, EnregisteredTypeHandle pIFaceHandle)
1169 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1170 TypeHandle ifaceHandle = TypeHandle::FromPtr(pIFaceHandle);
1172 if (typeHandle.IsGenericVariable())
1173 COMPlusThrow(kArgumentException, W("Arg_InvalidHandle"));
1175 if (typeHandle.IsTypeDesc()) {
1176 if (!typeHandle.IsArray())
1177 COMPlusThrow(kArgumentException, W("Arg_NotFoundIFace"));
1180 if (typeHandle.IsInterface())
1181 COMPlusThrow(kArgumentException, W("Argument_InterfaceMap"));
1183 if (!ifaceHandle.IsInterface())
1184 COMPlusThrow(kArgumentException, W("Arg_MustBeInterface"));
1186 // First try the cheap check, which amounts to iterating the interface map looking for
1187 // the ifaceHandle MethodTable.
1188 if (!typeHandle.GetMethodTable()->ImplementsInterface(ifaceHandle.AsMethodTable()))
1189 { // If the cheap check fails, try the more expensive but complete check.
1190 if (!typeHandle.CanCastTo(ifaceHandle))
1191 { // If the complete check fails, we're certain that this type
1192 // does not implement the interface specified.
1193 COMPlusThrow(kArgumentException, W("Arg_NotFoundIFace"));
1200 INT32 QCALLTYPE RuntimeTypeHandle::GetInterfaceMethodImplementationSlot(EnregisteredTypeHandle pTypeHandle, EnregisteredTypeHandle pOwner, MethodDesc * pMD)
1204 INT32 slotNumber = -1;
1208 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1209 TypeHandle thOwnerOfMD = TypeHandle::FromPtr(pOwner);
1211 // Ok to have INVALID_SLOT in the case where abstract class does not implement an interface method.
1212 // This case can not be reproed using C# "implements" all interface methods
1213 // with at least an abstract method. b19897_GetInterfaceMap_Abstract.exe tests this case.
1214 //@TODO:STUBDISPATCH: Don't need to track down the implementation, just the declaration, and this can
1215 //@TODO: be done faster - just need to make a function FindDispatchDecl.
1216 DispatchSlot slot(typeHandle.GetMethodTable()->FindDispatchSlotForInterfaceMD(thOwnerOfMD, pMD));
1218 slotNumber = slot.GetMethodDesc()->GetSlot();
1225 void QCALLTYPE RuntimeTypeHandle::GetDefaultConstructor(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retMethod)
1231 MethodDesc* pCtor = NULL;
1233 TypeHandle typeHandle = TypeHandle::FromPtr(pTypeHandle);
1235 if (!typeHandle.IsTypeDesc())
1237 MethodTable* pMethodTable = typeHandle.AsMethodTable();
1238 if (pMethodTable->HasDefaultConstructor())
1239 pCtor = pMethodTable->GetDefaultConstructor();
1245 retMethod.Set(pCtor->GetStubMethodInfo());
1252 FCIMPL1(ReflectMethodObject*, RuntimeTypeHandle::GetDeclaringMethod, ReflectClassBaseObject *pTypeUNSAFE) {
1258 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1260 if (refType == NULL)
1261 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1263 TypeHandle typeHandle = refType->GetType();;
1265 if (!typeHandle.IsTypeDesc())
1268 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
1269 mdToken defToken = pGenericVariable->GetTypeOrMethodDef();
1270 if (TypeFromToken(defToken) != mdtMethodDef)
1273 REFLECTMETHODREF pRet = NULL;
1274 HELPER_METHOD_FRAME_BEGIN_RET_0();
1275 MethodDesc * pMD = pGenericVariable->LoadOwnerMethod();
1276 pMD->CheckRestore();
1277 pRet = pMD->GetStubMethodInfo();
1278 HELPER_METHOD_FRAME_END();
1280 return (ReflectMethodObject*)OBJECTREFToObject(pRet);
1284 FCIMPL1(ReflectClassBaseObject*, RuntimeTypeHandle::GetDeclaringType, ReflectClassBaseObject *pTypeUNSAFE) {
1290 TypeHandle retTypeHandle;
1292 BOOL fThrowException = FALSE;
1293 LPCWSTR argName = W("Arg_InvalidHandle");
1294 RuntimeExceptionKind reKind = kArgumentNullException;
1296 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1298 if (refType == NULL)
1299 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1301 TypeHandle typeHandle = refType->GetType();
1303 MethodTable* pMT = NULL;
1304 mdTypeDef tkTypeDef = mdTokenNil;
1306 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1307 if (typeHandle.IsTypeDesc()) {
1309 if (typeHandle.IsGenericVariable()) {
1310 TypeVarTypeDesc* pGenericVariable = typeHandle.AsGenericVariable();
1311 mdToken defToken = pGenericVariable->GetTypeOrMethodDef();
1313 // Try the fast way first (if the declaring type has been loaded already).
1314 if (TypeFromToken(defToken) == mdtMethodDef)
1316 MethodDesc * retMethod = pGenericVariable->GetModule()->LookupMethodDef(defToken);
1317 if (retMethod != NULL)
1318 retTypeHandle = retMethod->GetMethodTable();
1322 retTypeHandle = pGenericVariable->GetModule()->LookupTypeDef(defToken);
1325 if (!retTypeHandle.IsNull() && retTypeHandle.IsFullyLoaded())
1328 // OK, need to go the slow way and load the type first.
1329 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1331 if (TypeFromToken(defToken) == mdtMethodDef)
1333 retTypeHandle = pGenericVariable->LoadOwnerMethod()->GetMethodTable();
1337 retTypeHandle = pGenericVariable->LoadOwnerType();
1339 retTypeHandle.CheckRestore();
1341 HELPER_METHOD_FRAME_END();
1344 if (!typeHandle.IsArray())
1346 retTypeHandle = TypeHandle();
1351 pMT = typeHandle.GetMethodTable();
1355 fThrowException = TRUE;
1359 if(!pMT->GetClass()->IsNested())
1361 retTypeHandle = TypeHandle();
1365 tkTypeDef = pMT->GetCl();
1367 if (FAILED(typeHandle.GetModule()->GetMDImport()->GetNestedClassProps(tkTypeDef, &tkTypeDef)))
1369 fThrowException = TRUE;
1370 reKind = kBadImageFormatException;
1375 // Try the fast way first (if the declaring type has been loaded already).
1376 retTypeHandle = typeHandle.GetModule()->LookupTypeDef(tkTypeDef);
1377 if (retTypeHandle.IsNull())
1379 // OK, need to go the slow way and load the type first.
1380 HELPER_METHOD_FRAME_BEGIN_RET_1(refType);
1382 retTypeHandle = ClassLoader::LoadTypeDefThrowing(typeHandle.GetModule(), tkTypeDef,
1383 ClassLoader::ThrowIfNotFound,
1384 ClassLoader::PermitUninstDefOrRef);
1386 HELPER_METHOD_FRAME_END();
1390 END_SO_INTOLERANT_CODE;
1392 if (fThrowException)
1394 FCThrowRes(reKind, argName);
1397 RETURN_CLASS_OBJECT(retTypeHandle, refType);
1401 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CanCastTo, ReflectClassBaseObject *pTypeUNSAFE, ReflectClassBaseObject *pTargetUNSAFE) {
1408 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1409 REFLECTCLASSBASEREF refTarget = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTargetUNSAFE);
1411 if ((refType == NULL) || (refTarget == NULL))
1412 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1414 TypeHandle fromHandle = refType->GetType();
1415 TypeHandle toHandle = refTarget->GetType();
1419 TypeHandle::CastResult r = fromHandle.CanCastToNoGC(toHandle);
1420 if (r == TypeHandle::MaybeCast)
1422 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refTarget);
1423 iRetVal = fromHandle.CanCastTo(toHandle);
1424 HELPER_METHOD_FRAME_END();
1428 iRetVal = (r == TypeHandle::CanCast);
1431 // We allow T to be cast to Nullable<T>
1432 if (!iRetVal && Nullable::IsNullableType(toHandle) && !fromHandle.IsTypeDesc())
1434 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, refTarget);
1435 if (Nullable::IsNullableForType(toHandle, fromHandle.AsMethodTable()))
1439 HELPER_METHOD_FRAME_END();
1442 FC_RETURN_BOOL(iRetVal);
1446 void QCALLTYPE RuntimeTypeHandle::GetTypeByNameUsingCARules(LPCWSTR pwzClassName, QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
1450 TypeHandle typeHandle;
1455 COMPlusThrowArgumentNull(W("className"),W("ArgumentNull_String"));
1457 typeHandle = TypeName::GetTypeUsingCASearchRules(pwzClassName, pModule->GetAssembly());
1460 retType.Set(typeHandle.GetManagedClassObject());
1467 void QCALLTYPE RuntimeTypeHandle::GetTypeByName(LPCWSTR pwzClassName, BOOL bThrowOnError, BOOL bIgnoreCase, BOOL bReflectionOnly,
1468 QCall::StackCrawlMarkHandle pStackMark,
1469 ICLRPrivBinder * pPrivHostBinder,
1470 BOOL bLoadTypeFromPartialNameHack, QCall::ObjectHandleOnStack retType,
1471 QCall::ObjectHandleOnStack keepAlive)
1475 TypeHandle typeHandle;
1480 COMPlusThrowArgumentNull(W("className"),W("ArgumentNull_String"));
1483 typeHandle = TypeName::GetTypeManaged(pwzClassName, NULL, bThrowOnError, bIgnoreCase, bReflectionOnly, /*bProhibitAsmQualifiedName =*/ FALSE, pStackMark,
1484 bLoadTypeFromPartialNameHack, (OBJECTREF*)keepAlive.m_ppObject,
1488 if (!typeHandle.IsNull())
1491 retType.Set(typeHandle.GetManagedClassObject());
1499 FCIMPL6(FC_BOOL_RET, RuntimeTypeHandle::SatisfiesConstraints, PTR_ReflectClassBaseObject pParamTypeUNSAFE, TypeHandle *typeContextArgs, INT32 typeContextCount, TypeHandle *methodContextArgs, INT32 methodContextCount, PTR_ReflectClassBaseObject pArgumentTypeUNSAFE);
1503 PRECONDITION(CheckPointer(typeContextArgs, NULL_OK));
1504 PRECONDITION(CheckPointer(methodContextArgs, NULL_OK));
1508 REFLECTCLASSBASEREF refParamType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pParamTypeUNSAFE);
1509 REFLECTCLASSBASEREF refArgumentType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pArgumentTypeUNSAFE);
1511 TypeHandle thGenericParameter = refParamType->GetType();
1512 TypeHandle thGenericArgument = refArgumentType->GetType();
1513 BOOL bResult = FALSE;
1514 SigTypeContext typeContext;
1516 Instantiation classInst;
1517 Instantiation methodInst;
1519 if (typeContextArgs != NULL)
1521 classInst = Instantiation(typeContextArgs, typeContextCount);
1524 if (methodContextArgs != NULL)
1526 methodInst = Instantiation(methodContextArgs, methodContextCount);
1529 SigTypeContext::InitTypeContext(classInst, methodInst, &typeContext);
1531 HELPER_METHOD_FRAME_BEGIN_RET_2(refParamType, refArgumentType);
1533 bResult = thGenericParameter.AsGenericVariable()->SatisfiesConstraints(&typeContext, thGenericArgument);
1535 HELPER_METHOD_FRAME_END();
1537 FC_RETURN_BOOL(bResult);
1541 void QCALLTYPE RuntimeTypeHandle::GetInstantiation(EnregisteredTypeHandle pType, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
1547 TypeHandle typeHandle = TypeHandle::FromPtr(pType);
1548 Instantiation inst = typeHandle.GetInstantiation();
1550 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
1556 void QCALLTYPE RuntimeTypeHandle::MakeArray(EnregisteredTypeHandle pTypeHandle, INT32 rank, QCall::ObjectHandleOnStack retType)
1560 TypeHandle arrayHandle;
1563 arrayHandle = TypeHandle::FromPtr(pTypeHandle).MakeArray(rank);
1565 retType.Set(arrayHandle.GetManagedClassObject());
1571 void QCALLTYPE RuntimeTypeHandle::MakeSZArray(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1575 TypeHandle arrayHandle;
1578 arrayHandle = TypeHandle::FromPtr(pTypeHandle).MakeSZArray();
1580 retType.Set(arrayHandle.GetManagedClassObject());
1586 void QCALLTYPE RuntimeTypeHandle::MakePointer(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1590 TypeHandle pointerHandle;
1593 pointerHandle = TypeHandle::FromPtr(pTypeHandle).MakePointer();
1595 retType.Set(pointerHandle.GetManagedClassObject());
1601 void QCALLTYPE RuntimeTypeHandle::MakeByRef(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1605 TypeHandle byRefHandle;
1608 byRefHandle = TypeHandle::FromPtr(pTypeHandle).MakeByRef();
1610 retType.Set(byRefHandle.GetManagedClassObject());
1616 BOOL QCALLTYPE RuntimeTypeHandle::IsCollectible(EnregisteredTypeHandle pTypeHandle)
1620 BOOL retVal = FALSE;
1623 retVal = TypeHandle::FromPtr(pTypeHandle).GetLoaderAllocator()->IsCollectible();
1629 void QCALLTYPE RuntimeTypeHandle::Instantiate(EnregisteredTypeHandle pTypeHandle, TypeHandle * pInstArray, INT32 cInstArray, QCall::ObjectHandleOnStack retType)
1636 type = TypeHandle::FromPtr(pTypeHandle).Instantiate(Instantiation(pInstArray, cInstArray));
1638 retType.Set(type.GetManagedClassObject());
1644 void QCALLTYPE RuntimeTypeHandle::GetGenericTypeDefinition(EnregisteredTypeHandle pTypeHandle, QCall::ObjectHandleOnStack retType)
1652 TypeHandle genericType = TypeHandle::FromPtr(pTypeHandle);
1654 typeDef = ClassLoader::LoadTypeDefThrowing(genericType.GetModule(),
1655 genericType.GetMethodTable()->GetCl(),
1656 ClassLoader::ThrowIfNotFound,
1657 ClassLoader::PermitUninstDefOrRef);
1660 retType.Set(typeDef.GetManagedClassObject());
1667 FCIMPL2(FC_BOOL_RET, RuntimeTypeHandle::CompareCanonicalHandles, ReflectClassBaseObject *pLeftUNSAFE, ReflectClassBaseObject *pRightUNSAFE)
1671 REFLECTCLASSBASEREF refLeft = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pLeftUNSAFE);
1672 REFLECTCLASSBASEREF refRight = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pRightUNSAFE);
1674 if ((refLeft == NULL) || (refRight == NULL))
1675 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1677 FC_RETURN_BOOL(refLeft->GetType().GetCanonicalMethodTable() == refRight->GetType().GetCanonicalMethodTable());
1681 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::HasInstantiation, PTR_ReflectClassBaseObject pTypeUNSAFE)
1685 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1687 if (refType == NULL)
1688 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1690 FC_RETURN_BOOL(refType->GetType().HasInstantiation());
1694 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericTypeDefinition, PTR_ReflectClassBaseObject pTypeUNSAFE)
1698 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1700 if (refType == NULL)
1701 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1703 FC_RETURN_BOOL(refType->GetType().IsGenericTypeDefinition());
1707 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::IsGenericVariable, PTR_ReflectClassBaseObject pTypeUNSAFE)
1711 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1713 if (refType == NULL)
1714 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1716 FC_RETURN_BOOL(refType->GetType().IsGenericVariable());
1720 FCIMPL1(INT32, RuntimeTypeHandle::GetGenericVariableIndex, PTR_ReflectClassBaseObject pTypeUNSAFE)
1724 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1726 if (refType == NULL)
1727 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1729 return (INT32)refType->GetType().AsGenericVariable()->GetIndex();
1733 FCIMPL1(FC_BOOL_RET, RuntimeTypeHandle::ContainsGenericVariables, PTR_ReflectClassBaseObject pTypeUNSAFE)
1737 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1739 if (refType == NULL)
1740 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1742 FC_RETURN_BOOL(refType->GetType().ContainsGenericVariables());
1746 FCIMPL1(IMDInternalImport*, RuntimeTypeHandle::GetMetadataImport, ReflectClassBaseObject * pTypeUNSAFE)
1750 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
1752 if (refType == NULL)
1753 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1755 Module *pModule = refType->GetType().GetModule();
1757 return pModule->GetMDImport();
1762 //***********************************************************************************
1763 //***********************************************************************************
1764 //***********************************************************************************
1766 void * QCALLTYPE RuntimeMethodHandle::GetFunctionPointer(MethodDesc * pMethod)
1774 funcPtr = (void*)pMethod->GetMultiCallableAddrOfCode();
1781 FCIMPL1(LPCUTF8, RuntimeMethodHandle::GetUtf8Name, MethodDesc *pMethod) {
1787 LPCUTF8 szName = NULL;
1790 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1792 szName = pMethod->GetName();
1794 _ASSERTE(CheckPointer(szName, NULL_OK));
1800 FCIMPL2(FC_BOOL_RET, RuntimeMethodHandle::MatchesNameHash, MethodDesc * pMethod, ULONG hash)
1804 FC_RETURN_BOOL(pMethod->MightHaveName(hash));
1808 FCIMPL1(StringObject*, RuntimeMethodHandle::GetName, MethodDesc *pMethod) {
1815 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1817 STRINGREF refName = NULL;
1819 HELPER_METHOD_FRAME_BEGIN_RET_0();
1820 refName = StringObject::NewString(pMethod->GetName());
1821 HELPER_METHOD_FRAME_END();
1823 return (StringObject*)OBJECTREFToObject(refName);
1827 FCIMPL1(INT32, RuntimeMethodHandle::GetAttributes, MethodDesc *pMethod) {
1834 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1837 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1838 retVal = (INT32)pMethod->GetAttrs();
1839 END_SO_INTOLERANT_CODE;
1844 FCIMPL1(INT32, RuntimeMethodHandle::GetImplAttributes, ReflectMethodObject *pMethodUNSAFE) {
1851 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1853 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
1854 INT32 attributes = 0;
1856 if (IsNilToken(pMethod->GetMemberDef()))
1859 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
1861 attributes = (INT32)pMethod->GetImplAttrs();
1863 END_SO_INTOLERANT_CODE;
1870 FCIMPL1(ReflectClassBaseObject*, RuntimeMethodHandle::GetDeclaringType, MethodDesc *pMethod) {
1873 PRECONDITION(CheckPointer(pMethod));
1878 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1880 MethodTable *pMT = pMethod->GetMethodTable();
1881 TypeHandle declType(pMT);
1884 HELPER_METHOD_FRAME_BEGIN_RET_0();
1886 // Load the TypeDesc for the array type. Note the returned type is approximate, i.e.
1887 // if shared between reference array types then we will get object[] back.
1888 DWORD rank = pMT->GetRank();
1889 TypeHandle elemType = pMT->GetApproxArrayElementTypeHandle();
1890 declType = ClassLoader::LoadArrayTypeThrowing(elemType, pMT->GetInternalCorElementType(), rank);
1891 HELPER_METHOD_FRAME_END();
1893 RETURN_CLASS_OBJECT(declType, NULL);
1897 FCIMPL1(INT32, RuntimeMethodHandle::GetSlot, MethodDesc *pMethod) {
1904 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
1906 return (INT32)pMethod->GetSlot();
1910 FCIMPL3(Object *, SignatureNative::GetCustomModifiers, SignatureNative* pSignatureUNSAFE,
1911 INT32 parameter, CLR_BOOL fRequired)
1920 SIGNATURENATIVEREF pSig;
1924 gc.pSig = (SIGNATURENATIVEREF)pSignatureUNSAFE;
1927 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
1930 BYTE callConv = *(BYTE*)gc.pSig->GetCorSig();
1931 SigTypeContext typeContext;
1932 gc.pSig->GetTypeContext(&typeContext);
1933 MetaSig sig(gc.pSig->GetCorSig(),
1934 gc.pSig->GetCorSigSize(),
1935 gc.pSig->GetModule(),
1937 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
1938 _ASSERTE(callConv == sig.GetCallingConventionInfo());
1940 SigPointer argument(NULL, 0);
1942 PRECONDITION(sig.GetCallingConvention() != IMAGE_CEE_CS_CALLCONV_FIELD || parameter == 1);
1946 argument = sig.GetReturnProps();
1950 for(INT32 i = 0; i < parameter; i++)
1953 argument = sig.GetArgProps();
1956 //if (parameter < 0 || parameter > (INT32)sig.NumFixedArgs())
1957 // FCThrowResVoid(kArgumentNullException, W("Arg_ArgumentOutOfRangeException"));
1959 SigPointer sp = argument;
1960 Module* pModule = sig.GetModule();
1962 CorElementType cmodType;
1964 CorElementType cmodTypeExpected = fRequired ? ELEMENT_TYPE_CMOD_REQD : ELEMENT_TYPE_CMOD_OPT;
1966 // Discover the number of required and optional custom modifiers.
1970 IfFailThrow(sp.GetByte(&data));
1971 cmodType = (CorElementType)data;
1973 if (cmodType == ELEMENT_TYPE_CMOD_REQD || cmodType == ELEMENT_TYPE_CMOD_OPT)
1975 if (cmodType == cmodTypeExpected)
1980 else if (cmodType != ELEMENT_TYPE_SENTINEL)
1985 IfFailThrow(sp.GetToken(NULL));
1988 // Reset sp and populate the arrays for the required and optional custom
1989 // modifiers now that we know how long they should be.
1992 MethodTable *pMT = MscorlibBinder::GetClass(CLASS__TYPE);
1993 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pMT), ELEMENT_TYPE_SZARRAY);
1995 gc.retVal = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &cMods, 1);
2000 IfFailThrow(sp.GetByte(&data));
2001 cmodType = (CorElementType)data;
2004 IfFailThrow(sp.GetToken(&token));
2006 if (cmodType == cmodTypeExpected)
2008 TypeHandle th = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, token,
2010 ClassLoader::ThrowIfNotFound,
2011 ClassLoader::FailIfUninstDefOrRef);
2013 OBJECTREF refType = th.GetManagedClassObject();
2014 gc.retVal->SetAt(--cMods, refType);
2018 HELPER_METHOD_FRAME_END();
2020 return OBJECTREFToObject(gc.retVal);
2024 FCIMPL1(INT32, RuntimeMethodHandle::GetMethodDef, ReflectMethodObject *pMethodUNSAFE) {
2031 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2033 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2035 if (pMethod->HasMethodInstantiation())
2037 HELPER_METHOD_FRAME_BEGIN_RET_1(pMethodUNSAFE);
2039 pMethod = pMethod->StripMethodInstantiation();
2041 HELPER_METHOD_FRAME_END();
2044 INT32 tkMethodDef = (INT32)pMethod->GetMemberDef();
2045 _ASSERTE(TypeFromToken(tkMethodDef) == mdtMethodDef);
2047 if (IsNilToken(tkMethodDef) || TypeFromToken(tkMethodDef) != mdtMethodDef)
2048 return mdMethodDefNil;
2054 FCIMPL6(void, SignatureNative::GetSignature,
2055 SignatureNative* pSignatureNativeUNSAFE,
2056 PCCOR_SIGNATURE pCorSig, DWORD cCorSig,
2057 FieldDesc *pFieldDesc, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE) {
2060 PRECONDITION(pDeclaringTypeUNSAFE || pMethodUNSAFE->GetMethod()->IsDynamicMethod());
2061 PRECONDITION(CheckPointer(pCorSig, NULL_OK));
2062 PRECONDITION(CheckPointer(pMethodUNSAFE, NULL_OK));
2063 PRECONDITION(CheckPointer(pFieldDesc, NULL_OK));
2069 REFLECTCLASSBASEREF refDeclaringType;
2070 REFLECTMETHODREF refMethod;
2071 SIGNATURENATIVEREF pSig;
2074 gc.refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2075 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2076 gc.pSig = (SIGNATURENATIVEREF)pSignatureNativeUNSAFE;
2078 MethodDesc *pMethod;
2079 TypeHandle declType;
2081 if (gc.refDeclaringType == NULL)
2083 // for dynamic method, see precondition
2084 pMethod = gc.refMethod->GetMethod();
2085 declType = pMethod->GetMethodTable();
2089 pMethod = gc.refMethod != NULL ? gc.refMethod->GetMethod() : NULL;
2090 declType = gc.refDeclaringType->GetType();
2093 HELPER_METHOD_FRAME_BEGIN_PROTECT(gc);
2095 Module* pModule = declType.GetModule();
2099 pMethod->GetSig(&pCorSig, &cCorSig);
2100 if (pMethod->GetClassification() == mcInstantiated)
2102 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2103 if (pLoaderAllocator->IsCollectible())
2104 gc.pSig->SetKeepAlive(pLoaderAllocator->GetExposedObject());
2107 else if (pFieldDesc)
2108 pFieldDesc->GetSig(&pCorSig, &cCorSig);
2110 gc.pSig->m_sig = pCorSig;
2111 gc.pSig->m_cSig = cCorSig;
2112 gc.pSig->m_pMethod = pMethod;
2114 REFLECTCLASSBASEREF refDeclType = (REFLECTCLASSBASEREF)declType.GetManagedClassObject();
2115 gc.pSig->SetDeclaringType(refDeclType);
2117 PREFIX_ASSUME(pCorSig!= NULL);
2118 BYTE callConv = *(BYTE*)pCorSig;
2119 SigTypeContext typeContext;
2121 SigTypeContext::InitTypeContext(
2122 pMethod, declType.GetClassOrArrayInstantiation(), pMethod->LoadMethodInstantiation(), &typeContext);
2124 SigTypeContext::InitTypeContext(declType, &typeContext);
2125 MetaSig msig(pCorSig, cCorSig, pModule, &typeContext,
2126 (callConv & IMAGE_CEE_CS_CALLCONV_MASK) == IMAGE_CEE_CS_CALLCONV_FIELD ? MetaSig::sigField : MetaSig::sigMember);
2128 if (callConv == IMAGE_CEE_CS_CALLCONV_FIELD)
2130 msig.NextArgNormalized();
2132 OBJECTREF refRetType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2133 gc.pSig->SetReturnType(refRetType);
2137 gc.pSig->SetCallingConvention(msig.GetCallingConventionInfo());
2139 OBJECTREF refRetType = msig.GetRetTypeHandleThrowing().GetManagedClassObject();
2140 gc.pSig->SetReturnType(refRetType);
2142 INT32 nArgs = msig.NumFixedArgs();
2143 TypeHandle arrayHandle = ClassLoader::LoadArrayTypeThrowing(TypeHandle(g_pRuntimeTypeClass), ELEMENT_TYPE_SZARRAY);
2145 PTRARRAYREF ptrArrayarguments = (PTRARRAYREF) AllocateArrayEx(arrayHandle, &nArgs, 1);
2146 gc.pSig->SetArgumentArray(ptrArrayarguments);
2148 for (INT32 i = 0; i < nArgs; i++)
2152 OBJECTREF refArgType = msig.GetLastTypeHandleThrowing().GetManagedClassObject();
2153 gc.pSig->SetArgument(i, refArgType);
2156 _ASSERTE(gc.pSig->m_returnType != NULL);
2159 HELPER_METHOD_FRAME_END();
2163 FCIMPL2(FC_BOOL_RET, SignatureNative::CompareSig, SignatureNative* pLhsUNSAFE, SignatureNative* pRhsUNSAFE)
2171 SIGNATURENATIVEREF pLhs;
2172 SIGNATURENATIVEREF pRhs;
2175 gc.pLhs = (SIGNATURENATIVEREF)pLhsUNSAFE;
2176 gc.pRhs = (SIGNATURENATIVEREF)pRhsUNSAFE;
2178 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2180 ret = MetaSig::CompareMethodSigs(
2181 gc.pLhs->GetCorSig(), gc.pLhs->GetCorSigSize(), gc.pLhs->GetModule(), NULL,
2182 gc.pRhs->GetCorSig(), gc.pRhs->GetCorSigSize(), gc.pRhs->GetModule(), NULL);
2184 HELPER_METHOD_FRAME_END();
2185 FC_RETURN_BOOL(ret);
2189 void QCALLTYPE RuntimeMethodHandle::GetMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack retTypes, BOOL fAsRuntimeTypeArray)
2194 Instantiation inst = pMethod->LoadMethodInstantiation();
2197 retTypes.Set(CopyRuntimeTypeHandles(NULL, inst.GetRawArgs(), inst.GetNumArgs(), fAsRuntimeTypeArray ? CLASS__CLASS : CLASS__TYPE));
2203 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::HasMethodInstantiation, MethodDesc * pMethod)
2207 FC_RETURN_BOOL(pMethod->HasMethodInstantiation());
2211 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsGenericMethodDefinition, MethodDesc * pMethod)
2215 FC_RETURN_BOOL(pMethod->IsGenericMethodDefinition());
2219 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsDynamicMethod, MethodDesc * pMethod)
2223 FC_RETURN_BOOL(pMethod->IsNoMetadata());
2227 FCIMPL1(Object*, RuntimeMethodHandle::GetResolver, MethodDesc * pMethod)
2232 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2234 OBJECTREF resolver = NULL;
2235 if (pMethod->IsLCGMethod())
2237 resolver = pMethod->AsDynamicMethodDesc()->GetLCGMethodResolver()->GetManagedResolver();
2239 return OBJECTREFToObject(resolver);
2243 void QCALLTYPE RuntimeMethodHandle::Destroy(MethodDesc * pMethod)
2249 if (pMethod == NULL)
2250 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2252 DynamicMethodDesc* pDynamicMethodDesc = pMethod->AsDynamicMethodDesc();
2256 // Destroy should be called only if the managed part is gone.
2257 _ASSERTE(OBJECTREFToObject(pDynamicMethodDesc->GetLCGMethodResolver()->GetManagedResolver()) == NULL);
2259 // Fire Unload Dynamic Method Event here
2260 ETW::MethodLog::DynamicMethodDestroyed(pMethod);
2262 BEGIN_PIN_PROFILER(CORProfilerIsMonitoringDynamicFunctionUnloads());
2263 g_profControlBlock.pProfInterface->DynamicMethodUnloaded((FunctionID)pMethod);
2266 pDynamicMethodDesc->Destroy();
2271 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsTypicalMethodDefinition, ReflectMethodObject *pMethodUNSAFE)
2276 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2278 MethodDesc* pMethod = pMethodUNSAFE->GetMethod();
2280 FC_RETURN_BOOL(pMethod->IsTypicalMethodDefinition());
2284 void QCALLTYPE RuntimeMethodHandle::GetTypicalMethodDefinition(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2292 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2295 MethodDesc *pMethodTypical = pMethod->LoadTypicalMethodDefinition();
2296 if (pMethodTypical != pMethod)
2299 refMethod.Set(pMethodTypical->GetStubMethodInfo());
2306 void QCALLTYPE RuntimeMethodHandle::StripMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod)
2313 COMPlusThrowArgumentNull(NULL, W("Arg_InvalidHandle"));
2318 _ASSERTE(((ReflectMethodObject *)(*refMethod.m_ppObject))->GetMethod() == pMethod);
2321 MethodDesc *pMethodStripped = pMethod->StripMethodInstantiation();
2322 if (pMethodStripped != pMethod)
2325 refMethod.Set(pMethodStripped->GetStubMethodInfo());
2332 // In the VM there might be more than one MethodDescs for a "method"
2333 // examples are methods on generic types which may have additional instantiating stubs
2334 // and methods on value types which may have additional unboxing stubs.
2336 // For generic methods we always hand out an instantiating stub except for a generic method definition
2337 // For non-generic methods on generic types we need an instantiating stub if it's one of the following
2338 // - static method on a generic class
2339 // - static or instance method on a generic interface
2340 // - static or instance method on a generic value type
2341 // The Reflection policy is to always hand out instantiating stubs in these cases
2343 // For methods on non-generic value types we can use either the cannonical method or the unboxing stub
2344 // The Reflection policy is to always hand out unboxing stubs if the methods are virtual methods
2345 // The reason for this is that in the current implementation of the class loader, the v-table slots for
2346 // those methods point to unboxing stubs already. Note that this is just a implementation choice
2347 // that might change in the future. But we should always keep this Reflection policy an invariant.
2349 // For virtual methods on generic value types (intersection of the two cases), reflection will hand
2350 // out an unboxing instantiating stub
2352 // GetInstantiatingStub is called to:
2353 // 1. create an InstantiatedMethodDesc for a generic method when calling BindGenericArguments() on a generic
2354 // method. In this case instArray will not be null.
2355 // 2. create an InstantiatedMethodDesc for a method in a generic class. In this case instArray will be null.
2356 // 3. create an UnboxingStub for a method in a value type. In this case instArray will be null.
2357 // For case 2 and 3, an instantiating stub or unboxing stub might not be needed in which case the original
2358 // MethodDesc is returned.
2359 FCIMPL3(MethodDesc*, RuntimeMethodHandle::GetStubIfNeeded,
2360 MethodDesc *pMethod,
2361 ReflectClassBaseObject *pTypeUNSAFE,
2362 PtrArray* instArrayUNSAFE)
2369 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2370 PTRARRAYREF instArray = (PTRARRAYREF)ObjectToOBJECTREF(instArrayUNSAFE);
2372 if (refType == NULL)
2373 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2375 TypeHandle instType = refType->GetType();
2376 MethodDesc *pNewMethod = pMethod;
2380 FCThrowRes(kArgumentException, W("Arg_InvalidHandle"));
2382 if (instType.IsNull())
2383 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2385 // Perf optimization: this logic is actually duplicated in FindOrCreateAssociatedMethodDescForReflection, but since it
2386 // is the more common case it's worth the duplicate check here to avoid the helper method frame
2387 if ( instArray == NULL &&
2388 ( pMethod->HasMethodInstantiation() ||
2389 ( !instType.IsValueType() &&
2390 ( !instType.HasInstantiation() || instType.IsGenericTypeDefinition() ) ) ) )
2395 HELPER_METHOD_FRAME_BEGIN_RET_2(refType, instArray);
2397 TypeHandle *inst = NULL;
2400 if (instArray != NULL)
2402 ntypars = instArray->GetNumComponents();
2404 size_t size = ntypars * sizeof(TypeHandle);
2405 if ((size / sizeof(TypeHandle)) != ntypars) // uint over/underflow
2406 COMPlusThrow(kArgumentException);
2407 inst = (TypeHandle*) _alloca(size);
2409 for (DWORD i = 0; i < ntypars; i++)
2411 REFLECTCLASSBASEREF instRef = (REFLECTCLASSBASEREF)instArray->GetAt(i);
2413 if (instRef == NULL)
2414 COMPlusThrowArgumentNull(W("inst"), W("ArgumentNull_ArrayElement"));
2416 inst[i] = instRef->GetType();
2420 pNewMethod = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMethod, instType, Instantiation(inst, ntypars));
2422 HELPER_METHOD_FRAME_END();
2429 FCIMPL2(MethodDesc*, RuntimeMethodHandle::GetMethodFromCanonical, MethodDesc *pMethod, ReflectClassBaseObject *pTypeUNSAFE)
2433 PRECONDITION(CheckPointer(pMethod));
2437 REFLECTCLASSBASEREF refType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pTypeUNSAFE);
2439 TypeHandle instType = refType->GetType();
2440 MethodDesc* pMDescInCanonMT = instType.GetMethodTable()->GetParallelMethodDesc(pMethod);
2442 return pMDescInCanonMT;
2447 FCIMPL2(MethodBody *, RuntimeMethodHandle::GetMethodBody, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2457 METHODBODYREF MethodBodyObj;
2458 EXCEPTIONHANDLINGCLAUSEREF EHClauseObj;
2459 LOCALVARIABLEINFOREF LocalVariableInfoObj;
2461 BASEARRAYREF TempArray;
2462 REFLECTCLASSBASEREF declaringType;
2463 REFLECTMETHODREF refMethod;
2466 gc.MethodBodyObj = NULL;
2467 gc.EHClauseObj = NULL;
2468 gc.LocalVariableInfoObj = NULL;
2470 gc.TempArray = NULL;
2471 gc.declaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2472 gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2476 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2478 MethodDesc* pMethod = gc.refMethod->GetMethod();
2480 TypeHandle declaringType = gc.declaringType == NULL ? TypeHandle() : gc.declaringType->GetType();
2482 if (!pMethod->IsIL())
2485 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2487 MethodDesc *pMethodIL = pMethod;
2488 if (pMethod->IsWrapperStub())
2489 pMethodIL = pMethod->GetWrappedMethodDesc();
2491 COR_ILMETHOD* pILHeader = pMethodIL->GetILHeader();
2495 MethodTable * pExceptionHandlingClauseMT = MscorlibBinder::GetClass(CLASS__EH_CLAUSE);
2496 TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);
2498 MethodTable * pLocalVariableMT = MscorlibBinder::GetClass(CLASS__LOCAL_VARIABLE_INFO);
2499 TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);
2501 Module* pModule = pMethod->GetModule();
2502 COR_ILMETHOD_DECODER::DecoderStatus status;
2503 COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);
2505 if (status != COR_ILMETHOD_DECODER::SUCCESS)
2507 if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
2509 // Throw a verification HR
2510 COMPlusThrowHR(COR_E_VERIFICATION);
2514 COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
2518 gc.MethodBodyObj = (METHODBODYREF)AllocateObject(MscorlibBinder::GetClass(CLASS__METHOD_BODY));
2520 gc.MethodBodyObj->m_maxStackSize = header.GetMaxStack();
2521 gc.MethodBodyObj->m_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);
2524 gc.MethodBodyObj->m_localVarSigToken = header.GetLocalVarSigTok();
2526 gc.MethodBodyObj->m_localVarSigToken = 0;
2528 // Allocate the array of IL and fill it in from the method header.
2529 BYTE* pIL = const_cast<BYTE*>(header.Code);
2530 COUNT_T cIL = header.GetCodeSize();
2531 gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);
2533 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_IL, gc.U1Array, GetAppDomain());
2534 memcpyNoGCRefs(gc.MethodBodyObj->m_IL->GetDataPtr(), pIL, cIL);
2536 // Allocate the array of exception clauses.
2537 INT32 cEh = (INT32)header.EHCount();
2538 const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
2539 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thEHClauseArray, &cEh, 1);
2541 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_exceptionClauses, gc.TempArray, GetAppDomain());
2543 for (INT32 i = 0; i < cEh; i++)
2545 COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
2546 const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
2547 (const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);
2549 gc.EHClauseObj = (EXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);
2551 gc.EHClauseObj->m_flags = ehClause->GetFlags();
2552 gc.EHClauseObj->m_tryOffset = ehClause->GetTryOffset();
2553 gc.EHClauseObj->m_tryLength = ehClause->GetTryLength();
2554 gc.EHClauseObj->m_handlerOffset = ehClause->GetHandlerOffset();
2555 gc.EHClauseObj->m_handlerLength = ehClause->GetHandlerLength();
2557 if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
2558 gc.EHClauseObj->m_catchToken = ehClause->GetClassToken();
2560 gc.EHClauseObj->m_filterOffset = ehClause->GetFilterOffset();
2562 gc.MethodBodyObj->m_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
2563 SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->m_methodBody), (OBJECTREF)gc.MethodBodyObj, GetAppDomain());
2566 if (header.LocalVarSig != NULL)
2568 SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
2569 MetaSig metaSig(header.LocalVarSig,
2570 header.cbLocalVarSig,
2573 MetaSig::sigLocalVars);
2574 INT32 cLocals = metaSig.NumFixedArgs();
2575 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2576 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_localVariables, gc.TempArray, GetAppDomain());
2578 for (INT32 i = 0; i < cLocals; i ++)
2580 gc.LocalVariableInfoObj = (LOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);
2582 gc.LocalVariableInfoObj->m_localIndex = i;
2586 CorElementType eType;
2587 IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
2588 if (ELEMENT_TYPE_PINNED == eType)
2589 gc.LocalVariableInfoObj->m_bIsPinned = TRUE;
2591 TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
2592 OBJECTREF refLocalType = tempType.GetManagedClassObject();
2593 gc.LocalVariableInfoObj->SetType(refLocalType);
2594 gc.MethodBodyObj->m_localVariables->SetAt(i, (OBJECTREF) gc.LocalVariableInfoObj);
2600 gc.TempArray = (BASEARRAYREF) AllocateArrayEx(thLocalVariableArray, &cLocals, 1);
2601 SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->m_localVariables, gc.TempArray, GetAppDomain());
2605 HELPER_METHOD_FRAME_END();
2607 return (MethodBody*)OBJECTREFToObject(gc.MethodBodyObj);
2611 FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsConstructor, MethodDesc *pMethod)
2615 PRECONDITION(CheckPointer(pMethod));
2620 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2621 ret = (BOOL)pMethod->IsClassConstructorOrCtor();
2622 END_SO_INTOLERANT_CODE;
2623 FC_RETURN_BOOL(ret);
2627 FCIMPL1(Object*, RuntimeMethodHandle::GetLoaderAllocator, MethodDesc *pMethod)
2634 OBJECTREF loaderAllocator = NULL;
2637 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2639 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(loaderAllocator);
2641 LoaderAllocator *pLoaderAllocator = pMethod->GetLoaderAllocator();
2642 loaderAllocator = pLoaderAllocator->GetExposedObject();
2644 HELPER_METHOD_FRAME_END();
2646 return OBJECTREFToObject(loaderAllocator);
2650 //*********************************************************************************************
2651 //*********************************************************************************************
2652 //*********************************************************************************************
2654 FCIMPL1(StringObject*, RuntimeFieldHandle::GetName, ReflectFieldObject *pFieldUNSAFE) {
2660 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2662 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2664 FieldDesc *pField = refField->GetField();
2666 STRINGREF refString = NULL;
2667 HELPER_METHOD_FRAME_BEGIN_RET_1(refField);
2669 refString = StringObject::NewString(pField->GetName());
2671 HELPER_METHOD_FRAME_END();
2672 return (StringObject*)OBJECTREFToObject(refString);
2676 FCIMPL1(LPCUTF8, RuntimeFieldHandle::GetUtf8Name, FieldDesc *pField) {
2679 PRECONDITION(CheckPointer(pField));
2683 LPCUTF8 szFieldName;
2685 if (FAILED(pField->GetName_NoThrow(&szFieldName)))
2687 FCThrow(kBadImageFormatException);
2693 FCIMPL2(FC_BOOL_RET, RuntimeFieldHandle::MatchesNameHash, FieldDesc * pField, ULONG hash)
2697 FC_RETURN_BOOL(pField->MightHaveName(hash));
2701 FCIMPL1(INT32, RuntimeFieldHandle::GetAttributes, FieldDesc *pField) {
2708 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2711 BEGIN_SO_INTOLERANT_CODE_NOTHROW(GetThread(), FCThrow(kStackOverflowException));
2712 ret = (INT32)pField->GetAttributes();
2713 END_SO_INTOLERANT_CODE;
2718 FCIMPL1(ReflectClassBaseObject*, RuntimeFieldHandle::GetApproxDeclaringType, FieldDesc *pField) {
2725 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2727 TypeHandle th = TypeHandle(pField->GetApproxEnclosingMethodTable()); // <REVISIT_TODO> this needs to be checked - see bug 184355 </REVISIT_TODO>
2728 RETURN_CLASS_OBJECT(th, NULL);
2732 FCIMPL1(INT32, RuntimeFieldHandle::GetToken, ReflectFieldObject *pFieldUNSAFE) {
2738 REFLECTFIELDREF refField = (REFLECTFIELDREF)ObjectToOBJECTREF(pFieldUNSAFE);
2740 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2742 FieldDesc *pField = refField->GetField();
2744 INT32 tkFieldDef = (INT32)pField->GetMemberDef();
2745 _ASSERTE(!IsNilToken(tkFieldDef) || tkFieldDef == mdFieldDefNil);
2750 FCIMPL2(FieldDesc*, RuntimeFieldHandle::GetStaticFieldForGenericType, FieldDesc *pField, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2757 REFLECTCLASSBASEREF refDeclaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2759 if ((refDeclaringType == NULL) || (pField == NULL))
2760 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2762 TypeHandle declaringType = refDeclaringType->GetType();
2765 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2766 if (declaringType.IsTypeDesc())
2767 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2768 MethodTable *pMT = declaringType.AsMethodTable();
2770 _ASSERTE(pField->IsStatic());
2771 if (pMT->HasGenericsStaticsInfo())
2772 pField = pMT->GetFieldDescByIndex(pField->GetApproxEnclosingMethodTable()->GetIndexForFieldDesc(pField));
2773 _ASSERTE(!pField->IsSharedByGenericInstantiations());
2774 _ASSERTE(pField->GetEnclosingMethodTable() == pMT);
2780 FCIMPL1(ReflectModuleBaseObject*, AssemblyHandle::GetManifestModule, AssemblyBaseObject* pAssemblyUNSAFE) {
2783 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2785 if (refAssembly == NULL)
2786 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2788 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2789 Assembly* currentAssembly = pAssembly->GetCurrentAssembly();
2791 if (currentAssembly == NULL)
2794 Module *pModule = currentAssembly->GetManifestModule();
2795 DomainFile * pDomainFile = pModule->FindDomainFile(GetAppDomain());
2800 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2801 orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2802 if (orModule == NULL)
2803 orModule = pModule->GetExposedObject();
2805 OBJECTREF orModule = (pDomainFile != NULL) ? pDomainFile->GetExposedModuleObjectIfExists() : NULL;
2806 if (orModule != NULL)
2807 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2809 HELPER_METHOD_FRAME_BEGIN_RET_1(refAssembly);
2810 orModule = pModule->GetExposedObject();
2813 HELPER_METHOD_FRAME_END();
2814 return (ReflectModuleBaseObject*)OBJECTREFToObject(orModule);
2819 FCIMPL1(INT32, AssemblyHandle::GetToken, AssemblyBaseObject* pAssemblyUNSAFE) {
2822 ASSEMBLYREF refAssembly = (ASSEMBLYREF)ObjectToOBJECTREF(pAssemblyUNSAFE);
2824 if (refAssembly == NULL)
2825 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2827 DomainAssembly *pAssembly = refAssembly->GetDomainAssembly();
2828 mdAssembly token = mdAssemblyNil;
2830 IMDInternalImport *mdImport = pAssembly->GetCurrentAssembly()->GetManifestImport();
2834 if (FAILED(mdImport->GetAssemblyFromScope(&token)))
2836 FCThrow(kBadImageFormatException);
2845 void QCALLTYPE ModuleHandle::GetPEKind(QCall::ModuleHandle pModule, DWORD* pdwPEKind, DWORD* pdwMachine)
2850 pModule->GetFile()->GetPEKindAndMachine(pdwPEKind, pdwMachine);
2854 FCIMPL1(INT32, ModuleHandle::GetMDStreamVersion, ReflectModuleBaseObject * pModuleUNSAFE)
2858 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2860 if (refModule == NULL)
2861 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2863 Module *pModule = refModule->GetModule();
2865 if (pModule->IsResource())
2868 return pModule->GetMDImport()->GetMetadataStreamVersion();
2872 void QCALLTYPE ModuleHandle::GetModuleType(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retType)
2876 TypeHandle globalTypeHandle = TypeHandle();
2882 globalTypeHandle = TypeHandle(pModule->GetGlobalMethodTable());
2884 EX_SWALLOW_NONTRANSIENT;
2886 if (!globalTypeHandle.IsNull())
2889 retType.Set(globalTypeHandle.GetManagedClassObject());
2897 FCIMPL1(INT32, ModuleHandle::GetToken, ReflectModuleBaseObject * pModuleUNSAFE) {
2903 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2905 if (refModule == NULL)
2906 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2908 Module *pModule = refModule->GetModule();
2910 if (pModule->IsResource())
2913 return pModule->GetMDImport()->GetModuleFromScope();
2917 FCIMPL1(IMDInternalImport*, ModuleHandle::GetMetadataImport, ReflectModuleBaseObject * pModuleUNSAFE)
2921 REFLECTMODULEBASEREF refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
2923 if (refModule == NULL)
2924 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2926 Module *pModule = refModule->GetModule();
2928 if (pModule->IsResource())
2931 return pModule->GetMDImport();
2935 BOOL QCALLTYPE ModuleHandle::ContainsPropertyMatchingHash(QCall::ModuleHandle pModule, INT32 tkProperty, ULONG hash)
2939 BOOL fContains = TRUE;
2943 fContains = pModule->MightContainMatchingProperty(tkProperty, hash);
2950 void QCALLTYPE ModuleHandle::ResolveType(QCall::ModuleHandle pModule, INT32 tkType, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retType)
2954 TypeHandle typeHandle;
2958 _ASSERTE(!IsNilToken(tkType));
2960 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2961 typeHandle = ClassLoader::LoadTypeDefOrRefOrSpecThrowing(pModule, tkType, &typeContext,
2962 ClassLoader::ThrowIfNotFound,
2963 ClassLoader::PermitUninstDefOrRef);
2966 retType.Set(typeHandle.GetManagedClassObject());
2973 MethodDesc *QCALLTYPE ModuleHandle::ResolveMethod(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount)
2977 MethodDesc* pMD = NULL;
2981 _ASSERTE(!IsNilToken(tkMemberRef));
2983 BOOL strictMetadataChecks = (TypeFromToken(tkMemberRef) == mdtMethodSpec);
2985 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
2986 pMD = MemberLoader::GetMethodDescFromMemberDefOrRefOrSpec(pModule, tkMemberRef, &typeContext, strictMetadataChecks, FALSE);
2988 // This will get us the instantiating or unboxing stub if needed
2989 pMD = MethodDesc::FindOrCreateAssociatedMethodDescForReflection(pMD, pMD->GetMethodTable(), pMD->GetMethodInstantiation());
2996 void QCALLTYPE ModuleHandle::ResolveField(QCall::ModuleHandle pModule, INT32 tkMemberRef, TypeHandle *typeArgs, INT32 typeArgsCount, TypeHandle *methodArgs, INT32 methodArgsCount, QCall::ObjectHandleOnStack retField)
3000 FieldDesc* pField = NULL;
3004 _ASSERTE(!IsNilToken(tkMemberRef));
3006 SigTypeContext typeContext(Instantiation(typeArgs, typeArgsCount), Instantiation(methodArgs, methodArgsCount));
3007 pField = MemberLoader::GetFieldDescFromMemberDefOrRef(pModule, tkMemberRef, &typeContext, FALSE);
3009 retField.Set(pField->GetStubFieldInfo());
3016 void QCALLTYPE ModuleHandle::GetAssembly(QCall::ModuleHandle pModule, QCall::ObjectHandleOnStack retAssembly)
3020 DomainAssembly *pAssembly = NULL;
3023 pAssembly = pModule->GetDomainAssembly();
3026 retAssembly.Set(pAssembly->GetExposedAssemblyObject());
3032 FCIMPL5(ReflectMethodObject*, ModuleHandle::GetDynamicMethod, ReflectMethodObject *pMethodUNSAFE, ReflectModuleBaseObject *pModuleUNSAFE, StringObject *name, U1Array *sig, Object *resolver) {
3035 PRECONDITION(CheckPointer(name));
3036 PRECONDITION(CheckPointer(sig));
3040 DynamicMethodDesc *pNewMD = NULL;
3045 OBJECTREF resolverRef;
3046 OBJECTREF methodRef;
3047 REFLECTMETHODREF retMethod;
3048 REFLECTMODULEBASEREF refModule;
3050 gc.nameRef = (STRINGREF)name;
3051 gc.resolverRef = (OBJECTREF)resolver;
3052 gc.methodRef = ObjectToOBJECTREF(pMethodUNSAFE);
3053 gc.retMethod = NULL;
3054 gc.refModule = (REFLECTMODULEBASEREF)ObjectToOBJECTREF(pModuleUNSAFE);
3056 if (gc.refModule == NULL)
3057 FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
3059 Module *pModule = gc.refModule->GetModule();
3061 HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
3063 DomainFile *pDomainModule = pModule->GetDomainFile();
3065 U1ARRAYREF dataArray = (U1ARRAYREF)sig;
3066 DWORD sigSize = dataArray->GetNumComponents();
3067 NewHolder<BYTE> pSig(new BYTE[sigSize]);
3068 memcpy(pSig, dataArray->GetDataPtr(), sigSize);
3070 DWORD length = gc.nameRef->GetStringLength();
3071 NewArrayHolder<char> pName(new char[(length + 1) * 2]);
3073 length = WszWideCharToMultiByte(CP_UTF8, 0, gc.nameRef->GetBuffer(), length, pName, (length + 1) * 2 - sizeof(char), NULL, NULL);
3075 pName[length / sizeof(char)] = '\0';
3077 DynamicMethodTable *pMTForDynamicMethods = pDomainModule->GetDynamicMethodTable();
3078 pNewMD = pMTForDynamicMethods->GetDynamicMethod(pSig, sigSize, pName);
3079 _ASSERTE(pNewMD != NULL);
3080 // pNewMD now owns pSig and pName.
3081 pSig.SuppressRelease();
3082 pName.SuppressRelease();
3084 // create a handle to hold the resolver objectref
3085 OBJECTHANDLE resolverHandle = pDomainModule->GetAppDomain()->CreateLongWeakHandle(gc.resolverRef);
3086 pNewMD->GetLCGMethodResolver()->SetManagedResolver(resolverHandle);
3087 gc.retMethod = pNewMD->GetStubMethodInfo();
3088 gc.retMethod->SetKeepAlive(gc.resolverRef);
3090 LoaderAllocator *pLoaderAllocator = pModule->GetLoaderAllocator();
3092 if (pLoaderAllocator->IsCollectible())
3093 pLoaderAllocator->AddReference();
3095 HELPER_METHOD_FRAME_END();
3097 return (ReflectMethodObject*)OBJECTREFToObject(gc.retMethod);
3101 void QCALLTYPE RuntimeMethodHandle::GetCallerType(QCall::StackCrawlMarkHandle pStackMark, QCall::ObjectHandleOnStack retType)
3107 MethodTable *pMT = NULL;
3109 pMT = SystemDomain::GetCallersType(pStackMark);
3112 retType.Set(pMT->GetManagedClassObject());