Because it's doing more work @JJS. Remember your training and use the source, Luke.
The documentation gives us some clues.
Type.GetType Method (String)
- If a type is in an assembly known to your program at compile time, it is more efficient to use in C#, GetType in Visual Basic, or in C++.
- If typeName includes the namespace but not the assembly name, this method searches only the calling object's assembly and Mscorlib.dll, in that order.
we know that typeof(T)
is a call to is compiled to ldtoken
and GetTypeFromHandle
, but what is GetTypeFromHandle
doing compared to GetTypeByName
?
Lets solve the easy one first.
GetTypeFromHandle
is defined as
[Pure]
[System.Security.SecuritySafeCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern Type GetTypeFromHandle(RuntimeTypeHandle handle);
Lets get a version of the CLR we can reference.
Shared Source Common Language Infrastructure 2.0 Release
runtimehandles.cpp
FCIMPL1(Object*, RuntimeTypeHandle::GetRuntimeType, void* th) {
CONTRACTL {
THROWS;
DISABLED(GC_TRIGGERS);
MODE_COOPERATIVE;
SO_TOLERANT;
}
CONTRACTL_END;
OBJECTREF refType = NULL;
TypeHandle typeHandle = TypeHandle::FromPtr(th);
TypeHandle* pTypeHandle = &typeHandle;
_ASSERTE(CheckPointer(pTypeHandle));
_ASSERTE(CheckPointer(pTypeHandle->AsPtr(), NULL_OK));
if (pTypeHandle->AsPtr() == NULL)
return NULL;
refType = pTypeHandle->GetManagedClassObjectIfExists();
if (refType != NULL)
return OBJECTREFToObject(refType);
HELPER_METHOD_FRAME_BEGIN_RET_ATTRIB_1(Frame::FRAME_ATTR_RETURNOBJ, refType);
refType = pTypeHandle->GetManagedClassObject();
HELPER_METHOD_FRAME_END();
return OBJECTREFToObject(refType);
}
FCIMPLEND
Alright. This is legit. We are doing a wicked simple call here to get an OBJECTREFToObject.
No searching, just looking up a type effectively by it's method table. Need a refresher on .Net internals?
OK, what about the slow method? Type.GetType Method (String)
Chase the call stack and find out that it calls RuntimeTypeHandle.GetTypeByName
[System.Security.SecurityCritical] // auto-generated
[ResourceExposure(ResourceScope.None)]
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
[SuppressUnmanagedCodeSecurity]
private extern static void GetTypeByName(string name, bool throwOnError, bool ignoreCase, bool reflectionOnly, StackCrawlMarkHandle stackMark,
#if FEATURE_HOSTED_BINDER
IntPtr pPrivHostBinder,
#endif
bool loadTypeFromPartialName, ObjectHandleOnStack type);
runtimehandles.cpp
FCIMPL6(EnregisteredTypeHandle, RuntimeTypeHandle::GetTypeByName,
StringObject* classNameUNSAFE, CLR_BOOL bThrowOnError, CLR_BOOL bIgnoreCase, CLR_BOOL bReflectionOnly, StackCrawlMark* pStackMark, CLR_BOOL bLoadTypeFromPartialNameHack)
{
CONTRACTL
{
THROWS;
DISABLED(GC_TRIGGERS);
MODE_COOPERATIVE;
SO_TOLERANT;
}
CONTRACTL_END;
STRINGREF sRef = (STRINGREF) classNameUNSAFE;
TypeHandle typeHandle;
HELPER_METHOD_FRAME_BEGIN_RET_1(sRef);
{
if (!sRef)
COMPlusThrowArgumentNull(L"className",L"ArgumentNull_String");
typeHandle = TypeName::GetTypeManaged(sRef->GetBuffer(), NULL, bThrowOnError, bIgnoreCase, bReflectionOnly, /*bProhibitAsmQualifiedName =*/ FALSE, pStackMark, bLoadTypeFromPartialNameHack);
}
HELPER_METHOD_FRAME_END();
return typeHandle.AsPtr();
}
FCIMPLEND
Fine, but what is TypeName::GetTypeManaged doing?!
typeparse.cpp
//--------------------------------------------------------------------------------------------------------------
// This everything-but-the-kitchen-sink version is what used to be called "GetType()". It exposes all the
// funky knobs needed for implementing the specific requirements of the managed Type.GetType() apis and friends.
//--------------------------------------------------------------------------------------------------------------
/*public static */ TypeHandle TypeName::GetTypeManaged
But it doesn't stop there
typeparse.cpp
// -------------------------------------------------------------------------------------------------------------
// This is the "uber" GetType() that all public GetType() funnels through. It's main job is to figure out which
// Assembly to load the type from and then invoke GetTypeHaveAssembly.
//
// It's got a highly baroque interface partly for historical reasons and partly because it's the uber-function
// for all of the possible GetTypes.
// -------------------------------------------------------------------------------------------------------------
/* private instance */ TypeHandle TypeName::GetTypeWorker
Doesn't stop here either.
typeparse.cpp
//----------------------------------------------------------------------------------------------------------------
// This is the one that actually loads the type once we've pinned down the Assembly it's in.
//----------------------------------------------------------------------------------------------------------------
/* private instance */ TypeHandle TypeName::GetTypeHaveAssembly(Assembly* pAssembly, BOOL bThrowIfNotFound, BOOL bIgnoreCase, BOOL bRecurse)
for (COUNT_T i = 0; i < names.GetCount(); i ++)
{
LPCWSTR wname = names[i]->GetUnicode();
MAKE_UTF8PTR_FROMWIDE(name, wname);
typeName.SetName(name);
th = pAssembly->GetLoader()->LoadTypeHandleThrowing(&typeName);
}
clsload.cpp
TypeHandle ClassLoader::LoadTypeHandleThrowing(NameHandle* pName, ClassLoadLevel level, Module* pLookInThisModuleOnly/*=NULL*/)
BOOL foundSomething = FindClassModuleThrowing(pName,
// FindClassModuleThrowing discovers which module the type you're looking for is in and loads the Module if necessary.
// Basically, it iterates through all of the assembly's modules until a name match is found in a module's
// AvailableClassHashTable.
if (!typeHnd.IsNull()) {
typeHnd = LoadTypeDefThrowing(typeHnd.GetModule(), typeHnd.GetCl(),
// Given a token specifying a typeDef, and a module in which to
// interpret that token, find or load the corresponding type handle.
typeHnd = pModule->LookupTypeDef(typeDef, level);
ceeload.h
TypeHandle LookupTypeDef(mdTypeDef token, ClassLoadLevel level = CLASS_LOAD_UNRESTOREDTYPEKEY)
which is giving us the TypeHandle. The same thing we got in a single stack frame of GetTypeFromHandle
PTR_MethodTable pMT = PTR_MethodTable(GetFromRidMap(&m_TypeDefToMethodTableMap, RidFromToken(token)));
if (pMT == NULL || pMT->GetLoadLevel() < level)
return TypeHandle();
else
return (TypeHandle)pMT;
So... What's slow? The iteration in FindClassModuleThrowing. It has to iterate through names to lookup the Method Table... Iterating over an array, is always slower than looking up something by a known key, which was available in GetTypeFromHandle
Case closed.