Solved

.NET - how to find type by name?

Posted on 2012-03-10
4
252 Views
Last Modified: 2012-03-15
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?
0
Comment
Question by:Dmitry_Bond
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:markmiddlemist
markmiddlemist earned 500 total points
ID: 37704876
The GetType method expects the assembly qualified name if the type (as here) isn't in Mscorlib.

Is this code to be used in such a way that different types are needed, or could you use typeof(System.Data.DataTable)

If not there is info here - http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx - on the AQN
0
 
LVL 40
ID: 37705456
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.
0
 

Accepted Solution

by:
Dmitry_Bond earned 0 total points
ID: 37705475
The idea is to obtain a type by a string which is a name of type.
Is required for the C# scripting - just need a some special statement in script like "#using_type System.Data.DataTable", so by this directive application should automatically add a reference to "System.Data" assembly to parameters for a script code compiler...

Finally I have solved it by following code:

public static Type FindType(string pFullTypeName)
{
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    foreach (Assembly asm in assemblies)
    {
        Type[] types = asm.GetTypes();
        foreach (Type t in types)
        {
            if (string.Compare(pFullTypeName, t.FullName, true) == 0)
                return t;
        }
    }
    return null;
}

Open in new window

0
 

Author Closing Comment

by:Dmitry_Bond
ID: 37723927
Nobody of commenters suggested a solution. Only markmiddlemist posted a useful link explaining why GetType does not work as I need it.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net winforms sizing/resolution? 4 46
VB.NET 2008 - SQL Timeout 9 34
Help with opening combobox using VB.NET. 3 26
Visual Studio npm 1 12
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question