How Do I...

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

how do I convert a string representation of a system.Type into a System.Type
object?

Cheers

Earth Worm Jim
 
how do I convert a string representation of a system.Type into a System.Type
object?

That entirely depends on the string representation and the type
involved. If you control the type involved, you can use the
TypeConverter class to do some of the work for you - if you look up
TypeConverter in the MSDN, you'll find quite a lot of information to
help you.
 
Hi, Jim.

if you know the fullname of the type, you can get the type like that.
Type myType1 = Type.GetType("System.Int32");

regards
 
Jon Skeet said:
That entirely depends on the string representation and the type
involved. If you control the type involved, you can use the
TypeConverter class to do some of the work for you - if you look up
TypeConverter in the MSDN, you'll find quite a lot of information to
help you.

Apologies - having read Ryan's answer, I see what you mean :)

Ryan's on the right track - but there is one caveat, which is that when
Type.GetType is presented with just the type name (and no assembly
information) it only looks in the current assembly and mscorlib, so
Type.GetType ("System.Windows.Forms.Form") will return null, for
instance. If your string contains the full assembly information, that's
used appropriately - otherwise you can use Assembly.GetType if you know
the assembly which contains the type you're after.

Sorry again about the previous reply...
 
Back
Top