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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:38:34+00:00 2026-05-31T01:38:34+00:00

I am creating a dynamic type which has a method that I’d like to

  • 0

I am creating a dynamic type which has a method that I’d like to return an object. I am failing to understand how to achieve this. Here’s what I have so far:

// .. stuff to create type builder



MethodBuilder builder =
                typeBuilder.DefineMethod(
                    method.Name,
                    MethodAttributes.Virtual | MethodAttributes.Public,
                    method.CallingConvention,
                    method.ReturnType,
                    typeArray1);
            builder.InitLocals = true;
ILGenerator gen = builder.GetILGenerator();
Object myObjectIdLikeToReturn = someMethodCall();
//gen.??(??????????) // here's where I'm lost
gen.Emit(OpCodes.Ldloc_0);
gen.Emit(OpCodes.Ret);

I believe, if I am reading the msdn correct, I need to get the reference of myObjectIdLikeToReturn on the stack–however I’ve not had luck. Can anyone point me in the right direction?

Edit: To make it more clear. I’m attempting to write the equivalent in IL:

public virtual Object MyNewMethod() {
   return myObjectIdLikeToReturn;
}
  • 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-31T01:38:36+00:00Added an answer on May 31, 2026 at 1:38 am

    You have this line:

    Object myObjectIdLikeToReturn = someMethodCall();
    

    You say you “want to get the reference to myObjectIdLikeToReturn onto the stack.” But that is impossible. I think you are confusing the timing of when these things are happening. The IL generation code is analogous to compiling — everything needs to be known statically. Whereas myObjectIdLikeToReturn is a variable that happens to exist while you are generating the code, but would not have any meaning to the class that is actually generated. You can’t “bake in” a reference to some arbitrary runtime object, the idea simply doesn’t make any sense.

    Your only solution is to somehow make your IL perform the invocation to someMethodCall. But without knowing more about where that method lives and its connection to your generated type, it’s hard to elaborate on precisely how you’ll accomplish that.

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

Sidebar

Related Questions

I'm creating dynamic templated XAML designs that I would like to convert to PNG
which method do you prefer for creating dynamic sql queries? formating or streaming? Is
I have the follow method that returns a dynamic object representing an IEnumerable<'a> ('a=anonymous
I'm creating some object types dynamically using type function. Ex return type('DynamicType', (object,), dict)
I need to integrate the functionality of creating dynamic pages (like google sites) to
I'm creating an dynamic image, which creates headers on my page using PHPs GD-library.
I am creating a dynamic cursor and I would like to loop over the
So I was thinking about creating a dynamic sql question, meaning that i want
I'm creating a dynamic gridview function which will bind different tables from DB into
I'm creating a dynamic function to create an object at runtime given an object[]

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.