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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:12:28+00:00 2026-05-16T12:12:28+00:00

I’m creating an object at runtime using reflection emit. I successfully created the fields,

  • 0

I’m creating an object at runtime using reflection emit. I successfully created the fields, properties and get set methods.
Now I want to add a method. For the sake of simplicity let’s say the method just returns a random number. How do I define the method body?

EDIT:

Yes, I’ve been looking at the msdn documentation along with other references and I’m starting to get my head wrapped around this stuff.
I see how the example above is adding and/or multplying, but what if my method is doing other stuff. How do I define that “stuff”
Suppose I was generating the class below dynamically, how would I create the body of GetDetails() method?

class TestClass
{
    public string Name  { get; set; }
    public int Size  { get; set; }

    public TestClass()
    {
    }

    public TestClass(string Name, int Size)
    {
        this.Name = Name;
        this.Size = Size;
    }

    public string GetDetails()
    {
        string Details = "Name = " + this.Name + ", Size = " + this.Size.ToString();
        return Details;
    }
}
  • 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-16T12:12:29+00:00Added an answer on May 16, 2026 at 12:12 pm

    You use a MethodBuilder to define methods. To define the method body, you call GetILGenerator() to get an ILGenerator, and then call the Emit methods to emit individual IL instructions. There is an example on the MSDN documentation for MethodBuilder, and you can find other examples of how to use reflection emit on the Using Reflection Emit page:

    public static void AddMethodDynamically(TypeBuilder myTypeBld,
                                        string mthdName,
                                        Type[] mthdParams,
                                        Type returnType,
                                        string mthdAction)
    {
        MethodBuilder myMthdBld = myTypeBld.DefineMethod(
                                                mthdName,
                                                MethodAttributes.Public |
                                                MethodAttributes.Static,
                                                returnType,
                                                mthdParams);
        ILGenerator ILout = myMthdBld.GetILGenerator();
        int numParams = mthdParams.Length;
        for (byte x = 0; x < numParams; x++)
        {
            ILout.Emit(OpCodes.Ldarg_S, x);
        }
        if (numParams > 1)
        {
            for (int y = 0; y < (numParams - 1); y++)
            {
                switch (mthdAction)
                {
                    case "A": ILout.Emit(OpCodes.Add);
                        break;
                    case "M": ILout.Emit(OpCodes.Mul);
                        break;
                    default: ILout.Emit(OpCodes.Add);
                        break;
                }
            }
        }
        ILout.Emit(OpCodes.Ret);
    }
    

    It sounds like you’re looking for resources on writing MSIL. One important resource is the OpCodes class, which has a member for every IL instruction. The documentation describes how each instruction works. Another important resource is either Ildasm or Reflector. These will let you see the IL for compiled code, which will help you understand what IL you want to write. Running your GetDetailsMethod through Reflector and setting the language to IL yields:

    .method public hidebysig instance string GetDetails() cil managed
    {
        .maxstack 4
        .locals init (
            [0] string Details,
            [1] string CS$1$0000,
            [2] int32 CS$0$0001)
        L_0000: nop 
        L_0001: ldstr "Name = "
        L_0006: ldarg.0 
        L_0007: call instance string ConsoleApplication1.TestClass::get_Name()
        L_000c: ldstr ", Size = "
        L_0011: ldarg.0 
        L_0012: call instance int32 ConsoleApplication1.TestClass::get_Size()
        L_0017: stloc.2 
        L_0018: ldloca.s CS$0$0001
        L_001a: call instance string [mscorlib]System.Int32::ToString()
        L_001f: call string [mscorlib]System.String::Concat(string, string, string, string)
        L_0024: stloc.0 
        L_0025: ldloc.0 
        L_0026: stloc.1 
        L_0027: br.s L_0029
        L_0029: ldloc.1 
        L_002a: ret 
    }
    

    To generate a method like that dynamically, you will need to call ILGenerator.Emit for each instruction:

    ilGen.Emit(OpCodes.Nop);
    ilGen.Emit(OpCodes.Ldstr, "Name = ");
    ilGen.Emit(OpCodes.Ldarg_0);
    ilGen.Emit(OpCodes.Call, nameProperty.GetGetMethod());
    // etc..
    

    You may also want to look for introductions to MSIL, such as this one: Introduction to IL Assembly Language.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.