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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:33:32+00:00 2026-05-31T02:33:32+00:00

This is an extension to the solutions offered here . I’ve created a static

  • 0

This is an extension to the solutions offered here. I’ve created a static method that returns me an object. My goal, is the write a dynamic method for a type I define at runtime to return me the object that this static method is returning. My code thus far:

 // type builder and other prep stuff removed for sake of space and reading

private void EmitReferenceMethodBody(Type returnType)
{
    MethodBuilder builder =
    typeBuilder.DefineMethod(
                    method.Name,
                    MethodAttributes.Virtual | MethodAttributes.Public,
                    method.CallingConvention,
                    method.ReturnType,
                    typeArray1);
    builder.InitLocals = true;
    ILGenerator gen = builder.GetILGenerator();
    MethodInfo getStoredObject = typeof(ObjectStore).GetMethod("GetStoredObject",                  BindingFlags.Public | BindingFlags.Static);        
    MethodInfo getTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle");            

    gen.Emit(OpCodes.Ldtoken, returnType);
    gen.Emit(OpCodes.Call, getTypeFromHandle);
    gen.Emit(OpCodes.Call, getStoredObject);
    gen.Emit(OpCodes.Ret);   
}

The updated code now calls the method but appears to be passing the type of the dynamically created type rather then the variable returnType.

  • 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-05-31T02:33:33+00:00Added an answer on May 31, 2026 at 2:33 am

    At least one problem is that you are pushing the “this” reference (OpCodes.Ldarg_0) onto the stack even though it is never popped (since you are invoking a static method). I’d try removing that line and see if it behaves better.

    Another problem is that you are passing in new Type[] { returnType } to the EmitCall method. That is intended for optional arguments (params) and I suspect your method actually doesn’t have any parameters. Therefore, you should remove that argument as well.

    Edit:

    Based on comments, you are trying to pass in a System.Type object known statically to a method you are invoking dynamically. This is possible, but you need to jump through a couple of hoops.

    1. Get a reference to the MethodInfo for the method Type.GetTypeFromHandle:

      MethodInfo getTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle");
      
    2. Use the following lines of IL to push your returnType onto the stack:

      gen.Emit(OpCodes.Ldtoken, returnType);
      gen.Emit(OpCodes.Call, getTypeFromHandle);
      

    To sum, your code should look like this:

    MethodInfo getTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle");
    gen.Emit(OpCodes.Ldtoken, returnType);
    gen.Emit(OpCodes.Call, getTypeFromHandle);
    gen.EmitCall(OpCodes.Call, getStoredObject);                
    gen.Emit(OpCodes.Ret);
    

    The transitional stack behavior of this code is:

    1. Push the RuntimeTypeHandle corresponding to the specified Type reference onto the stack by using Opcodes.Ldtoken.

    2. Invoke getTypeFromHandle which pops the type handle off the stack, and pushes the actual System.Type onto the stack.

    3. Call your static method, which will pop the Type argument off of the stack and push the return value of your own method onto the stack.

    4. Return from the method.

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

Sidebar

Related Questions

I try to build extension method for IQuerable like this: public static IQueryable<T> FilterByString<T>(this
The CollectionViewSource.GetDefaultView() method is not in Silverlight 3. In WPF I have this extension
For those of you that use Mercurial with the MQ extension: This is the
This is an extension of a question I previously asked here . Long story
I was using this extension method to transform very large xml files with an
I want to write an extension for Google chrome that detects snippets of source
I'm writing a Chrome extension, and want to write one JS file, that provides
I would like to write an extension method for a generic type, with additional
I have an HtmlHelper extension that currently returns a string using a string builder
I have an extension method that needs to return an HtmlString. The method has

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.