Type.GetType(string) question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Eckart

    Type.GetType(string) question

    Hi folks,

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

    Type t = Type.GetType("S ystem.Xml.XmlRe ader");

    For "System.Str ing" it works as well as for "System.IO.Stre am" or
    "System.Globali zation.CultureI nfo". Does that related to the constructor of
    XmlReader being not public?

    Thanks,
    Martin


  • Marc Gravell

    #2
    Re: Type.GetType(st ring) question

    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("S ystem.Xml.XmlRe ader, System.Xml,
    Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9");

    The easiest way to get the aqn is via:
    string aqn = typeof(XmlReade r).AssemblyQual ifiedName;

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

    Marc

    Comment

    • Martin Eckart

      #3
      Re: Type.GetType(st ring) question

      Hi Marc,

      That already helps.

      Now, the issue I have is that I get a string value set to
      "System.Xml.Xml Reader". But it could also be "System.Str ing" or
      "System.Xml.Xml NodeList" or whatever. Therefore I cannot issue the code line
      string aqn = typeof(XmlReade r).AssemblyQual ifiedName;
      statically, but have to replace the "typeof(XmlRead er)" by something like
      "typeof(<myInpu tString>)".

      Is there a way to accomplish that?

      Thanks,
      Martin

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:On4D1EluIH A.3564@TK2MSFTN GP03.phx.gbl...
      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("S ystem.Xml.XmlRe ader, System.Xml, Version=2.0.0.0 ,
      Culture=neutral , PublicKeyToken= b77a5c561934e08 9");
      >
      The easiest way to get the aqn is via:
      string aqn = typeof(XmlReade r).AssemblyQual ifiedName;
      >
      (then write that to the trace or something if you want it for a config
      file etc)
      >
      Marc

      Comment

      • Peter Duniho

        #4
        Re: Type.GetType(st ring) question

        On Tue, 20 May 2008 00:53:04 -0700, Martin Eckart <moartl17ATyaho o.de
        wrote:
        Who can explain me why the following expression does not result in
        getting
        the correct type, but null:
        >
        Type t = Type.GetType("S ystem.Xml.XmlRe ader");
        Do you have the System.Xml assembly referenced in your assembly?

        Pete

        Comment

        • Martin Eckart

          #5
          Re: Type.GetType(st ring) question

          Yes, have. Has nothing do to with it.


          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
          news:op.ubf180v r8jd0ej@petes-computer.local. ..
          On Tue, 20 May 2008 00:53:04 -0700, Martin Eckart <moartl17ATyaho o.de>
          wrote:
          Who can explain me why the following expression does not result in
          getting
          the correct type, but null:
          >
          Type t = Type.GetType("S ystem.Xml.XmlRe ader");
          Do you have the System.Xml assembly referenced in your assembly?

          Pete


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Type.GetType(st ring) question

            On May 20, 9:25 am, "Martin Eckart" <moartl17ATyaho o.dewrote:
            That already helps.
            >
            Now, the issue I have is that I get a string value set to
            "System.Xml.Xml Reader". But it could also be "System.Str ing" or
            "System.Xml.Xml NodeList" or whatever. Therefore I cannot issue the code line
            string aqn = typeof(XmlReade r).AssemblyQual ifiedName;
            statically, but have to replace the "typeof(XmlRead er)" by something like
            "typeof(<myInpu tString>)".
            >
            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.GetRef erencedAssembli es) and call Assembly.GetTyp e on each of
            those assemblies until you find the type.

            Jon

            Comment

            • Marc Gravell

              #7
              Re: Type.GetType(st ring) question

              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.Xml Reader". Of course, if this isn't an option you'll have
              to start trawling assemlbies...

              Marc

              Comment

              • Marc Gravell

                #8
                Re: Type.GetType(st ring) question

                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.Collecti ons.Generic;
                using System.Reflecti on;
                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(na me);
                if (type != null) return type;

                List<stringskip = new List<string>();
                AssemblyName root = Assembly.GetEnt ryAssembly().Ge tName();
                return WalkAssemblies( name, root, skip);
                }
                static Type WalkAssemblies( string name, AssemblyName an,
                IList<stringski p)
                {
                // check "an" for the type
                skip.Add(an.Ful lName);
                Assembly a;
                try {
                a = Assembly.Load(a n);
                } catch {
                return null; // oops
                }
                Type type = a.GetType(name) ;
                if (type != null) return type;

                // see what is referenced
                foreach(Assembl yName nextRef in a.GetReferenced Assemblies()) {
                if(skip.Contain s(nextRef.FullN ame)) continue;
                type = WalkAssemblies( name, nextRef, skip);
                if (type != null) return type;
                }
                return null;

                }
                }

                Comment

                • Hans Kesting

                  #9
                  Re: Type.GetType(st ring) question



                  "Marc Gravell" <marc.gravell@g mail.comschreef in bericht
                  news:On4D1EluIH A.3564@TK2MSFTN GP03.phx.gbl...
                  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("S ystem.Xml.XmlRe ader, System.Xml, Version=2.0.0.0 ,
                  Culture=neutral , PublicKeyToken= b77a5c561934e08 9");
                  >
                  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("S ystem.Xml.XmlRe ader, System.Xml");

                  Hans Kesting


                  Comment

                  Working...