如何从匿名表达式中获取值?

发布于 2024-10-30 21:25:04 字数 441 浏览 1 评论 0 原文

为了简单起见,想象一下以下代码:

我想创建一个 Foo:

public class Foo
{
    public string Bar { get; set; }
}

并将其传递给一个特殊的 Html Helper 方法:

Html.SomeFunction(f => f.Bar);

其定义为:

public string SomeFunction<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)

我想获取此函数内 Bar 的值,但完全不知道如何得到它。

For sake of simplicity, imagine the following code:

I want to create a Foo:

public class Foo
{
    public string Bar { get; set; }
}

And pass it to a special Html Helper method:

Html.SomeFunction(f => f.Bar);

Which is defined as:

public string SomeFunction<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)

I want to get the value of Bar inside of this function, but have absolutely no idea how to get it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴骨ら 2024-11-06 21:25:04

只需编译表达式并获取值即可。

Func<TModel, TValue> method = expression.Compile();

TValue value = method(html.ViewData.Model);
// might be a slightly different property, but you can get the ViewModel 
// from the HtmlHelper object. 

Simply compile the expression and get the value.

Func<TModel, TValue> method = expression.Compile();

TValue value = method(html.ViewData.Model);
// might be a slightly different property, but you can get the ViewModel 
// from the HtmlHelper object. 
很酷不放纵 2024-11-06 21:25:04

您需要对表达式调用 Compile() 来获取 Func,然后执行它。

public string SomeFunction<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    TValue valueOfBar = expression.Compile()(html.Model); // Assumes Model is accessible from html.

    // Do stuff
}

旁注:如果不需要动态表达式或表达式分析,您不妨直接传递 Func 。

You will need to call Compile() on the expression to get the Func and then execute that.

public string SomeFunction<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    TValue valueOfBar = expression.Compile()(html.Model); // Assumes Model is accessible from html.

    // Do stuff
}

Side note: If there isn't any need for the dynamic expressions or expression analysis you might as well pass the Func directly in instead.

桃气十足 2024-11-06 21:25:04

对于那些使用没有 MVT 模型的表达式的人,可以通过以下方式获取属性的名称和值。

public static string Meth<T>(Expression<Func<T>> expression)
{
    var name = ((MemberExpression)expression.Body).Member.Name;
    var value = expression.Compile()();
    return string.Format("{0} - {1}", name, value);
}

使用:

Meth(() => YourObject.Property);

For those that are using expression without MVT Model, one would obtain name and value of property in a following way.

public static string Meth<T>(Expression<Func<T>> expression)
{
    var name = ((MemberExpression)expression.Body).Member.Name;
    var value = expression.Compile()();
    return string.Format("{0} - {1}", name, value);
}

use:

Meth(() => YourObject.Property);
葵雨 2024-11-06 21:25:04

在 Microsoft.AspNetCore.Mvc.Rendering 中,方法有帮助价值;

public static string ValueFor(this IHtmlHelper htmlHelper, Expression>表达式);

public string SomeFunction<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression){
var valueOfExpression = html.ValueFor(expression); 
//do your stuff
}

in Microsoft.AspNetCore.Mvc.Rendering there is helpfull valuefor method;

public static string ValueFor<TModel, TResult>(this IHtmlHelper htmlHelper, Expression<Func<TModel, TResult>> expression);

public string SomeFunction<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression){
var valueOfExpression = html.ValueFor(expression); 
//do your stuff
}
半世蒼涼 2024-11-06 21:25:04

使用 Compile() 将使用 Roslyn 编译器框架,并发出 MSIL 代码,该代码将动态加载到您的应用程序中。此可执行代码会占用内存,与“正常”内存相比,它不受垃圾回收的影响,您也无法自行释放它。如果您过于频繁地执行此操作(例如在 SQL 生成期间定期执行此操作),您最终将耗尽内存。我遇到了这个问题,并将我的解决方案作为开源库开源:

https:// www.nuget.org/packages/MiaPlaza.ExpressionUtils

Using Compile() will use the Roslyn compiler-framework and will emit MSIL-code that will be dynamically loaded into your application. This executable code takes up memory, and in contrast to "normal" memory it is not subject to garbage collection nor can you free it yourself. If you do this too frequently (like regularly during SQL generation) you will run out of memory eventually. I ran into this issue and open-sourced my solutions as an open-source library:

https://www.nuget.org/packages/MiaPlaza.ExpressionUtils

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文