jeudi 13 décembre 2018

How to set property values for indexers using Expressions?

I am currently kind of stuck with setting a value into an indexer expression passed into the following function:

private static void SetPropertyValue<T, TValue>(this T target, Expression<Func<T, TValue>> memberLamda, TValue value)
{
    var memberSelectorExpression = memberLamda.Body as MemberExpression;
    if (memberSelectorExpression != null)
    {
        var property = memberSelectorExpression.Member as PropertyInfo;
        if (property != null)
        {
            property.SetValue(target, value, null);
            return;
        }
    }
}

I have the following class

class Entity 
{
    public object this[string name] 
    {
        get { /* */ }
        set { /* */ }
    }
}

When I now call the previous defined function with the following values I only get a reference to the backing get_Item() method:

SetPropertyValue(target, x => x[memberName], value);

Does anybody have a hint for me, how to solve this issue? Any idea will help.

Many thanks to all of you...





Aucun commentaire:

Enregistrer un commentaire