Link to home
Create AccountLog in
Avatar of Dmitry_Bond
Dmitry_BondFlag for Ukraine

asked on

.NET - how to find type by name?

Hi.

I need an ability to find type by name.
At the moment I'm using following code:

Type t = Type.GetType("System.Data.DataTable", false, true);

but it always returns null! :-(
Why?!

The application which is run this code has a reference to System.Data.dll, so I assume it must be able to find a "System.Data.DataTable".
Any ideas?
SOLUTION
Avatar of markmiddlemist
markmiddlemist
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
If you already have a DataTable, you can call GetType directly on your object. Otherwise, you can instantiate a new object in order to retrieve the type:

System.Data.DataTable tb = new System.Data.DataTable;
Type t = tb.GetType();

Otherwise, you need to know the name and path of the assembly (name of the dll) that contains the type to be able to retrieve the type information.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Dmitry_Bond

ASKER

Nobody of commenters suggested a solution. Only markmiddlemist posted a useful link explaining why GetType does not work as I need it.