Reflection and Type.GetType

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Ducker

    Reflection and Type.GetType

    Is there any way to use Type.GetType to return a type that's in an
    Assembly loaded from disk?

    I sadly _have_ to use Type.GetType, as it's in framework code I can't
    change.

    At the moment, I'm loading the Assembly from disk (which works fine)
    and adding it to the current AppDomain (just to be on the safe side),
    but even when I do that, Type.GetType fails to find it.

    Any suggestions?

    Andy D

  • Guest's Avatar

    #2
    Re: Reflection and Type.GetType

    Are you passing in the fully qualified name to Type.GetType("M yNameSpace.MyCl ass") ?
    i.e Namespace.Class

    Kalpesh

    Comment

    • Mattias Sjögren

      #3
      Re: Reflection and Type.GetType

      >Are you passing in the fully qualified name to Type.GetType("M yNameSpace.MyCl ass") ?[color=blue]
      >i.e Namespace.Class[/color]

      You have to include the assembly name as well to get a truly FQ type
      name.


      Mattias

      --
      Mattias Sjögren [C# MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Reflection and Type.GetType

        Andrew Ducker <andrew@ducker. org.uk> wrote:[color=blue]
        > Is there any way to use Type.GetType to return a type that's in an
        > Assembly loaded from disk?
        >
        > I sadly _have_ to use Type.GetType, as it's in framework code I can't
        > change.
        >
        > At the moment, I'm loading the Assembly from disk (which works fine)
        > and adding it to the current AppDomain (just to be on the safe side),
        > but even when I do that, Type.GetType fails to find it.
        >
        > Any suggestions?[/color]

        Yes - specify the assembly name in the call to Type.GetType. For
        instance:

        using System;

        public class Test
        {
        static void Main()
        {
        Type type = Type.GetType("S ystem.Data.SqlC lient.SqlComman d, " +
        "System.Dat a, "+
        "Version=1.0.50 00.0, "+
        "Culture=neutra l, "+
        "PublicKeyToken =b77a5c561934e0 89");
        Console.WriteLi ne (type);
        }
        }


        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Guest's Avatar

          #5
          Re: Reflection and Type.GetType

          Thanks for correcting Mattias

          Kalpesh

          Comment

          • Guest's Avatar

            #6
            Re: Reflection and Type.GetType

            Jon,

            What if the assembly (not signed) is loaded form filesystem (not from GAC) ?

            How does one still find the type in such assembly ?
            (except Assembly.Get... . methods())

            How can I use Type.GetType with this kind of assembly ?

            Kalpesh

            Comment

            • Guest's Avatar

              #7
              Re: Reflection and Type.GetType

              Hi Jon,

              The MSDN Doc says (for Type.GetType)
              "GetType only works on assemblies loaded from disk"

              So, in order to get a class, this is the naming scheme - it should use
              TopNamespace.Su bNameSpace.Cont ainingClass+Nes tedClass,MyAsse mbly

              Right?

              Kalpesh

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Reflection and Type.GetType

                <Kalpesh/> <kalpesh@disc.m icrosoft.com> wrote:[color=blue]
                > The MSDN Doc says (for Type.GetType)
                > "GetType only works on assemblies loaded from disk"
                >
                > So, in order to get a class, this is the naming scheme - it should use
                > TopNamespace.Su bNameSpace.Cont ainingClass+Nes tedClass,MyAsse mbly[/color]

                Yes, that's right.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Andrew Ducker

                  #9
                  Re: Reflection and Type.GetType

                  Hmm, in which case this should work - but doesn't:

                  OpenFileDialog fd = new OpenFileDialog( );
                  fd.ShowDialog(t his);
                  Assembly a = Assembly.LoadFi le(fd.FileName) ;
                  Type t= a.GetType("Test Class.FirstTest Class");
                  MessageBox.Show (t.AssemblyQual ifiedName);
                  Type t2 = Type.GetType(t. AssemblyQualifi edName);
                  MessageBox.Show (t2.AssemblyQua lifiedName);

                  In fact, the a.GetType works (providing you've created a class called
                  FirstTestClass in the Namespace TestClass), but the Type.GetType
                  doesn't.

                  Any ideas why?

                  Andy D

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Reflection and Type.GetType

                    Andrew Ducker <andrew@ducker. org.uk> wrote:[color=blue]
                    > Hmm, in which case this should work - but doesn't:
                    >
                    > OpenFileDialog fd = new OpenFileDialog( );
                    > fd.ShowDialog(t his);
                    > Assembly a = Assembly.LoadFi le(fd.FileName) ;
                    > Type t= a.GetType("Test Class.FirstTest Class");
                    > MessageBox.Show (t.AssemblyQual ifiedName);
                    > Type t2 = Type.GetType(t. AssemblyQualifi edName);
                    > MessageBox.Show (t2.AssemblyQua lifiedName);
                    >
                    > In fact, the a.GetType works (providing you've created a class called
                    > FirstTestClass in the Namespace TestClass), but the Type.GetType
                    > doesn't.
                    >
                    > Any ideas why?[/color]

                    Yes - Type.GetType looks for assemblies in the "normal" places - it
                    doesn't know where to find "other" assemblies loaded from elsewhere on
                    the disk. I'm slightly surprised that it doesn't look at all the
                    assemblies loaded in the current AppDomain first, but that's life, I
                    guess...

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    • Andrew Ducker

                      #11
                      Re: Reflection and Type.GetType

                      Well, at least that means it can't be done, so I can stop trying.

                      I've found an alternative to GetType that should work in this case:

                      foreach(Assembl y assembly in AppDomain.Curre ntDomain.GetAss emblies())
                      {
                      Type t2 = assembly.GetTyp e(TypeName);
                      if (t2 != null)
                      MessageBox.Show (t2.AssemblyQua lifiedName);
                      }

                      So I'll pass that onto the framework people and see if they can
                      incorporate it.

                      Andy D

                      Comment

                      • Guest's Avatar

                        #12
                        Re: Reflection and Type.GetType

                        Well, I dont know - whether how good this is & applicable ?

                        How about AppDomain.Curre ntDomain.Create InstanceFromAnd Unwrap ?
                        (this will load assembly from filesystem & create an instance of the type that you specify)

                        This will get you an instance of the class & you can call .GetType() on it

                        Does this help ?

                        Kalpesh

                        Comment

                        • Truong Hong Thi

                          #13
                          Re: Reflection and Type.GetType

                          >Any ideas why?
                          Type.GetType expects a typeName parameter which, in it full form,
                          includes all the necessary info where to locate the type. Thus, when
                          you omit the assembly spec in typeName, the method makes use of
                          "defaults", and searches the calling assembly, then mscorlib.dll. The
                          reason it does not search other loaded assemblies is that it would
                          violate that parameter's semantics.

                          Thi
                          Thoughts and ideas about software development


                          Comment

                          • Andrew Ducker

                            #14
                            Re: Reflection and Type.GetType

                            However, with dynamically loaded Types, it can't find them, no matter
                            what you pass in as the TypeName (see my example code a few entries
                            up).

                            Andy D

                            Comment

                            • Truong Hong Thi

                              #15
                              Re: Reflection and Type.GetType

                              Hi Andy,

                              It does not matter whether you loaded the assembly or not. Type.GetType
                              uses some specific rules to look for the assembly (these rules are in
                              ..NET documentation, search google for ""How the Runtime Locates
                              Assemblies"").

                              Thi - http://thith.blogspot.com

                              Comment

                              Working...