How to get instance Type from Type.GetType function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use Type.GetType(name of type in string format) to get an
instance Type. For example: Type t = Type.GetType("System.Data.DataTable").
I have no trouble to get a value type For example: Type t =
Type.GetType("System.String").
 
I am trying to use Type.GetType(name of type in string format) to get an
instance Type. For example: Type t = Type.GetType("System.Data.DataTable").
I have no trouble to get a value type For example: Type t =
Type.GetType("System.String").

typeof is the best way of doing this if you know the name in advance.
If you don't, you also need to know the assembly the type is in -
Type.GetType only looks in the currently executing assembly and
mscorlib unless you specify the assembly name as well.
 
You need to add the assembly name in for any type that is not in the corrent assembly or mscorlib. So

Type t = Type.GetType("System.Data.DataTable, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

Will work (for version 1.1 of the framework - if you're using 1.0 use the version number for that one)

Btw: System.String is not a Value Type, it a Reference Type

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#[email protected]>

Did you try "typeof(...)" ?

- Sahil Malik
You can reach me thru my blog -
http://www.dotnetjunkies.com/weblog/sahilmalik

I am trying to use Type.GetType(name of type in string format) to get an
instance Type. For example: Type t =
Type.GetType("System.Data.DataTable").
I have no trouble to get a value type For example: Type t =
Type.GetType("System.String").



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.770 / Virus Database: 517 - Release Date: 27/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Back
Top