Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:35:26+00:00 2026-06-13T01:35:26+00:00

Given a MethodInfo instance that identifies an open generic method of a non-generic class,

  • 0

Given a MethodInfo instance that identifies an open generic method of a non-generic class, consider the following pseudocode:

class Foo { void FooMethod<T>() {} }

public static void PrintMethodInfo(RuntimeMethodHandle methodHandle)
{
    var mi = (MethodInfo) MethodBase.GetMethodFromHandle(methodHandle);
    Console.WriteLine("Method: "+mi.ToString());
}

var methodInfo = typeof(Foo).GetMethod("FooMethod");

Generate a method “void GeneratedMethod<T>()” containing this code in the body:

IL.Emit(OpCodes.Ldtoken, methodInfo);
IL.Emit(OpCodes.Call, methodInfoPrintMethodInfo);

Calling GeneratedMethod<int>(), the output on .Net 3.5 will be:

Method: System.Object Method[Int32]()

While on .Net 4.0 it will be:

Method: System.Object Method[T]()

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<> instantiated with the type argument given when GeneratedMethod<T> was called.

In .Net 4.0, however, the ldtoken will contain metadata that identifies the open generic type.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:35:27+00:00Added an answer on June 13, 2026 at 1:35 am

    When you disassemble the generated code, you can see that in .NET 3.5, the ldtoken instruction in connection to the open generic method is emitted as follows:

    .method public static void  GeneratedMethod<T>() cil managed
    {
      // Code size       11 (0xb)
      .maxstack  1
      IL_0000:  ldtoken    method instance void [ConsoleApplication16]ConsoleApplication16.Program/Foo::FooMethod<!!0>()
      IL_0005:  call       void [ConsoleApplication16]ConsoleApplication16.Program::PrintMethodInfo(valuetype [mscorlib]System.RuntimeMethodHandle)
      IL_000a:  ret
    } // end of method TestType::GeneratedMethod
    

    The syntax !!0 is a reference to the type parameter of the surrounding method (GeneratedMethod), so the Foo method is loaded instantiated with the T belonging to GeneratedMethod<T>. (In fact, this is the same IL as would be emitted for IL.Emit (OpCodes.Ldtoken, methodInfo.MakeGenericMethod (<typeParameterOfGeneratedMethod>)).) This !!0 reference is emitted even when GeneratedMethod is not generic at all – the resulting program is then no longer verifiable (and, when executed, results in a BadImageFormatException).

    This is clearly a bug, and in .NET 4, this seems to be fixed, as the (disassembled) emitted code now looks like this:

    .method public static void  GeneratedMethod<T>() cil managed
    {
      // Code size       11 (0xb)
      .maxstack  1
      IL_0000:  ldtoken    method instance void [ConsoleApplication16]ConsoleApplication16.Program/Foo::FooMethod<[1]>()
      IL_0005:  call       void [ConsoleApplication16]ConsoleApplication16.Program::PrintMethodInfo(valuetype [mscorlib]System.RuntimeMethodHandle)
      IL_000a:  ret
    } // end of method TestType::GeneratedMethod
    

    As you can see, the signature now refers to the uninstantiated FooMethod (in IL assembly, this is denoted as FooMethod[1]).

    So yes, this looks like a bug in .NET 3.5 that was fixed with .NET 4. However, it doesn’t seem to be a change in the semantics of ldtoken; it was just that Reflection.Emit didn’t emit references to open generic methods correctly. I suspect it’s also connected to the fact that IL assembler didn’t even have a syntax to denote open generic methods in the past.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a method that returns a methodinfo class from a
given public Class Example { public static void Foo< T>(int ID){} public static void
I am given: A class T which implements properties that are decorated with Attributes
I have a MethodInfo object that represents an explicitly-implemented interface method, as follows. MethodInfo
I have a System.Reflection.MethodInfo and would like to have a method that creates a
Given a MethodInfo object, how can I get the Type object for the class
Say you've got a MethodInfo, or any other sub-class of MemberInfo for that matter,
I have the following method: public MethodInfo FancyGetMethodInfo (object obj, string methodName, Type[] methodSignature)
Is there any possibility that GetPropInfo returns nil even if the given class is
Given that the web application doesn't have su privileges, I'd like to execute a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.