In response to your comment
I need to be able to handle List<Tomato>, IEnumerable<Tomato>, ObservableCollection<Tomato>, Tomato, TomatoCollection
The first three of them (and possibly the last one) can be resumed in IEnumerable<Tomato>
.
I see little sense if mixing a lambda that returns Tomato
in these, probably you would be better suited by an overloaded method.
So:
public class MyProduce(Func<IEnumerable<Tomato>> expression) // No need to make it an expression, so you allow for an already compiled lambda to be used.
if you want to add the Tomato
public class MyProduce(Func<Tomato> expression) {
Func<IEnumerable<Tomato>> expression2 = () => ( new Tomato[] { expression() });
// Here you use expression2 as in the previous constructor.
}
If you want to add Potato
to the mix, either make the class generic or create a superclass / interface common to both classes.
The bottom line is: make your preconditions stronger.
If you allow your code to receive anything, you won't be able to make a valid assumption of what you are dealing with and your code will end being a lot of spaggetti. Allowing the pass of an object
and hoping for your code to deal with it is forbidding you to use the facilities the language provide you (you could be writting in Javascript, for what is worth).