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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:04:27+00:00 2026-06-17T05:04:27+00:00

Can a Dynamic Method work like a normal method or code, in that it

  • 0

Can a Dynamic Method work like a normal method or code, in that it can access variables where variables can normally be accessed, call methods, and initialize variables (of course in the scope of the method)?

The only examples I’ve seen is where it is passed some parameters and returns some value and does nothing to change any variables outside of it.

I’m talking about the System.Reflection.Emit.DynamicMethod class. I’m having trouble understanding it since one needs to use MSIL which I don’t know much of yet.

  • 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-17T05:04:28+00:00Added an answer on June 17, 2026 at 5:04 am

    Yes. A DynamicMethod can be attached to a class, in which case it can access class-private static fields (and possibly class-private fields if the DynamicMethod is an instance method, but I don’t recall whether that’s a supported scenario). It can also access assembly-internal methods, properties, and types that are internal to the assembly in which the DynamicMethod is created.

    What’s the scenario you’re using dynamic methods for?

    I have some blog articles about dynamic programming, including a couple entries about using the DynamicMethod class on my blog: http://robpaveza.net/tag/dynamic-programming . Specifically, this article talks about how to calculate a file revision proof, and you can see the result implementation here (evidently, I never wrote part 2, but the implementation in BN# that I linked was the result of the analysis).

    Let me walk you through the Compile method:

    1. Type parameterType = typeof(uint).MakeByRefType()

    The final method is going to take out uint parameters; this line obtains a reference to the uint-ref runtime type. The method declaration would look like this if I were to write it in normal C#:

    public static void CheckRevision(out uint a, out uint b, out uint c, out uint s);
    

    38-40. foreach (string formula in formulas) CompileStandardFormula(generator, formula)

    As I mention in my blog post about it, the math that I do is always provided in the form of:

    A=A-S B=B-C C=C+A A=A+B
    

    Where A, B, and C are state variables and S is an input (the next uint value from the file).

    The CompileStandardFormula function emits the IL that computes the logic for one individual operation of the four shown. Recall that the CLR is a stack-based state machine, and math operations occur with the values on the stack being popped and results being pushed. So, for A=A-S, for example, the following IL is what would be emitted:

    ldarg.0  // push &A, which is a reference to the location that actually contains the value of A
    ldarg.0  // push &A
    ldind.u4 // dereference the top-most value on the stack, which puts the actual value of &A ready for operation
    ldarg.3  // push &S
    ldind.u4 // dereference &S
    sub      // subtracts [stack-1] from [stack-2], which effectively is A-S
    stind.u4 // remember the first ldarg.0? That's getting accessed now and the subtraction result is going there
    

    So, at this point, it should be pretty easy to figure out: my DynamicMethod compiles the math operation required to update all state variables for a single pass in the file. After all of the IL is emitted, because we know the state of the stack has nothing on it (more than when the method entered, anyway), we can just throw out a quick ‘ret’ instruction and we’re done.

    Anyway, hope this is helpful.

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

Sidebar

Related Questions

i call my asp.net page method like below code $.ajax({ type: POST, url: MyPage.aspx/GetItems,
Can anyone explain when using an anonymously hosted dynamic method why I get an
The CompareTo() method for my class is dynamic, and can range from a simple
Can Dynamic libraries have global, local and static variables in linux? If yes, then
I work on project that use some dynamic linq query to an entities. i
So I'm developing something that works on dynamic variables using C# 4. And I'm
I add variables with Dynamic from scala 2.10.0-RC1 like this: import language.dynamics import scala.collection.mutable.HashMap
I want to create dynamic lambda expressions so that I can filter a list
I'm currently using Generics to make some dynamic methods, like creating an object and
Below is the code from the Intercept method on a custom type that implements

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.