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

问题描述 投票:-1回答:1

我有两节课

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而言,它必须依赖于当前的文化

怎么样?

c# linq html-helper
1个回答
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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.