In this answer to the question of the fastest way to determine if a property contains a given attribute, user Darin Dimitrov posited that expression trees are safer than reflect
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> 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
. So then later you can traverse it and get MemberInfo
of Id
. But it is not as universal as Reflection because you can't search by string
.