如何为html助手制作这个表达式?

时间:2018-03-06 03:13:38

标签: c# linq html-helper

我有两个班级

public class LocalizedString {
    public string Ru { get; set; }
    public string Kk { get; set; }
    public string En { get; set; }
}

public class Person {
    public LocalizedString FirstName { get; set; }
    public LocalizedString LastName { get; set; }
}

我需要像x => x.FirstName.Ru这样的表达式 @Html.TextBoxFor(x => x.FirstName.Ru)

LastName,它必须取决于当前的文化

怎么做?

1 个答案:

答案 0 :(得分:0)

    public static Expression<Func<T, string>> GetMember<T>(
        Expression<Func<T, LocalizedString>> expression) {
        MemberExpression member = expression.Body as MemberExpression;
        PropertyInfo propInfo = member.Member as PropertyInfo;

        var param = Expression.Parameter(typeof(T), "x");
        Expression propertyLambda = Expression.Property(param, propInfo.Name);

        propertyLambda = Expression.Property(propertyLambda, "Ru");

        return Expression.Lambda<Func<T, string>>(propertyLambda, param);
    }
相关问题