How to create Expression>;

前端 未结 1 1265
暗喜
暗喜 2021-01-05 19:04

Is it possible to create Expression>() which can be used in different htmlHelpers (for instance in CheckBoxFor()), i

相关标签:
1条回答
  • 2021-01-05 19:31

    Sure:

    static Expression<Func<TModel,TProperty>> CreateExpression<TModel,TProperty>(
        string propertyName)
    {
        var param = Expression.Parameter(typeof(TModel), "x");
        return Expression.Lambda<Func<TModel, TProperty>>(
            Expression.PropertyOrField(param, propertyName), param);
    }
    

    then:

    var lambda = CreateExpression<SomeModel, bool>("IsAlive");
    
    0 讨论(0)
提交回复
热议问题