Assembly.Load() and Type.GetType()

  • Thread starter Thomas Sondergaard
  • 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
 
J

Jon Skeet

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.
 
C

Conrad Zhang

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"
 
R

Richard Grimes [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top