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


hi there

can explain these results (see code below)?

the console should output "system.runtimetype" outputs "reflectionbug.myclass".

essentially, methodinfo "gettype" method of class "myclass". invoke ht method , returned value. expected return value should system.type but returns instance on gettype() method invoked.

if 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());   }  } } 

 

 

this working correctly.  actual type of ret system.runtimetype.  have called tostring method.  runtimetype.tostring() implemented return name of type represented runtimetype.  following shows how print system.runtimetype.

console.writeline(ret.gettype().tostring()); // prints system.runtimetype

note system.runtimetype internal.  inherits type.  following shows ret type.

console.writeline(ret type); // prints true  console.writeline(ret reflectionbug.myclass); // prints false

 

 



.NET Framework  >  Common Language Runtime Internals and Architecture



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

BizTalk Server 2013 Azure VM Log Shipping and HA for hosts

How to send non-standard Content-Type header ?