This post details workarounds for passing properties as references including using Expressions such as
public void StoreProperty(Expression<Func<T, object>> expr)
This approach is ok and I note many frameworks appear to use this (eg automapper, autofac) as detailed in James Gregory's Introduction to static reflection where he states
The great thing about this is that if you change the name of a member inside a lambda, you’ll get a compile error if you haven’t updated all the references! No more hidden bugs.
Whilst I much prefer this approach it is still not perfect as you can pass any expression returning an object (or whatever your return val is) eg
x => x.Name) //fine
x => x.Name+"x") //runtime error
Is there currently any better way to reference the property (by locking down the Expression, or some other way)
If No, how might a future version of C# lock down the Expression? for example, something like:
public void StoreProperty(Expression<Func<T, object>> expr) where expr.Member is PropertyInfo
clarification: above is only an example, I know this isn't currently supported; thats what I'm trying to discuss.