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

  • Home
  • SEARCH
  • 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 6111037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:35:05+00:00 2026-05-23T14:35:05+00:00

I’m trying to create delegates to access any Property Get & Set methods. I

  • 0

I’m trying to create delegates to access any Property Get & Set methods.

I have found the following code (on this post : driis about deep properties ) which works really fine :

public static class Extractor<TObject> where TObject : class
{
    public static DelegateAccessor<TObject, TValue> GetAccessorsDelegates<TValue>(Expression<Func<TObject, TValue>> expression)
    {
        Func<TObject, TValue> getter = expression.Compile();

        ParameterExpression pObj = expression.Parameters[0];
        ParameterExpression pValue = Expression.Parameter(typeof(TValue), "value");
        BlockExpression setterBlock = Expression.Block(Expression.Assign(expression.Body, pValue));
        Expression<Action<TObject, TValue>> setterExpression = Expression.Lambda<Action<TObject, TValue>>(setterBlock, pObj, pValue);
        Action<TObject, TValue> setter = setterExpression.Compile();

        return new DelegateAccessor<TObject, TValue>(getter,setter);
    }
}

For example : when I create the setter from the Expression in parameter, I obtain my getter and setter delegates like these ones :

get : (t => t.ID)
set : (t, value) => { t.ID = value}

My problem is with indexers like this one :

public object this[int id]

When I call with a sub property of an indexer, there is no problem. It works fine since the indexer, internally named “Item” is accessed by reading it :

Extractor<MyObject>.DelegateAccessor(t => t.MySubTable[2].MyProperty)

the expression is :

t => t.MySubTable.get_Item(2).MyProperty

But I don’t find a way to get the internal setter expression. My goal is to obtain something like this :

(t, value) => { t.MySubTable.set_Item(2, value) }

But I can’t use assignment with a Lambda like this one :

(t,v) => t.SubStrings[0] = v

I get an error CS0832 : An expression tree may not contain an assignment operator.

Is there a way to write a Lambda Expression in order to find the “set_Item(2, value)” body in a Expression> parameter ???

Thanks in advance, I know it’s a hard question…

  • 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-23T14:35:05+00:00Added an answer on May 23, 2026 at 2:35 pm

    If I understand you correctly, you want a method that works exactly as the one you already have, but that also works for something like

    Extractor<MyObject>.DelegateAccessor(t => t.MySubTable[2])
    

    If that’s the case, something like this should work:

    public static DelegateAccessor<TObject, TValue> GetAccessorsDelegates<TValue>(
        Expression<Func<TObject, TValue>> expression)
    {
        Func<TObject, TValue> getter = expression.Compile();
    
        Action<TObject, TValue> setter = null;
        ParameterExpression pValue = Expression.Parameter(typeof(TValue), "value");
        ParameterExpression pObj = expression.Parameters[0];
        if (expression.Body is MemberExpression)
        {
            Expression setterBlock = Expression.Assign(expression.Body, pValue);
            Expression<Action<TObject, TValue>> setterExpression =
                Expression.Lambda<Action<TObject, TValue>>(setterBlock, pObj, pValue);
            setter = setterExpression.Compile();
        }
        else
        {
            var getterCall = expression.Body as MethodCallExpression;
            if (getterCall != null)
            {
                var method = getterCall.Method;
                if (method.IsSpecialName && method.Name.StartsWith("get_"))
                {
                    var parameters = method.GetParameters()
                                           .Select(p => p.ParameterType)
                                           .Concat(new[] { method.ReturnType })
                                           .ToArray();
                    var setterName = "set_" + method.Name.Substring(4);
                    var setterMethod =
                        method.DeclaringType.GetMethod(setterName, parameters);
                    var setterCall = Expression.Call(
                        getterCall.Object, setterMethod,
                        getterCall.Arguments.Concat(new[] { pValue }));
                    var setterExpression =
                        Expression.Lambda<Action<TObject, TValue>>(
                            setterCall, pObj, pValue);
                    setter = setterExpression.Compile();
                }
            }
        }
    
        return new DelegateAccessor<TObject, TValue>(getter, setter);
    }
    

    The first part is basically copy of the method you have. The second part checks whether the expression is a call to a getter method and if it is, replaces it with call to setter method. There is some work regarding parameters, but other than that, quite straight-forward.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and

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.