How to get instance Type from Type.GetType function

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").
 
J

Jon Skeet [C# MVP]

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

Richard Blewett [DevelopMentor]

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]
 

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