Note that there are some explanatory texts on larger screens.

plurals
  1. POChanged behaviour of LdToken for generic methods in .Net 4?
    text
    copied!<p>Given a MethodInfo instance that identifies an open generic method of a non-generic class, consider the following pseudocode:</p> <pre><code>class Foo { void FooMethod&lt;T&gt;() {} } public static void PrintMethodInfo(RuntimeMethodHandle methodHandle) { var mi = (MethodInfo) MethodBase.GetMethodFromHandle(methodHandle); Console.WriteLine("Method: "+mi.ToString()); } var methodInfo = typeof(Foo).GetMethod("FooMethod"); </code></pre> <p>Generate a method "void GeneratedMethod&lt;T&gt;()" containing this code in the body:</p> <pre><code>IL.Emit(OpCodes.Ldtoken, methodInfo); IL.Emit(OpCodes.Call, methodInfoPrintMethodInfo); </code></pre> <p>Calling GeneratedMethod&lt;int&gt;(), the output on .Net 3.5 will be:</p> <pre><code>Method: System.Object Method[Int32]() </code></pre> <p>While on .Net 4.0 it will be:</p> <pre><code>Method: System.Object Method[T]() </code></pre> <p>So it appears that in .Net 2.0/3.5, the generated IL for the ldtoken will contain a metadata token that identifies the generic FooMethod&lt;> instantiated with the type argument given when GeneratedMethod&lt;T&gt; was called.</p> <p>In .Net 4.0, however, the ldtoken will contain metadata that identifies the open generic type.</p> <p>I'm having trouble finding documentation that support what's happening in the .Net 3.5 case (and indeed, it should fail completely if the generated method isn't itself generic) - the .Net 4 behaviour seems more logical. I can't find any documentation of the change either. Is this a bug in the earlier version that has now been fixed?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload