To be sure that I understand could someone confirm that lambda expressions use reflection?
-
2No, they don't - but Linq (which is often used with Lambda expressions) does. – Matthew Watson Nov 20 '12 at 15:04
-
4LINQ doesn't necessarily use reflection either. – Rawling Nov 20 '12 at 15:05
-
2I can deny. They have nothing to do with reflection. http://stackoverflow.com/a/3844487/44620 – Jonas Elfström Nov 20 '12 at 15:05
-
1Is the lack of types part of the confusion? Lambda expressions use type inference to determine the types of the parameters. – stusmith Nov 20 '12 at 15:06
-
What for the problem to must be solved? – Boris Gappov Nov 20 '12 at 15:06
-
1Lambda expressions are a simplification on top of delegate instantiations... – Boomer Nov 20 '12 at 15:09
-
No. I don't think so.. Can you clarify the question a bit more... – Gishu Nov 20 '12 at 15:04
-
I think OP meant expression trees. In that case see [is-reflection-used-when-retrieving-information-from-a-linq-expression](http://stackoverflow.com/questions/11358618/is-reflection-used-when-retrieving-information-from-a-linq-expression) – nawfal Oct 12 '13 at 14:56
3 Answers
They can, but they are not required to. If they are compiled to a delegate, then no reflection is necessary: however, if they are compiled to ab expression tree, then an expression tree is absolutely reflection-based. Some parts of expression trees can be assembled directly from metadata tokens (ldtoken) - in particular methods (including operators and getters/setters, and types) - but some other parts cannot . This includes properties (PropertyInfo
cannot be loaded by token) - so the IL for a compiled lambda can explicitly include GetProperty
etc.
But however it is loaded (token or reflection), an expression tree is expressed in terms of reflection (MemberInfo
, etc). This might later be compiled, or might be analysed by a provider.
To help performance, the expression compiler may cache some or all of the expression tree, and re-use it.

- 70,104
- 56
- 326
- 368

- 1,026,079
- 266
- 2,566
- 2,900
Lambda expressions are turned by the compiler into either anonymous functions or expression trees. Since reflection can only be performed at runtime it doesn't come into the picture at all when considering what the compiler does with them in either case.
At runtime, lambda expressions might result in reflection being used under very specific circumstances:
For anonymous functions: if you write an anonymous function that explicitly reflects on something then the lambda will perform that reflection when invoked. This is of course the same as if you were reflecting from within a "proper" method.
For expression trees (i.e. values of type Expression<TDelegate>
for some TDelegate
): using them at runtime when working with an IQueryable
might result in reflection being used by the query provider. For example, assume that you do:
var user = new User { Id = 42 };
var posts = Queryable.Posts.Where(p => p.UserId == user.Id);
When posts
is about to be materialized the query provider sees that it must find those posts with a UserId
equal to the Id
of the variable user
. Since that id has a specific known value at runtime, the query provider needs to fish it out of user
. One way it might decide to do that is through reflection.

- 428,835
- 81
- 738
- 806
-
Regardless of how it is used later, expression trees are fundamentally expressed in terms of reflection. – Marc Gravell Nov 20 '12 at 16:06
-
@MarcGravell: I can understand the reasoning, but OTOH if you get technical something like `Posts.Where(p => p.IsDeleted)` on EF won't result in actual reflection taking place... In any case I 'm open to suggestions if you feel the answer needs improvement. – Jon Nov 20 '12 at 16:11
-
it won't call GetValue etc, true: however, simply building the expression tree itself is: reflection-based – Marc Gravell Nov 20 '12 at 16:15
-
@MarcGravell: I agree with "reflection-based" or "reflection-oriented" as a description, *but*. It's the compiler that builds the tree, and since compiler != runtime (excluding expressly invoking compiler services etc) while reflection = runtime, it follows that compiler != reflection. In the end I feel that the one-word answer is "no" because it's not the expressions themselves that tap into reflection. – Jon Nov 20 '12 at 16:21
-
if you look at what the compiler emits, it is runtime reflection :) – Marc Gravell Nov 20 '12 at 18:20
A lambda expressions is just compiler syntax sugar for creating delegates. No reflection used here.

- 38,496
- 59
- 215
- 394
-
1No, there are two possible outcomes from lambdas. Expression trees are fundamentally redlection-based. – Marc Gravell Nov 20 '12 at 16:05