Solved

.NET - how to find type by name?

Posted on 2012-03-10
4
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

628 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