Get getter and setter as methods or delegate RRS feed

  • Question

  • Hi,

    Somewhere low-level, probably MSIL, each property's get and set accessors

    string Text { get; set; }

    resolve to a pair of methods:

    string get_Text();
    void  set_Text(string value);


    Is it possible to access these methods
    as delegates in C# based on a
    Linq-Lambda Expression such as:
    "type => type.Text" or 2nd best a PropertyInfo?
    Background is avoiding non-typesafe and probably slow
    reflection based code that uses GetGetMethod and GetSetMethod
    and Invoke respectively.


    Tuesday, March 13, 2012 3:59 PM


All replies

  • What are you trying to do here?

    You can use an expression tree to get a PropertyInfo, but this is still effectively using reflection at some point.  For example, take a look at the expression tree code here: http://monotorrent.blogspot.com/2009/12/yet-another-inotifypropertychanged-with_05.html 

    Once you have the MemberExpression, you can get teh MemberInfo (which will be a PropertyInfo if it's a property) via MemberExpression.Member...

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, March 13, 2012 4:11 PM
  • Hi Reed

    thanks for the link. Your assumption is right. It's about wrapping
    VM-Properties and especially the setter. We/I already use
    expression tree - found it in some MVVM blog.
    Also we use something like the ChangeNotifier class for that.
    In addition to the sample of your blog, we do not set the value to
    a field of the  wrapper: TValue value; in the sample -
    but to a property of an "entity", which the VM wrapps around.
    The entity-property is also defined by an Expression.

    So the initialization goes like:

    author = new EntityChangeNotifier<string> (() => Author, notify, entity => entity.Author)

    the code relevant is like:

    	TPropertySource owner,
    	Expression<Func<TPropertySource, object>> vmPropertyExpression,
    	Expression<Func<TEntity, object>> entityPropertyExpression
    	var lambda = expression as LambdaExpression;
        MemberExpression memberExpression;
        if (lambda.Body is UnaryExpression)
            var unaryExpression = lambda.Body as UnaryExpression;
            memberExpression = unaryExpression.Operand as MemberExpression;
            memberExpression = lambda.Body as MemberExpression;
        var propertyInfo = memberExpression.Member as PropertyInfo;
        _getter = propertyInfo.GetGetMethod();
        _setter = propertyInfo.GetSetMethod();    
     public override TPropertyValue Value
        get { (TPropertyValue)_getter.Invoke(); }
            TPropertyValue currentValue = (TPropertyValue)_getter.Invoke(_owner._entity, null);
            if (!_valueComparer.Equals(currentValue, value))
                _setter.Invoke(_owner._entity, new object[]{ value });

    So, returning to the original request, I'd like to get rid
    of the reflection based setter and getter, assuming that
    they are slower then the using the properties themselves (or
    if possible delegates for the getter/setter).
    Instead, I'd like to replace the MethodInfo's by delegates.
    Probably it's not possible, cause I have never seen such code that gets access
    to get/set method pair other then through reflection.


    Wednesday, March 14, 2012 9:36 AM
  • Chris,

    Once you have the setter, you should be able to use Delegate.CreateDelegate (http://msdn.microsoft.com/en-us/library/s3860fy3.aspx) to generate a delegate from it, then use the delegate instead of MethodInfo.Invoke.

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, March 14, 2012 10:46 PM
  • Great!
    exacetly what I was looking for.


    Thursday, March 15, 2012 5:30 PM
  • Hi Chris,

    I'm working on something similar. Would it be possible to share a complete code sample?

    Kind regards,

    Dinand Veldman

    Wednesday, February 13, 2019 1:13 PM