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 9095567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:38:10+00:00 2026-06-16T23:38:10+00:00

I have some code that uses a MethodInfo of a generic method found on

  • 0

I have some code that uses a MethodInfo of a generic method found on a generated type. To avoid some reflection, I have the code use the

ldtoken Method
ldtoken Type
call GetMethodFromHandle(RuntimeMethodHandle,RunTimeTypeHandle)

Pattern to generate the MethodInfos at compile time.

However, if the methodInfo belongs to a generic type and itself is a generic method things get screwy.
Here is some code that simply generates a GM that emits an open version of its methodInfo.
If I call it to retrieve the method than try to close it over a specific type I get a perplexing exception::

System.Reflection.MethodInfo GM[M]() is not a GenericMethodDefinition. MakeGenericMethod may only be called on a method for which MethodBase.IsGenericMethodDefinition is true.

Here is the relevant code::

var aBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.RunAndSave);
var mBuilder = aBuilder.DefineDynamicModule(aBuilder.GetName().Name, true);
var typeBuilder = mBuilder.DefineType("NameSpace.Generic`1",TypeAttributes.AutoClass | TypeAttributes.Sealed | TypeAttributes.Public,typeof(object));
var TypeGenerics = typeBuilder.DefineGenericParameters(new[] { "T" });
var methodBuilder = typeBuilder.DefineMethod("GM", MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig);
var methodGenerics = methodBuilder.DefineGenericParameters(new[] { "M" });
methodBuilder.SetSignature(typeof(MethodInfo), null, null, Type.EmptyTypes, null, null);
var ilgenerator = methodBuilder.GetILGenerator();
var typeBuilderClosedOverT = typeBuilder.MakeGenericType(TypeGenerics);
ilgenerator.Emit(OpCodes.Ldtoken, methodBuilder);
ilgenerator.Emit(OpCodes.Ldtoken, typeBuilderClosedOverT);
ilgenerator.Emit(OpCodes.Call, 
    typeof(MethodBase).GetMethod(
        "GetMethodFromHandle", 
        BindingFlags.Public | BindingFlags.Static,
        null,
        new[] { typeof(RuntimeMethodHandle), typeof(RuntimeTypeHandle) },
        null
    )
);
ilgenerator.Emit(OpCodes.Castclass,typeof(MethodInfo));
ilgenerator.Emit(OpCodes.Ret);
var bakedType = typeBuilder.CreateType();
var methodInfo = bakedType.MakeGenericType(typeof(int)).GetMethod("GM").MakeGenericMethod(typeof(bool)).Invoke(null, null) as MethodInfo;
var methodInfoClosedOverBool = methodInfo.MakeGenericMethod(typeof(bool));

It seems the only time my code screws up is if it’s a genericmethod on a non-generic type. If the code is rewritten so that its about a normal method on a normal type, or a generic method on a normal type, or a normal method on a generic type it all works. It’s only the combination of both that causes errors. Am I doing something wrong?

I submitted a bug about this issue:
https://connect.microsoft.com/VisualStudio/feedback/details/775989/clr-cannot-emit-a-token-for-an-open-generic-method-on-a-generic-type

  • 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-16T23:38:12+00:00Added an answer on June 16, 2026 at 11:38 pm

    Looks like a CLR issue to me, because the same thing happens if you write the IL by hand and use ilasm. That is, given a generic class G and a nongeneric class N, each with a generic method M, then trying to get the generic method definition from the non-generic class works:

    ldtoken    method void class N::M<[1]>()
    ldtoken    class N<!T>
    call       class [mscorlib]System.Reflection.MethodBase [mscorlib]
                 System.Reflection.MethodBase::GetMethodFromHandle(
                    valuetype [mscorlib]System.RuntimeMethodHandle,
                    valuetype [mscorlib]System.RuntimeTypeHandle)
    castclass  [mscorlib]System.Reflection.MethodInfo
    ret
    

    but the MethodInfo returned from the generic class is not a generic method definition (but it almost is; it’s D.MakeGenericMethod(D.GetGenericArguments()) where D is the method definition you want):

    ldtoken    method void class G`1<!T>::M<[1]>()
    ldtoken    class G`1<!T>
    call       class [mscorlib]System.Reflection.MethodBase [mscorlib]
                 System.Reflection.MethodBase::GetMethodFromHandle(
                    valuetype [mscorlib]System.RuntimeMethodHandle,
                    valuetype [mscorlib]System.RuntimeTypeHandle)
    castclass  [mscorlib]System.Reflection.MethodInfo
    ret
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some code that uses type-punning to avoid having to call a member
I have found some code that uses the virtual keyword for functions, like: package
I have some code that uses the SQL Server 2005 SMO objects to backup
I have some code that uses Open XML to open up a .docx file,
I have some code that uses ODP.Net using (OracleConnection connection = new OracleConnection(connectionString)) {
I have some code here that uses bitsets to store many 1 bit values
I have some old code that uses qsort to sort an MFC CArray of
We have some code kicking around that uses this old internal Sun package for
So I have a chunk of code that declares some classes, creates data, uses
I have some code that uses a fair amount of GCD code. I need

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.