Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2008

Tip: Looking for answers? Try searching our database.

Type.GetType(string) question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Eckart - 20 May 2008 08:53 GMT
Hi folks,

Who can explain me why the following expression does not result in getting
the correct type, but null:

Type t = Type.GetType("System.Xml.XmlReader");

For "System.String" it works as well as for "System.IO.Stream" or
"System.Globalization.CultureInfo". Does that related to the constructor of
XmlReader being not public?

Thanks,
Martin
Marc Gravell - 20 May 2008 09:12 GMT
Type.GetType() doesn't automatically search all assemblies for the type;
 actually, it expects an "assembly qualified name", but will forgive
you  (i.e. allow the short name) if the type is found in either the
calling assembly (your code) or mscorlib.dll (common types).

The following is the aqn version of XmlReader (watch for wrap):

Type t = Type.GetType("System.Xml.XmlReader, System.Xml,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

The easiest way to get the aqn is via:
string aqn = typeof(XmlReader).AssemblyQualifiedName;

(then write that to the trace or something if you want it for a config
file etc)

Marc
Martin Eckart - 20 May 2008 09:25 GMT
Hi Marc,

That already helps.

Now, the issue I have is that I get a string value set to
"System.Xml.XmlReader". But it could also be "System.String" or
"System.Xml.XmlNodeList" or whatever. Therefore I cannot issue the code line
string aqn = typeof(XmlReader).AssemblyQualifiedName;
statically, but have to replace the "typeof(XmlReader)" by something like
"typeof(<myInputString>)".

Is there a way to accomplish that?

Thanks,
Martin

> Type.GetType() doesn't automatically search all assemblies for the type;
> actually, it expects an "assembly qualified name", but will forgive you
[quoted text clipped - 13 lines]
>
> Marc
Jon Skeet [C# MVP] - 20 May 2008 09:55 GMT
> That already helps.
>
[quoted text clipped - 6 lines]
>
> Is there a way to accomplish that?

No - typeof(...) is a compile-time operator - it looks up the type
name based on the context of the code and the referenced assemblies,
and works out the fully-qualified name at compile-time.

What you *can* do is recursively look through the assemblies
referenced by your current assembly (I can't remember the method name
off hand, but it's something obvious like
Assembly.GetReferencedAssemblies) and call Assembly.GetType on each of
those assemblies until you find the type.

Jon
Marc Gravell - 20 May 2008 10:38 GMT
> What you *can* do is recursively look through the assemblies
> referenced by your current assembly

Actually, one thing to note here is that the compiler is clever - it
will drop things that you have referenced but not used... just one to
watch if it doesn't work. You also need to watch for the circular
reference at the bottom ;-p

But something like below.

Marc

using System;
using System.Collections.Generic;
using System.Reflection;
static class Program
{
    static void Main()
    {
        Type type = GetType("System.Xml.XmlReader");
    }
    static Type GetType(string name)
    {
        // tryu the lazy way first
        Type type = Type.GetType(name);
        if (type != null) return type;

        List<string> skip = new List<string>();
        AssemblyName root = Assembly.GetEntryAssembly().GetName();
        return WalkAssemblies(name, root, skip);
    }
    static Type WalkAssemblies(string name, AssemblyName an,
IList<string> skip)
    {
        // check "an" for the type
        skip.Add(an.FullName);
        Assembly a;
        try {
            a = Assembly.Load(an);
        } catch {
            return null; // oops
        }
        Type type = a.GetType(name);
        if (type != null) return type;

        // see what is referenced
        foreach(AssemblyName nextRef in a.GetReferencedAssemblies()) {
            if(skip.Contains(nextRef.FullName)) continue;
            type = WalkAssemblies(name, nextRef, skip);
            if (type != null) return type;
        }
        return null;

    }
}
Marc Gravell - 20 May 2008 10:23 GMT
The "typeof" line was just so you could obtain the assembly-qualified
name of the type - this isn't something you'd normally keep in the real
code. The idea being to use the assembly-qualified name *instead* of
"System.Xml.XmlReader". Of course, if this isn't an option you'll have
to start trawling assemlbies...

Marc
Hans Kesting - 20 May 2008 11:57 GMT
> Type.GetType() doesn't automatically search all assemblies for the type;
> actually, it expects an "assembly qualified name", but will forgive you
[quoted text clipped - 5 lines]
> Type t = Type.GetType("System.Xml.XmlReader, System.Xml, Version=2.0.0.0,
> Culture=neutral, PublicKeyToken=b77a5c561934e089");

If you use just typename and assembly-name (no "Version" etc), then GetType
will find it (*if* the assembly was referenced).
So: Type t = Type.GetType("System.Xml.XmlReader, System.Xml");

Hans Kesting
Peter Duniho - 20 May 2008 09:37 GMT
> Who can explain me why the following expression does not result in  
> getting
> the correct type, but null:
>
> Type t = Type.GetType("System.Xml.XmlReader");

Do you have the System.Xml assembly referenced in your assembly?

Pete
Martin Eckart - 20 May 2008 09:44 GMT
Yes, have. Has nothing do to with it.

On Tue, 20 May 2008 00:53:04 -0700, Martin Eckart <moartl17ATyahoo.de>
wrote:

> Who can explain me why the following expression does not result in
> getting
> the correct type, but null:
>
> Type t = Type.GetType("System.Xml.XmlReader");

Do you have the System.Xml assembly referenced in your assembly?

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.