Solved

.NET - how to find type by name?

Posted on 2012-03-10
4
246 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Red error squiggly on vb.net 7 27
Error in page 3 46
.Net Web Site IIS Web.Config Content-Security-Policy 1 43
C# guarantee sql connection close 6 25
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…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now