public static Type GetType(string typeName)
{
Type type;
lock (typeDictionaryLock)
{
if (typeDictionary.TryGetValue(typeName, out type))
{
return(type);
}
}
type = Type.GetType(typeName);
if (type == null)
{
foreach (System.Reflection.Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
{
try
{
type = assembly.GetType(typeName);
if (type != null)
{
break;
}
}
catch
{
// Skip problematic assemblies
}
}
if (type == null)
{
string fullTypeName;
foreach (string nameSpace in interpreter.GetNamespaces())
{
fullTypeName = nameSpace + "." + typeName;
foreach (System.Reflection.Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
{
try
{
type = assembly.GetType(fullTypeName);
if (type != null)
{
break;
}
}
catch
{
// Skip problematic assemblies
}
}
if (type != null)
{
break;
}
}
}
}
// Cache the lookup result to speeds up subsequent lookups
// NOTE: Failed lookups are also cached by inserting null values,
// which prevents additional lengthy repeats of the process
lock (typeDictionaryLock)
{
if (typeDictionary.ContainsKey(typeName))
{
// Compensate for a possible race condition
if (typeDictionary[typeName] != null)
{
type = typeDictionary[typeName];
}
else if (type != null)
{
typeDictionary[typeName] = type;
}
}
else
{
typeDictionary.Add(typeName, type);
}
}
return(type);
}