vendredi 16 novembre 2018

Static method to swap the values of two objects' property values using Expressions

I'm trying to make a utility function that can Swap two property values given by two lambda expressions - assuming that both expressions indicate properties that have a getter and a setter:

Swap(() => John.Lunch, () => Jimmy.Lunch);

I imagine the method would need to look something like this, but I'm having trouble pulling it together.

private static void Swap<TProperty>(
    Expression<Func<TProperty>> first,
    Expression<Func<TProperty>> second)
{
    PropertyInfo firstProp = (PropertyInfo)((MemberExpression)first.Body).Member;
    PropertyInfo secondProp = (PropertyInfo)((MemberExpression)second.Body).Member;
    object firstObj = (((first.Body as MemberExpression).Expression as MemberExpression)
        .Expression as ConstantExpression).Value;
    object secondObj = (((second.Body as MemberExpression).Expression as MemberExpression)
        .Expression as ConstantExpression).Value;
    TProperty temp = (TProperty)firstProp.GetValue(firstObj);
    firstProp.SetValue(firstObj, secondProp.GetValue(secondObj));
    secondProp.SetValue(secondObj, temp);
}

Getting to the "subject" object of the expression is proving to be difficult, although I'm pretty sure it's possible.





Aucun commentaire:

Enregistrer un commentaire