Type.GetType(String) not working

  • Thread starter Thread starter Gugale at Lincoln
  • Start date Start date
G

Gugale at Lincoln

In my code Type.GetType(text) works when text="System.IO.File". However, it
doesn't work when text="System.Windows.Forms.Button". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows.Forms.dll assembly?

Thanks
SG
 
Gugale at Lincoln said:
In my code Type.GetType(text) works when text="System.IO.File". However, it
doesn't work when text="System.Windows.Forms.Button". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows.Forms.dll assembly?

I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows.Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.

You can however still load the button class as long as you fully qualify
the type:

| Type.GetType("System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089")

-- Barry
 
I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows.Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.

No it's because when you don't specify the assembly name, Type.GetType
only looks in mscorlib.dll and the calling assembly. That works for
System.IO.File but not something in the Winforms assembly.


Mattias
 
Barry Kelly said:
I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows.Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.

From the docs of Type.GetType(string):

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

(The OP is mistaken about types from System.dll - System.IO.File, for
instance, is in mscorlib.dll. Trying a type which is genuinely in
System.dll, such as System.IO.FileSystemWatcher, returns null as
expected given the above docs.)
 
Mattias Sjögren said:
No it's because when you don't specify the assembly name, Type.GetType
only looks in mscorlib.dll and the calling assembly. That works for
System.IO.File but not something in the Winforms assembly.

At first I thought it was just what you mentioned, that the assembly name
is missing. But it's more than that.

If you try it with just the class name and assembly name and without the
Version, Culture and PublicKeyToken fields it still doesn't work. You need
all fields for it to work.

-- Barry
 
Jon Skeet said:
From the docs of Type.GetType(string):

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

If you try

| Type.GetType("System.Windows.Forms.Button, System.Windows.Forms")

you'll find it returns null, at least when you have both .NET 1.1 and 2.0
installed (I'm guessing). I tried before posting.

-- Barry
 
Barry Kelly said:
If you try

| Type.GetType("System.Windows.Forms.Button, System.Windows.Forms")

you'll find it returns null, at least when you have both .NET 1.1 and 2.0
installed (I'm guessing). I tried before posting.

I don't think that counts as "enough" of the assembly name - I think
you need to give at least some of the version number. Certainly if you
give the *complete* assembly name (with full version number) it will
work. There's more to an "assembly name" than the file name. It's not
terribly well documented, unfortunately.
 
Thanks Barry!

In case if anyone is wondering, as I did, you can get version number and
public key using
gacutil /l assemblyname
from Visual Studio .Net Command Prompt
 
Jon Skeet said:
I don't think that counts as "enough" of the assembly name - I think
you need to give at least some of the version number. Certainly if you
give the *complete* assembly name (with full version number) it will
work. There's more to an "assembly name" than the file name. It's not
terribly well documented, unfortunately.

The version number isn't enough. You also need *both* the Culture *and*
PublicKeyToken. I tried pretty much every combination.

I've used this method myself for extensibility scenarios, and all the
times I've used it I've only needed the type name and assembly name - I've
never seen the behaviour that System.Windows.Forms.Button manifests.

Odd. <g>

-- Barry
 

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

Back
Top