Another answer shows the best way for obtaining (at runtime) a Type
defined in an Assembly
that might not be loaded:
var T1 = Type.GetType("System.Web.Configuration.IAssemblyCache, " +
"System.Web, " +
"Version=4.0.0.0, " +
"Culture=neutral, " +
"PublicKeyToken=b03f5f7f11d50a3a");
As you can see, unfortunately that method requires you to supply the full AssemblyQualifiedName of the Type
and will not work with any of the abbreviated forms of the assembly name that I tried. This somewhat defeats our main purposes here. If you knew that much detail about the assembly, it wouldn't be that much harder to just load it yourself:
var T2 = Assembly.Load("System.Web, " +
"Version=4.0.0.0, " +
"Culture=neutral, " +
"PublicKeyToken=b03f5f7f11d50a3a")
.GetType("System.Web.Configuration.IAssemblyCache");
Although these two examples look similar, they execute very different code paths; note for example, that the latter version calls an instance overload on Assembly.GetType
versus the static call Type.GetType
. Because of this, the second version is probably faster or more efficient. Either way, though, you seem to end up at the following CLR internal method, and with the second argument set to false
, and this is why neither method goes to the trouble of searching for the required assembly on your behalf.
[System.Runtime.Remoting.RemotingServices]
private static RuntimeType LoadClrTypeWithPartialBindFallback(
String typeName,
bool partialFallback);
A tiny step forward from these inconveniences would be to instead call this CLR method yourself, but with the partialFallback
parameter set to true. In this mode, the function will accept a truncated version of the AssemblyQualifiedName
and will locate and load the relevant assembly, as necessary:
static Func<String, bool, TypeInfo> LoadClrTypeWithPartialBindFallback =
typeof(RemotingServices)
.GetMethod("LoadClrTypeWithPartialBindFallback", (BindingFlags)0x28)
.CreateDelegate(typeof(Func<String, bool, TypeInfo>))
as Func<String, bool, TypeInfo>;
// ...
var T3 = LoadClrTypeWithPartialBindFallback(
"System.Web.Configuration.IAssemblyCache, System.Web",
true); // <-- enables searching for the assembly
This works as shown, and also continues to support specifying the full AssemblyQualifiedName
as in the earlier examples. This is a small improvement, but it's still not a fully unqualified namespace search, because you do still have to specify the short name of the assembly, even if it might be deduced from the namespace that appears in the type name itself.