none
Reflection bug in .NET? GetType( ) invoked through a MethodInfo returns wrong value. RRS feed

  • Question

  • Hi there

    Can anyone explain these results (see code below)?

    The console should output "System.RuntimeType" but it outputs "ReflectionBug.MyClass".

    Essentially, I get a MethodInfo for the "GetType" method of the class "MyClass". I invoke ht method and get the returned value. The expected return value should be a System.Type but it actually returns the instance on which the GetType() method was invoked.

    If anyone can explain it, please let know.

    Cheers

     

    using System;
    using System.Reflection;
     
    namespace ReflectionBug
    {
     public class MyClass
     {
     }
     
     class Program
     {
      static void Main(string[] args)
      {
       object myObject = new MyClass();
       Type type = myObject.GetType();
       MethodInfo getTypeMethod = type.GetMethod("GetType", new Type[0]);
       object ret = getTypeMethod.Invoke(myObject, new object[0]);
       // expected result: System.RuntimeType
       // result obtained: ReflectionBug.MyClass ??
       Console.WriteLine(ret.ToString());
      }
     }
    }
    

     

     

    • Edited by Mike Boissé Wednesday, June 9, 2010 10:44 PM More descriptive title
    Wednesday, June 9, 2010 10:40 PM

Answers

  • This is working correctly.  The actual type of ret is System.RuntimeType.  You have called its ToString method.  RuntimeType.ToString() is implemented to return the name of the type represented by the RuntimeType.  The following shows how to get it to print System.RuntimeType.

    Console.WriteLine(ret.GetType().ToString()); // Prints System.RuntimeType

    Note that System.RuntimeType is internal.  It inherits from Type.  The following shows that ret is a Type.

    Console.WriteLine(ret is Type); // Prints true
    Console.WriteLine(ret is ReflectionBug.MyClass); // Prints false

     

     

    • Marked as answer by SamAgain Thursday, June 10, 2010 3:41 AM
    Wednesday, June 9, 2010 11:39 PM

All replies

  • This is working correctly.  The actual type of ret is System.RuntimeType.  You have called its ToString method.  RuntimeType.ToString() is implemented to return the name of the type represented by the RuntimeType.  The following shows how to get it to print System.RuntimeType.

    Console.WriteLine(ret.GetType().ToString()); // Prints System.RuntimeType

    Note that System.RuntimeType is internal.  It inherits from Type.  The following shows that ret is a Type.

    Console.WriteLine(ret is Type); // Prints true
    Console.WriteLine(ret is ReflectionBug.MyClass); // Prints false

     

     

    • Marked as answer by SamAgain Thursday, June 10, 2010 3:41 AM
    Wednesday, June 9, 2010 11:39 PM
  • Of course.

    Thanks a lot.

    Thursday, June 10, 2010 12:31 AM
  • Hi,

       Hope the code below doesn't confuse  you.

    using System;
    using System.Reflection;
    
    namespace ReflectionBug
    {
      public class MyClass
      {
      }
    
      class Program
      {
        static void Main(string[] args)
        {
          object myObject = new MyClass();
          Type type = myObject.GetType();
          Console.WriteLine("type.UnderlyingSystemType : {0}", type.UnderlyingSystemType);
          Console.WriteLine("type.UnderlyingSystemType.GetType() : {0}", type.UnderlyingSystemType.GetType());
          Console.WriteLine("type : {0}", type);
          Console.WriteLine("type.GetType() : {0}", type.GetType());
        }
      }
    }
    

       Yet another related discussion: http://doogalbellend.blogspot.com/2006/06/what-is-systemruntimetype.html


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Edited by SamAgain Thursday, June 10, 2010 3:52 AM refine
    Thursday, June 10, 2010 3:43 AM