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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:25:03+00:00 2026-05-23T16:25:03+00:00

I have had to find a way to substitute implicit field references in a

  • 0

I have had to find a way to substitute implicit field references in a lambda expression with it’s real value. For example :

Expression<Func<TestObject, String>> exp = null;
for (int i = 0; i < 1; i++)
{
    exp = t => t.SubObjs[i].TestSTR;
}
Func<TestObject, String> testFunc = exp.Compile();
String testValue = testFunc(myObj);

When inspecting the delegate, you can see this :

{t => t.SubObjs.get_Item(value(testExpression.Program+<>c__DisplayClass4).i).TestSTR}

When calling the delegate outside the for loop, the value of “i” is solved, by reference. But “i” have changed since it’s last iteration (“i” == 1 and not 0).

So I build a specific ExpressionVisitor in order to replace the corresponding node with a ConstantExpression :

public class ExpressionParameterSolver : ExpressionVisitor
{
    protected override Expression VisitMember(MemberExpression node)
    {
        if (node.ToString().StartsWith("value(") && node.NodeType == ExpressionType.MemberAccess)
        {
            var index = Expression.Lambda(node).Compile().DynamicInvoke(null);
            return Expression.Constant(index, index.GetType());
        }
        return base.VisitMember(node);
    }
}

I don’t have found a way other than .StartsWith(“value(“) in order to detect that the current node is a reference to a field… this kind of node inherits from FieldExpression but this class is internal, and I’m not sure FieldExpression only encapsulate what I consider an “implicit field reference”.

So is there a way (an attribute or a method) to explicitly know that a MemberExpression node is an implicit field reference ???

Thanks in advance !!!

and thanks to this stakx post

  • 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-23T16:25:04+00:00Added an answer on May 23, 2026 at 4:25 pm

    Just fetch the Member property from the expression and see whether it’s a FieldInfo…

    If you only want it to be for cases where the class is compiler-generated you could use

    if (expression.Member is FieldInfo && 
        expression.Member
                  .DeclaringType
                  .IsDefined(typeof(CompilerGeneratedAttribute), false))
    {
        ....
    }
    

    There can be other reasons why a type might be compiler-generated though. It doesn’t sound like a terribly good idea to me.

    Can’t you just avoid capturing loop variables in your lambda expressions to start with?

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

Sidebar

Related Questions

I have had searched the web to find the way to put a name
I find jsfiddle very useful for client side web development. I have had a
I have had a look around online to try and find out the best
Possible Duplicates: Test for value in Javascript Array Best way to find an item
I have been trying to find an 'easy' way of including files in my
I have had Delphi 2007 for a while. I tried the Delphi 2009 trial.
I have had a few problems getting this right, so I wanted to ask
I have had a talk with a friend of mine about the relative vulnerability
I have had been on a roller coaster trying to get ImageMagick to work
I have had the following problem for a while and I am really not

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.