about the Type type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    about the Type type

    Hello!

    If I have string name = "Mytest";
    and
    Type test1 = Type.GetType(na me);
    and
    Type test2 = name.GetType();
    then the first one will assign null to test1
    but the second test2 will be given the correct type which is System.String

    Now to my question why will test1 be assign null I would assume that
    System.String would be assigned
    because name is of that type

    //Tony


  • Jeroen Mostert

    #2
    Re: about the Type type

    Tony Johansson wrote:
    If I have string name = "Mytest";
    and
    Type test1 = Type.GetType(na me);
    and
    Type test2 = name.GetType();
    then the first one will assign null to test1
    but the second test2 will be given the correct type which is System.String
    >
    Now to my question why will test1 be assign null I would assume that
    System.String would be assigned
    because name is of that type
    >
    "Mytest" is not the name of a type, so Type.GetType() fails.

    Gets a Type object that represents the specified type.


    --
    J.

    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: about the Type type

      Tony Johansson wrote:
      If I have string name = "Mytest";
      and
      Type test1 = Type.GetType(na me);
      and
      Type test2 = name.GetType();
      then the first one will assign null to test1
      but the second test2 will be given the correct type which is System.String
      >
      Now to my question why will test1 be assign null I would assume that
      System.String would be assigned
      because name is of that type


      Parameters

      typeName
      Type: System..::.Stri ng
      The assembly-qualified name of the type to get. See
      AssemblyQualifi edName. If the type is in the currently executing
      assembly or in Mscorlib.dll, it is sufficient to supply the type name
      qualified by its namespace.

      Arne

      Comment

      • Jeff Johnson

        #4
        Re: about the Type type

        "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
        news:klGUk.4215 $U5.29009@newsb .telia.net...
        Hello!
        >
        If I have string name = "Mytest";
        and
        Type test1 = Type.GetType(na me);
        and
        Type test2 = name.GetType();
        then the first one will assign null to test1
        but the second test2 will be given the correct type which is System.String
        >
        Now to my question why will test1 be assign null I would assume that
        System.String would be assigned
        because name is of that type
        It doesn't matter what name's type is; it matters what its CONTENTS are.
        You're calling the overload of GetType() that accepts a string, and
        therefore what the function wants out of that string is, from MSDN:

        ------
        The assembly-qualified name of the type to get. See AssemblyQualifi edName.
        If the type is in the currently executing assembly or in Mscorlib.dll, it is
        sufficient to supply the type name qualified by its namespace.
        ------

        You have not supplied enough information. Try puting the namespace on the
        front of the type.


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: about the Type type

          Tony,

          Have you looked at the documentation of the overload of GetType which
          takes a string? The string that you pass is the name of the class that you
          want to get the type for, not the object that you want to get the type for
          (for that, you just call GetType on the instance of the object).


          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m

          "Tony Johansson" <johansson.ande rsson@telia.com wrote in message
          news:klGUk.4215 $U5.29009@newsb .telia.net...
          Hello!
          >
          If I have string name = "Mytest";
          and
          Type test1 = Type.GetType(na me);
          and
          Type test2 = name.GetType();
          then the first one will assign null to test1
          but the second test2 will be given the correct type which is System.String
          >
          Now to my question why will test1 be assign null I would assume that
          System.String would be assigned
          because name is of that type
          >
          //Tony
          >
          >

          Comment

          • Jeff Johnson

            #6
            Re: about the Type type

            "Jeff Johnson" <i.get@enough.s pamwrote in message
            news:OGWozTcSJH A.1172@TK2MSFTN GP03.phx.gbl...
            >Now to my question why will test1 be assign null I would assume that
            >System.Strin g would be assigned
            >because name is of that type
            Okay, it just occured to me what you're trying to do. C# has a special
            keyword for this:

            Type test1 = typeof(name);


            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: about the Type type

              That's not going to work. You can use typeof only when you use the
              actual name. It's not an alias for the overload of the static GetType
              method. That line will fail to compile.

              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m

              "Jeff Johnson" <i.get@enough.s pamwrote in message
              news:%232x6MYcS JHA.4764@TK2MSF TNGP05.phx.gbl. ..
              "Jeff Johnson" <i.get@enough.s pamwrote in message
              news:OGWozTcSJH A.1172@TK2MSFTN GP03.phx.gbl...
              >
              >>Now to my question why will test1 be assign null I would assume that
              >>System.Stri ng would be assigned
              >>because name is of that type
              >
              Okay, it just occured to me what you're trying to do. C# has a special
              keyword for this:
              >
              Type test1 = typeof(name);
              >

              Comment

              • Jeff Johnson

                #8
                Re: about the Type type

                "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
                message news:5F773B66-39BB-41FD-BB35-5A6D02C68861@mi crosoft.com...
                >Type test1 = typeof(name);
                That's not going to work. You can use typeof only when you use the
                actual name. It's not an alias for the overload of the static GetType
                method. That line will fail to compile.
                Ah, you're right. I use typeof() so infrequently I forgot that I usually
                only use it as typeof(<EnumCla ssName>) and that you supply the real type
                name, not a variable.

                So using <variable>.GetT ype() is really the only way, isn't it?


                Comment

                • Peter Duniho

                  #9
                  Re: about the Type type

                  On Tue, 18 Nov 2008 13:21:20 -0800, Tony Johansson
                  <johansson.ande rsson@telia.com wrote:
                  Hello!
                  >
                  If I have string name = "Mytest";
                  and
                  Type test1 = Type.GetType(na me);
                  and
                  Type test2 = name.GetType();
                  then the first one will assign null to test1
                  but the second test2 will be given the correct type which is
                  System.String
                  >
                  Now to my question why will test1 be assign null I would assume that
                  System.String would be assigned
                  because name is of that type
                  The answer is in the documentation that Jeff posted. The two methods
                  you're looking at have the same name, but they do entirely different
                  things. Thus they return entirely different values. And in the case of
                  Type.GetType(st ring), since you don't have a type named "Mytest" in your
                  assembly, it returns null.

                  Pete

                  Comment

                  • Nicholas Paldino [.NET/C# MVP]

                    #10
                    Re: about the Type type

                    Jeff,

                    Well, assuming the variable is of the type that the OP wants to get,
                    then yes, that is. If all the OP has is the name in a string, then calling
                    the static GetType method is the way to go.

                    --
                    - Nicholas Paldino [.NET/C# MVP]
                    - mvp@spam.guard. caspershouse.co m

                    "Jeff Johnson" <i.get@enough.s pamwrote in message
                    news:%236BPnlcS JHA.5568@TK2MSF TNGP05.phx.gbl. ..
                    "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote
                    in message news:5F773B66-39BB-41FD-BB35-5A6D02C68861@mi crosoft.com...
                    >
                    >>Type test1 = typeof(name);
                    >
                    > That's not going to work. You can use typeof only when you use the
                    >actual name. It's not an alias for the overload of the static GetType
                    >method. That line will fail to compile.
                    >
                    Ah, you're right. I use typeof() so infrequently I forgot that I usually
                    only use it as typeof(<EnumCla ssName>) and that you supply the real type
                    name, not a variable.
                    >
                    So using <variable>.GetT ype() is really the only way, isn't it?
                    >

                    Comment

                    Working...