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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:55:19+00:00 2026-05-25T20:55:19+00:00

I am trying to build a dynamic Property Accessor. Want something which is like

  • 0

I am trying to build a dynamic Property Accessor. Want something which is like really fast as close to calling the actually Property. Dont want to go the Reflection route as its very slow. So i opted to using DynamicAssembly and inject IL using ILGenerator. Below is the ILGenerator related code which seems to work

        Label nulllabel = getIL.DefineLabel();
        Label returnlabel = getIL.DefineLabel();
        //_type = targetGetMethod.ReturnType;
        if (methods.Count > 0)
        {
            getIL.DeclareLocal(typeof(object));
            getIL.DeclareLocal(typeof(bool));

            getIL.Emit(OpCodes.Ldarg_1); //Load the first argument

            //(target object)

            //Cast to the source type

            getIL.Emit(OpCodes.Castclass, this.mTargetType);
            //Get the property value

            foreach (var methodInfo in methods)
            {
                getIL.EmitCall(OpCodes.Call, methodInfo, null);

                if (methodInfo.ReturnType.IsValueType)
                {
                    getIL.Emit(OpCodes.Box, methodInfo.ReturnType);
                    //Box if necessary
                }
            }

            getIL.Emit(OpCodes.Stloc_0); //Store it
            getIL.Emit(OpCodes.Br_S,returnlabel);

            getIL.MarkLabel(nulllabel);
            getIL.Emit(OpCodes.Ldnull);
            getIL.Emit(OpCodes.Stloc_0);

            getIL.MarkLabel(returnlabel);
            getIL.Emit(OpCodes.Ldloc_0);
        }
        else
        {
            getIL.ThrowException(typeof(MissingMethodException));
        }
        getIL.Emit(OpCodes.Ret);

So above get the first argument which is the object that contains the property. the methods collection contains the nested property if any. for each property i use EmitCall which puts the the value on the stack and then i try to box it. This works like a charm.

The only issue is if you have a property like Order.Instrument.Symbol.Name and assume that Instrument object is null. Then the code will throw an null object exception.

So this what i did, i introduced a null check

            foreach (var methodInfo in methods)
            {
                getIL.EmitCall(OpCodes.Call, methodInfo, null);

                getIL.Emit(OpCodes.Stloc_0);
                getIL.Emit(OpCodes.Ldloc_0);

                getIL.Emit(OpCodes.Ldnull);
                getIL.Emit(OpCodes.Ceq);
                getIL.Emit(OpCodes.Stloc_1);
                getIL.Emit(OpCodes.Ldloc_1);
                getIL.Emit(OpCodes.Brtrue_S, nulllabel);
                getIL.Emit(OpCodes.Ldloc_0);

                if (methodInfo.ReturnType.IsValueType)
                {
                    getIL.Emit(OpCodes.Box, methodInfo.ReturnType);
                    //Box if necessary
                }
            }

Now this code breaks saying That the object/memory is corrupted etc. So what exactly is wrong with this code. Am i missing something here.

Thanks in Advance.

  • 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-25T20:55:19+00:00Added an answer on May 25, 2026 at 8:55 pm

    Previously, if you had consecutive properties P returning string and then Q returning int, you would get something like this:

    ...   
    call P // returns string
    call Q // requires a string on the stack, returns an int
    box
    ...
    

    Now you have something like this:

    ...
    call P // returns string
    store  // stores to object
    ...    // load, compare to null, etc.
    load   // loads an *object*
    call Q // requires a *string* on the stack
    store  // stores to object *without boxing*
    ...
    

    So I see two clear problems:

    1. You are calling methods in such a way that the target is only known to be an object, not a specific type which has that method.
    2. You are not boxing value types before storing them to a local of type object.

    These can be solved by reworking your logic slightly. There are also a few other minor details you could clean up:

    1. Rather than ceq followed by brtrue, just use beq.
    2. There’s no point in doing Stloc_1 followed by Ldloc_1 rather than just using the value on the stack since that local isn’t used anywhere else.

    Incorporating these changes, here’s what I’d do:

    Type finalType = null;
    
    foreach (var methodInfo in methods)
    {
        finalType = methodInfo.ReturnType;
    
        getIL.EmitCall(OpCodes.Call, methodInfo, null);
        if (!finalType.IsValueType)
        {
            getIL.Emit(OpCodes.Dup);
            getIL.Emit(OpCodes.Ldnull);
            getIL.Emit(OpCodes.Beq_S, nulllabel);
        }
    }
    
    if (finalType.IsValueType)
    {
        getIL.Emit(OpCodes.Box, methodInfo.ReturnType);
        //Box if necessary
    }
    
    getIL.Emit(OpCodes.Br_S, returnLabel);
    
    getIL.MarkLabel(nulllabel);
    getIL.Emit(OpCodes.Pop);    
    getIL.Emit(OpCodes.Ldnull);
    
    getIL.MarkLabel(returnlabel);
    

    Note that we can get rid of both locals since we now just duplicate the top value on the stack before comparing against null.

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

Sidebar

Related Questions

I am trying to build a dynamic forms app like wufoo . Creating new
I am trying to build a dynamic Expression Tree like below : Func<IEnumerable<int>, int,
Using JPA 2 with EclipseLink implementation. I'm trying to build a dynamic query which
I am trying to find the best way to build a dynamic linq query
I'm trying build a method which returns the shortest path from one node to
Trying to build dynamic output from json and using jq/template tmpl display rows/columns. Somehow
I'm trying to build an archetype structure like this, a webapp with some custom
I hope someone can help me..... i am trying to build a dynamic form
I am trying to build a real-time web service that deals with dynamic 10k
I'm trying to build a dynamic query with Doctrine's query builder. Let's say I'd

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.