System.Type.GetType problem

F

Frank

Anyone Please Help,

Why does this statement work:

Type objType = System.Type.GetType("System.String");

And this one does not work:

Type objType = System.Type.GetType("System.Windows.Forms.DataGridTextBoxColumn");

when I run this statement I get the following exception:

File or assembly name System.Windows.Forms, or one of its
dependencies, was not found.

I read MSDN and saw the you have to use a fully qualified name so I
have also tried:

Type objType = System.Type.GetType("System.Windows.Forms.DataGridTextBoxColumn,System.Windows.Forms.dll");

But this did not work either. I know it has something to do with
finding the location of the assembly.

The end result I want is to read a string from an Xml file at runtime
and create an object from it.

Thanks in advance,

Frank
 
J

Jon Skeet [C# MVP]

Frank said:
Anyone Please Help,

Why does this statement work:

Type objType = System.Type.GetType("System.String");

And this one does not work:

Type objType = System.Type.GetType("System.Windows.Forms.DataGridTextBoxColumn");

Because System.String is in mscorlib, but
System.Windows.Forms.DataGridTextBoxColumn isn't.

When you give a simple type name, it only looks in mscorlib and the
currently executing assembly.
when I run this statement I get the following exception:

File or assembly name System.Windows.Forms, or one of its
dependencies, was not found.

I read MSDN and saw the you have to use a fully qualified name so I
have also tried:

Type objType = System.Type.GetType("System.Windows.Forms.DataGridTextBoxColumn,System.Windows.Forms.dll");

That's not a fully qualified name - give the version information as
well and it'll find it in the GAC.
But this did not work either. I know it has something to do with
finding the location of the assembly.

The end result I want is to read a string from an Xml file at runtime
and create an object from it.

If you have a reference to the appropriate assembly, you could always
load all the referenced assemblies (see
Assembly.GetReferencedAssemblies) and then check in each referenced
assembly for the type (Assembly.GetType).
 
F

Frank Wisniewski [MCSE 4.0, A+]

Jon,

Thanks, I think I understand now, but let me ask you, what would be the best
way to do what I want then? (Being able to store the type name in XML and
then loading it at runtime) I don't really want to store all that extra
info in my xml file. How do you know what the version, culture, and
publickeytoken fields mean in relation to the string you pass to gettype. I
have search MSDN and never saw anything about them in the gettype
definition.

Thanks,

Frank
 
J

Jon Skeet [C# MVP]

Frank Wisniewski said:
Thanks, I think I understand now, but let me ask you, what would be the best
way to do what I want then? (Being able to store the type name in XML and
then loading it at runtime) I don't really want to store all that extra
info in my xml file. How do you know what the version, culture, and
publickeytoken fields mean in relation to the string you pass to gettype. I
have search MSDN and never saw anything about them in the gettype
definition.

As I said, you can load all the referenced assemblies and look in those
if you want to. It's not very much extra code, but you should of course
check that you don't load the same assembly twice, etc.
 

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