Solved

.NET - how to find type by name?

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

12 Experts available now in Live!

Get 1:1 Help Now