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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:40:44+00:00 2026-05-31T23:40:44+00:00

Not so long ago I started to learn to use System.Reflection.Emit namespace. I’m now

  • 0

Not so long ago I started to learn to use System.Reflection.Emit namespace. I’m now trying to translate this code to use of ILGenerator:

MyClass c = new MyClass("MyClass");
c.Do(":D");

For this piece of code I have three questions: how to create object? how to call contructor and how to call method of class? Please help.

  • 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-31T23:40:45+00:00Added an answer on May 31, 2026 at 11:40 pm

    Here’s a complete example that shows the necessary IL code.

    You can test this in LINQPad:

    void Main()
    {
        // Manual test first
        MyClass c = new MyClass("MyClass");
        c.Do(":D");
    
        var method = new DynamicMethod("dummy", null, Type.EmptyTypes);
        var il = method.GetILGenerator();
    
        // <stack> = new MyClass("MyClass");
        il.Emit(OpCodes.Ldstr, "MyClass");
        il.Emit(OpCodes.Newobj, typeof(MyClass).GetConstructor(new[] { typeof(string) }));
    
        // <stack>.Do(":D");
        il.Emit(OpCodes.Ldstr, ":D");
        il.Emit(OpCodes.Call, typeof(MyClass).GetMethod("Do", new[] { typeof(string) }));
    
        // return;
        il.Emit(OpCodes.Ret);
    
        var action = (Action)method.CreateDelegate(typeof(Action));
        action();
    }
    
    public class MyClass
    {
        public MyClass(string text)
        {
            Console.WriteLine("MyClass(" + text + ")");
        }
    
        public void Do(string text)
        {
            Console.WriteLine("Do(" + text + ")");
        }
    }
    

    Output:

    MyClass(MyClass)
    Do(:D)
    MyClass(MyClass)
    Do(:D)
    

    Incidentally, you can use LINQPad to get hold of the IL code for a particular example. Let me cut out the IL-part of the above example, like this (I removed the class as well, it’s the same class):

    void Main()
    {
        MyClass c = new MyClass("MyClass");
        c.Do(":D");
    }
    

    By executing this code, and then using the IL tab of the output, you can see the generated code:

    LINQPad IL Output

    The two instructions stloc.0 and ldloc.0 is the variable in the code.

    The IL I emitted first is akin to this piece of code:

    new MyClass("MyClass").Do(":D");
    

    ie. no variable, just temporary storage on the stack, and indeed:

    LINQPad IL output #2

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

Sidebar

Related Questions

I discovered Sinatra not long ago and started to translate a webform I made
I started assembly (nasm) programming not too long ago. Now I made a C
Not a long ago I started to learn Mathematica - i.e. I'm novice. Usually
This enum used to exist not too long ago. I am upgrading to Castle
Not long ago I started programming Python with Django. Sometimes I get strange errors
Not too long ago I started writing a docking library for WPF (similar to
I have started in web development not long time ago. I know some stuff
I started this project a long long time ago when I was just beginning
Started using PDO prepared statements not too long ago, and, as i understand, it
I Google'd not long ago and came across this solution to loop out information

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.