简体   繁体   English

从ASP.NET MVC Lambda Expression获取价值

[英]Get value from ASP.NET MVC Lambda Expression

I am trying to create my own HTML Helper which takes in an expression (similar to the built-in LabelFor<> helper. I have found examples to obtain the value of a property when the expression is similar to this: 我正在尝试创建自己的HTML Helper,它接受一个表达式(类似于内置的LabelFor <>帮助器。我已经找到了在表达式类似于此时获取属性值的示例:

model => model.Forename

However, in some of my models, I want to obtain properties in child elements, eg 但是,在我的一些模型中,我想获取子元素中的属性,例如

model => mode.Person.Forename

In these examples, I cannot find anyway to (easily) obtain the value of Forename. 在这些例子中,无论如何我都找不到(轻松)获取Forename的值。 Can anybody advise on how I should be getting this value. 任何人都可以建议我应该如何获得这个价值。

Thanks 谢谢

If you are using the same pattern that the LabelFor<> method uses, then the expression will always be a LambdaExpression and you can just execute it to get the value. 如果您使用的是LabelFor <>方法使用的相同模式,那么表达式将始终是LambdaExpression,您只需执行它即可获取值。

var result = ((LambdaExpression)expression).Compile().DynamicInvoke(model);

Generally, you can always wrap generic expressions in LambdaExpressions and then compile & invoke them to get the value. 通常,您始终可以在LambdaExpressions中包装泛型表达式,然后编译并调用它们以获取值。

If what you want isn't the value of Forename, but the field itself (fx. to print out the string "Forename") then your only option is to use some form of expressionwalking. 如果你想要的不是Forename的值,而是字段本身(fx。打印出字符串“Forename”)那么你唯一的选择是使用某种形式的表达式。 In C#4 the framework provides a class called ExpressionVisitor that can be used for this, but for earlier versions of the framework you have to implement it yourself - see: http://msdn.microsoft.com/en-us/library/bb882521(VS.90).aspx 在C#4中,框架提供了一个名为ExpressionVisitor的类,可以用于此,但是对于框架的早期版本,您必须自己实现它 - 请参阅: http//msdn.microsoft.com/en-us/library/ bb882521(VS.90)的.aspx

Your looking for the value? 你在寻找价值?

Why wouldn't this work? 为什么这不起作用?

    public object GetValue<T>( Expression<Func<T>> accessor )
    {
        var func = accessor.Compile();

        return func.Invoke();
    }

I've answered this separately because there was two things I didn't like about the accepted answer. 我已经单独回答了这个问题,因为有两件事我不喜欢接受的答案。

  1. It doesn't explain how to get a reference to the model which is a critical piece of information when writing a custom html helper 它没有解释如何在编写自定义html帮助程序时获取对模型的引用,该模型是关键信息
  2. If you know what the delegate type for the lambda expression is up front it is unneccessary to cast it to the Lambda expression and use DynamicInvoke. 如果您事先知道lambda表达式的委托类型是什么,则将其强制转换为Lambda表达式并使用DynamicInvoke是不必要的。 In my experience of writing custom helpers I tend to know up front the types. 根据我编写自定义助手的经验,我倾向于先了解类型。

Example where I know up front it is designed for a lambda expression that yields a byte array: 我之前知道的示例是为lambda表达式设计的,它产生一个字节数组:

public static MvcHtmlString MyHelper<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, byte[]>> expression)
    {

        var compiledExpression = expression.Compile();
        byte[] byteData = compiledExpression(htmlHelper.ViewData.Model);

        ...
        ...
        ...

        return new MvcHtmlString(.......);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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