0
public class Tomato
{}
public class Potato
{}
public class UIPotatoBinding(Expression<Func<object>> expression)
{
    // What to put here to make sure lambda results in Potato(s)
}     
public class UITomatoBinding(Expression<Func<object>> expression)
{
    // What code do I need to put here to determine if the lambda expression being passed in
    // results in Tomato, List<Tomato>, IEnumerable<Tomato>, ObservableCollection<Tomato>
    // TomatoCollection, or some other Tomato related Linq construct.
}

This lambda stuff is still foreign to me. I apologize if I am asking something obvious that has been answered elsewhere already.

Wonderbird
  • 392
  • 4
  • 12
  • If there is no common superclass/interface so you can get the compiler to check it at compile time... run it, cast and check for exceptions. – SJuan76 Nov 15 '13 at 01:56
  • 1
    Better, use `TypeOf` or `as` and check if anything survives the filter. – SJuan76 Nov 15 '13 at 01:58
  • Can you edit your question to include what the problem being solved one level higher than this is, please? Knowing that will probably help get a better answer or point you in a different direction. – chwarr Nov 15 '13 at 02:21
  • @SJuan76 I would MUCH prefer to check it at compile time but have no idea how that would work. I need to be able to handle List, IEnumerable, ObservableCollection, Tomato, TomatoCollection, and possibly several more. Since one of the "great" things about a lambda expression is deferred evaluation/execution I do not comprehend how it would be possible to do compile time checking. – Wonderbird Nov 15 '13 at 14:13

2 Answers2

3

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).

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • `ArrayList` isn't generic; you probably just want to use an array there instead. – Servy Nov 15 '13 at 14:57
  • @Servy fixed it thanks. Coming from Java and still learning the BCL (my interest in answering is getting corrected) :-D. – SJuan76 Nov 15 '13 at 15:01
  • Yeah, it's always the Java users that make that mistake – Servy Nov 15 '13 at 15:04
  • Thanks to @SJuan76 for input/response. There are 513 "MyProduce" classes in our solution. I did not create the constructor syntax for the base class or the 513 derived classes. ie MyTomatos, MyPotatos, MyOnions, etc, etc... (I was just trying to figure out how to properly throw an exception/complaint if a passed in lambda that results in Potatos gets passed to the MyOnions constructor... Since lambda's are deferred evaluation I wanted a way to check the incoming lambda and complain if the type did not match instead of just hoping nobody ever passed a bad/mismatched lambda... – Wonderbird Nov 15 '13 at 20:04
2

Here is an example to do what you want. Will run in linqpad if you have it.

void Main()
{
    Expression<Func<object>> f = () => new Potato();
    Helper.MyProduce(f);
}


public class Tomato 
{}
public class Potato
{}

public static class Helper
{
    public static void MyProduce(Expression<Func<object>> expression)
    {
        var func = expression.Compile();
        var result = func();

        if(result is Tomato)
            Console.Write("Tomato");
        else if (result is Potato)
            Console.Write("Potato");
        else
            Console.Write("Unknown");
    }
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
josey wales
  • 271
  • 1
  • 4