Why would you use Expression> rather than Func?

后端 未结 10 2012
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 00:32

I understand lambdas and the Func and Action delegates. But expressions stump me.

In what circumstances would you use an Expression

10条回答
  •  心在旅途
    2020-11-22 00:46

    The primary reason is when you don't want to run the code directly, but rather, want to inspect it. This can be for any number of reasons:

    • Mapping the code to a different environment (ie. C# code to SQL in Entity Framework)
    • Replacing parts of the code in runtime (dynamic programming or even plain DRY techniques)
    • Code validation (very useful when emulating scripting or when doing analysis)
    • Serialization - expressions can be serialized rather easily and safely, delegates can't
    • Strongly-typed safety on things that aren't inherently strongly-typed, and exploiting compiler checks even though you're doing dynamic calls in runtime (ASP.NET MVC 5 with Razor is a nice example)

提交回复
热议问题