Because when you search for your field (as in that question) you use string representation
"Id". Once it is changed your reflection will collapse.
What Darin suggests is static typing:
Expression<Func<Program, int>> expression = p => p.Id;
You see that? This is interesting, but not well-known feature of C# 4.0 compiler: automatically build expression tree from lambda expression and cast it to
Expression<T>. So then later you can traverse it and get
Id. But it is not as universal as Reflection because you can't search by
The question as it is stated is why is expression trees safer then reflection.
The answer is that they are both using reflection.
Edit to clarify - MemberInfo.GetCustomAttributes is a reflection call.