Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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....

share|improve this question
4  
What don't you get? Don't want to give you another overly abstract answer... –  Oded Jan 16 '13 at 8:51

4 Answers 4

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.

share|improve this answer
1  
I think i'm more lost in the expression part. I've tried the Func part en i see that i can invoke that. So when i have a function that gets a Func as parameter, it's sort of a pointer to a method in a class, and i can execute that method / the Func. But the expression and expression tree' thing, i don't get what i can do with it. Especially in this case, why isn't the Func` enough? isn't the model => model.Name also a Func which can be used in the DisplayFor, so what's the need for the Expression. When i look in the watch what the expression can do, it doesn't clarify much. –  Michel Jan 16 '13 at 9:44
1  
@Michel - stackoverflow.com/questions/793571/… –  Oded Jan 16 '13 at 9:46
    
That makes sense.. but... can you explain me why and how the EditorFor uses the Expression? Because the property gets send in as Expression, and then what to do with the expression? I think i'm lost at this part 'This tree structure describes what a lambda expression does rather than doing the actual thing.' –  Michel Jan 16 '13 at 9:53
    
@Michel - Take a look at the other answers there and follow the links - Marc Gravel has a good blog post about the subject, and the links by LSpencer as well. –  Oded Jan 16 '13 at 9:56

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.

share|improve this answer
    
The wikipedia article is exactly one of those articles which makes no sense to me :-) –  Michel Jan 16 '13 at 9:46
    
@Michel yacc tutorials go over this a lot because typically, you build an AST in the parser. The concept is fairly simple, for example: an if statement has an expression to check, and a body, so there would be an if node, with child[0] being the if expression, and the child[1] being the if "body" that is executed when the expression result is true. There are node types for adding, subtracting, calling functions, etc. it is simply a tree representation of the source code. –  doug65536 Jan 16 '13 at 18:19

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.