I'm trying to create a class that takes a lambda and stores it internally. The syntax would be something like:
class Lambda<TIn, TOut> {
private Expression<Func<TIn, TOut>> expr;
private Func<TIn, TOut>> func;
public Lambda(Expression<Func<TIn, TOut>> e) {
expr = e;
}
public Lambda(Func<TIn, TOut> f) {
func = f;
}
public static implicit operator Lambda<TIn, TOut>([lambdatype] o) {
return new Lambda(o);
}
}
Usage:
Lambda<TIn, TOut> l = o => ... ;
But I'm having some trouble figuring out the details. I know that a lambda is an anon type until it's assigned to either an expression or a delegate and that I would (I believe) have to use an implicit operator to get the syntax I'm going for, but beyond that I've hit a wall. I can use Expression or Func in my implicit operator if they've already been assigned to a variable like so:
Expression<Func<T1, T2>> e = o => ...;
Func<T1, T2> f = o => ...;
Lambda<T1, T2> l1 = e, l2 = f;
But I'd much prefer to just assign the lambda itself and have the class figure out the details.