What does this mean: Expression<Func>

In ASP.Net MVC, in the razor view, you can type this kind of code:

@Html.EditorFor(model => model.Name)

(in this case, it creates a textbox for the field Name of the object which is defined as the type of the model)

Now when i see the definition of the EditorFor method, it is this:

EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression);

So i tried implementing that kind of method myself, but i realy don’t see what this means
Expression<Func<TModel, TValue>>

Who can explain to me what an expression is, and the Func, and the Expression of type Func?

The explanations i found where all so abstract it didn’t give me a clue….

1

The Expression<T> class:

Represents a strongly typed lambda expression as a data structure in the form of an expression tree.

And:

The System.Linq.Expressions namespace contains classes, interfaces and enumerations that enable language-level code expressions to be represented as objects in the form of expression trees.

The abstract class Expression provides the root of a class hierarchy used to model expression trees.

The classes in this namespace that derive from Expression, for example MemberExpression and ParameterExpression, are used to represent nodes in an expression tree. The Expression class contains static (Shared in Visual Basic) factory methods to create expression tree nodes of the various types.

The enumeration type ExpressionType specifies the unique node types.


The Func<TResult> (and family) of delegates:

You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate.

The family basically encapsulates delegates for functions that have a return value.


Taken together, these are expressions that take func delegates typed take a parameter of the model type and return whatever value the lambda returns.

4

Expression is a class that enables you to take a lambda expression and transform it into a syntax tree. It is used by Linq (for example) to convert Linq queries into actual SQL queries. It is highly extensible so you could do other things with it.

http://msdn.microsoft.com/en-us/library/bb335710(v=vs.90).aspx

Have a look at http://en.wikipedia.org/wiki/Abstract_syntax_tree if you aren’t familiar with AST’s.

2

The HTML Helpers use a trick that has been called “Static Reflection” that was brought to the .NET framework to support LINQ to SQL and Entity Framework. Basically the Expression captures a Lambda which is verified at compile time (hence the Static) in a syntactic tree. The tree can be walked and can give things like the name of the member that is referenced by the body of the lambda.

Essentially, it’s a work around for .NET’s lack of a “symbol” keyword like some other languages support.

1

In this specific example, the generated HTML looks like this:

<input class="text-box single-line" id="Name" name="Name" type="text" value="ActualName" />

If the parameter was just a Func, you would be able to get value ActualName, but not the name of the property you’re accessing (Name). So, to get that name, you would need it as a string parameter (e.g. something like @Html.EditorFor(model => model.Name, "Name")).

This is not nice, because it means you have to enter the property name twice. Worse, you lose compile-time safety: if the property name changes, you won’t get an error on the second parameter.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật