如何使用表达式设置属性值?

时间:2012-03-07 12:39:59

标签: c# linq expression

考虑以下方法:

public static void SetPropertyValue(object target, string propName, object value)
{
    var propInfo = target.GetType().GetProperty(propName,
                         BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);

    if (propInfo == null)
        throw new ArgumentOutOfRangeException("propName", "Property not found on target");
    else
        propInfo.SetValue(target, value, null);
}

你如何编写它的表达式启用等效而无需为目标传递额外的参数?

为什么这样做而不是直接设置属性我可以听到你说。例如,假设我们有以下类,其属性具有公共getter但私有setter:

public class Customer 
{
   public string Title {get; private set;}
   public string Name {get; set;}
}

我希望能够致电:

var myCustomerInstance = new Customer();
SetPropertyValue<Customer>(cust => myCustomerInstance.Title, "Mr");

现在这里有一些示例代码。

public static void SetPropertyValue<T>(Expression<Func<T, Object>> memberLamda , object value)
{
    MemberExpression memberSelectorExpression;
    var selectorExpression = memberLamda.Body;
    var castExpression = selectorExpression as UnaryExpression;

    if (castExpression != null)
        memberSelectorExpression = castExpression.Operand as MemberExpression;
    else
        memberSelectorExpression = memberLamda.Body as MemberExpression;

    // How do I get the value of myCustomerInstance so that I can invoke SetValue passing it in as a param? Is it possible

}

任何指针?

1 个答案:

答案 0 :(得分:111)

您可以使用扩展方法作弊并让生活更轻松:

public static class LambdaExtensions
{
    public 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);
            }
        }
    }
}

然后:

var myCustomerInstance = new Customer();
myCustomerInstance.SetPropertyValue(c => c.Title, "Mr");

这更容易的原因是因为您已经拥有调用扩展方法的目标。 lambda表达式也是一个没有闭包的简单成员表达式。在您的原始示例中,目标在闭包中捕获,到达基础目标PropertyInfo可能有点棘手。