Assembly.Load() and Type.GetType()

  • Thread starter Thread starter Thomas Sondergaard
  • Start date Start date
T

Thomas Sondergaard

Why doesn't this work:

Assembly.Load(AssemblyName.GetAssemblyName("C:/WINDOWS/Microsoft.NET/Framewo
rk/v1.1.4322/System.Windows.Forms.dll"));

Type t = Type.GetType("System.Windows.Forms.Label");


I would except Type.GetType() above to return an instance of Type but it
returns null. If I iterate over all the types in all the assemblies I can
find it, but Type.GetType() can't.

Any ideas?

Cheers,

Thomas
 
Thomas Sondergaard said:
Why doesn't this work:

Assembly.Load(AssemblyName.GetAssemblyName("C:/WINDOWS/Microsoft.NET/Framewo
rk/v1.1.4322/System.Windows.Forms.dll"));

Type t = Type.GetType("System.Windows.Forms.Label");

I would except Type.GetType() above to return an instance of Type but it
returns null. If I iterate over all the types in all the assemblies I can
find it, but Type.GetType() can't.

When a method doesn't behave as you expect it to, read the
documentation *very carefully*. You are calling Type.GetType(string),
and the documentation for that contains this:

<quote>
If typeName includes only the name of the Type, this method searches in
the calling object's assembly, then in the mscorlib.dll assembly. If
typeName is fully qualified with the partial or complete assembly name,
this method searches in the specified assembly.
</quote>

In other words, the appropriate assembly isn't being checked. Either
specify the assembly in the call to GetType, or use Assembly.GetType on
the appropriate assembly.
 
Don't hard-code the path of System.Windowns.Forms.dll. Use its identity:

Assembly.Load("system.windows.forms, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
 
Thomas said:
Why doesn't this work:

Assembly.Load(AssemblyName.GetAssemblyName("C:/WINDOWS/Microsoft.NET/Framewo
rk/v1.1.4322/System.Windows.Forms.dll"));

This is not the windows forms assembly. The windows form assembly (like all
of the other framework assemblies) is ngen'd (ie pre JIT compiled) and is
installed in the GAC (look in C:\windows\assembly\gac using a command line,
Windows Explorer hides the structure of this folder). The file you mention
is the intermediate language/metadata file *before* it is ngen'd. This is
used by the framework and compilers and debuggers to get the metadata for
the assembly. You don't use this file at run time.

Richard
 
Back
Top