简体   繁体   中英

Get chained property names from MemberExpression

I'm creating a form builder for MVC and I want to simulate Razor's treatment of chained properties in the following way:

builder.TextBoxFor(x => x.User.Email);

Which would produce the following in the same manner as Razor:

<input id="User_Email" name="User.Email" type="textbox" />

The following code works for a single level of chaining (eg x.Email produces Email ), but I'm trying to detect when there is a parent before the final property and then use recursion to go back up the chain (or at least go one step up).

private static string GetFieldName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
{
    var memberExpression = (MemberExpression) expression.Body;
    return memberExpression.Member.Name;
}

How can I adapt this so that x.User.Email produces User.Email and not just Email as it does currently?

You need a little recursion:

private static string GetPropertyPath<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
{
    var propertyPath = new Stack<string>();
    var body = (MemberExpression)expression.Body;

    do
    {
        propertyPath.Push(body.Member.Name);

        // this will evaluate to null when we will reach ParameterExpression (x in "x => x.Foo.Bar....")
        body = body.Expression as MemberExpression;
    }
    while (body != null);

    return string.Join(".", propertyPath);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM