public void GiveMeAProp(Expression<Func<TInstance,TProp>> prop)
Is often used as the standard way of saying "please give me a property selector". But there are many things that can satisfy this signature but not actually be the "real" property. e.g. All of the following compile.
GiveMeAProp(x => Method());
GiveMeAProp((x) => localVariable);
GiveMeAProp(x => x);
GiveMeAProp(x => (Type)x);
GiveMeAProp((x) => !x.BooleanProp);
I certainly could simply declare that my method takes a MemberExpression
, but then I lose the strict typing of my expression, and in particular lose the ability to write:
public void SetThisProp(Expression<Func<TInstance,TProp>> prop, TProp value)
I can have a run time check that the expression I've ended up with has a Body that is a MemberExpression
, but it would be really nice if I could have the type system enforce that for me? No other kind of Expression<T>
is actually a valid argument in my use-case.
Question: Is there anyway that I can enforce statically that an argument represents a MemberExpression
and also that it returns a type T
?
Aucun commentaire:
Enregistrer un commentaire