Type.GetType(String) not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gugale at Lincoln

    Type.GetType(String) not working

    In my code Type.GetType(te xt) works when text="System.IO .File". However, it
    doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
    for classes in System.dll assembly. Is there anyway I can make it work for
    classes in System.Windows. Forms.dll assembly?

    Thanks
    SG


  • code4life
    New Member
    • Apr 2006
    • 7

    #2
    Actually you need to pass the fully qualified assembly name. The following example will reveal that more than just "System.Windows .Forms.Button" is required:

    // I'm assuming this will go into a winform project, since you're referring to a button... you can call this method from the form you copy it over to.
    public static void TypeTest()
    {
    //the actual string will be "System.Windows .Forms.Button, System.Windows. Forms, Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9" -- when you debug the code, this will become evident...
    string fullyqualifiedn ame = new Button().GetTyp e().AssemblyQua lifiedName;
    MessageBox.Show (fullyqualified name);
    MessageBox.Show (System.Type.Ge tType(fullyqual ifiedname).Name );
    }

    Originally posted by Gugale at Lincoln
    In my code Type.GetType(te xt) works when text="System.IO .File". However, it
    doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
    for classes in System.dll assembly. Is there anyway I can make it work for
    classes in System.Windows. Forms.dll assembly?

    Thanks
    SG

    Comment

    • Barry Kelly

      #3
      Re: Type.GetType(St ring) not working

      "Gugale at Lincoln" <phaltu0-0@yahoo.com> wrote:
      [color=blue]
      > In my code Type.GetType(te xt) works when text="System.IO .File". However, it
      > doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
      > for classes in System.dll assembly. Is there anyway I can make it work for
      > classes in System.Windows. Forms.dll assembly?[/color]

      I'm not sure why it won't work trivially for Button. Perhaps it is to do
      with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
      I'm not sure.

      You can however still load the button class as long as you fully qualify
      the type:

      | Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms, Culture=neutral , Version=2.0.0.0 , PublicKeyToken= b77a5c561934e08 9")

      -- Barry

      Comment

      • Mattias Sjögren

        #4
        Re: Type.GetType(St ring) not working

        >I'm not sure why it won't work trivially for Button. Perhaps it is to do[color=blue]
        >with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
        >I'm not sure.[/color]

        No it's because when you don't specify the assembly name, Type.GetType
        only looks in mscorlib.dll and the calling assembly. That works for
        System.IO.File but not something in the Winforms assembly.


        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]

          #5
          Re: Type.GetType(St ring) not working

          Barry Kelly <barry.j.kelly@ gmail.com> wrote:[color=blue]
          > "Gugale at Lincoln" <phaltu0-0@yahoo.com> wrote:
          >[color=green]
          > > In my code Type.GetType(te xt) works when text="System.IO .File". However, it
          > > doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
          > > for classes in System.dll assembly. Is there anyway I can make it work for
          > > classes in System.Windows. Forms.dll assembly?[/color]
          >
          > I'm not sure why it won't work trivially for Button. Perhaps it is to do
          > with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
          > I'm not sure.[/color]

          From the docs of Type.GetType(st ring):

          <quote>
          If typeName includes only the name of the Type, this method searches in
          the calling object's assembly, then in the mscorlib.dll assembly. If
          typeName is fully qualified with the partial or complete assembly name,
          this method searches in the specified assembly.
          </quote>

          (The OP is mistaken about types from System.dll - System.IO.File, for
          instance, is in mscorlib.dll. Trying a type which is genuinely in
          System.dll, such as System.IO.FileS ystemWatcher, returns null as
          expected given the above docs.)

          --
          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

          • Barry Kelly

            #6
            Re: Type.GetType(St ring) not working

            Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote:
            [color=blue][color=green]
            > >I'm not sure why it won't work trivially for Button. Perhaps it is to do
            > >with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
            > >I'm not sure.[/color]
            >
            > No it's because when you don't specify the assembly name, Type.GetType
            > only looks in mscorlib.dll and the calling assembly. That works for
            > System.IO.File but not something in the Winforms assembly.[/color]

            At first I thought it was just what you mentioned, that the assembly name
            is missing. But it's more than that.

            If you try it with just the class name and assembly name and without the
            Version, Culture and PublicKeyToken fields it still doesn't work. You need
            all fields for it to work.

            -- Barry

            Comment

            • Barry Kelly

              #7
              Re: Type.GetType(St ring) not working

              Jon Skeet [C# MVP] <skeet@pobox.co m> wrote:
              [color=blue]
              > From the docs of Type.GetType(st ring):
              >
              > <quote>
              > If typeName includes only the name of the Type, this method searches in
              > the calling object's assembly, then in the mscorlib.dll assembly. If
              > typeName is fully qualified with the partial or complete assembly name,
              > this method searches in the specified assembly.
              > </quote>[/color]

              If you try

              | Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms")

              you'll find it returns null, at least when you have both .NET 1.1 and 2.0
              installed (I'm guessing). I tried before posting.

              -- Barry

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Type.GetType(St ring) not working

                Barry Kelly <barry.j.kelly@ gmail.com> wrote:[color=blue][color=green]
                > > <quote>
                > > If typeName includes only the name of the Type, this method searches in
                > > the calling object's assembly, then in the mscorlib.dll assembly. If
                > > typeName is fully qualified with the partial or complete assembly name,
                > > this method searches in the specified assembly.
                > > </quote>[/color]
                >
                > If you try
                >
                > | Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms")
                >
                > you'll find it returns null, at least when you have both .NET 1.1 and 2.0
                > installed (I'm guessing). I tried before posting.[/color]

                I don't think that counts as "enough" of the assembly name - I think
                you need to give at least some of the version number. Certainly if you
                give the *complete* assembly name (with full version number) it will
                work. There's more to an "assembly name" than the file name. It's not
                terribly well documented, unfortunately.

                --
                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

                • Gugale at Lincoln

                  #9
                  Re: Type.GetType(St ring) not working

                  Thanks Barry!

                  In case if anyone is wondering, as I did, you can get version number and
                  public key using[color=blue]
                  >gacutil /l assemblyname[/color]
                  from Visual Studio .Net Command Prompt

                  "Barry Kelly" <barry.j.kelly@ gmail.com> wrote in message
                  news:4t60525u5s 396jc4jovblngut pj2ksqvei@4ax.c om...[color=blue]
                  > "Gugale at Lincoln" <phaltu0-0@yahoo.com> wrote:
                  >[color=green]
                  >> In my code Type.GetType(te xt) works when text="System.IO .File". However,
                  >> it
                  >> doesn't work when text="System.Wi ndows.Forms.But ton". In general it works
                  >> for classes in System.dll assembly. Is there anyway I can make it work
                  >> for
                  >> classes in System.Windows. Forms.dll assembly?[/color]
                  >
                  > I'm not sure why it won't work trivially for Button. Perhaps it is to do
                  > with multiple System.Windows. Forms assemblies, one each for 1.1 and 2.0?
                  > I'm not sure.
                  >
                  > You can however still load the button class as long as you fully qualify
                  > the type:
                  >
                  > | Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms,
                  > Culture=neutral , Version=2.0.0.0 , PublicKeyToken= b77a5c561934e08 9")
                  >
                  > -- Barry[/color]


                  Comment

                  • Barry Kelly

                    #10
                    Re: Type.GetType(St ring) not working

                    Jon Skeet [C# MVP] <skeet@pobox.co m> wrote:
                    [color=blue]
                    > Barry Kelly <barry.j.kelly@ gmail.com> wrote:[color=green]
                    > > If you try
                    > >
                    > > | Type.GetType("S ystem.Windows.F orms.Button, System.Windows. Forms")
                    > >
                    > > you'll find it returns null, at least when you have both .NET 1.1 and 2.0
                    > > installed (I'm guessing). I tried before posting.[/color]
                    >
                    > I don't think that counts as "enough" of the assembly name - I think
                    > you need to give at least some of the version number. Certainly if you
                    > give the *complete* assembly name (with full version number) it will
                    > work. There's more to an "assembly name" than the file name. It's not
                    > terribly well documented, unfortunately.[/color]

                    The version number isn't enough. You also need *both* the Culture *and*
                    PublicKeyToken. I tried pretty much every combination.

                    I've used this method myself for extensibility scenarios, and all the
                    times I've used it I've only needed the type name and assembly name - I've
                    never seen the behaviour that System.Windows. Forms.Button manifests.

                    Odd. <g>

                    -- Barry

                    Comment

                    Working...