So I’m developing something that works on dynamic variables using C# 4. And I’m in a situation where I have two variables a and b and I know that either a.Foo(b) or b.Foo(a) is defined. However I don’t know which so at the moment I use something like this:
dynamic a, b, result;
...
try
{
result = a.Foo(b);
}
catch
{
result = b.Foo(a);
}
Which is horrible (not only is it inelegant but it’s very slow since there is around a 0.5 probability of raising an Exception and producing a stack trace). I could use reflection but I expect that’d be quite slow too.
So is there a better way?
So that’s the problem… but I’ll also explain the context since I think there’s a good chance there’s a better way to handle the whole situation. Essentially I am building expression trees (using my own node structure) that work with many different datatypes.
If you consider the expression 1+'2', the a and b values are the operands 1 and '2'. If I want to evaluate the + node, which has the sub-trees a and b, then either operand may contain a method to Add the other operand’s type to it. That is, either a.Add(b) is implemented or b.Add(a) is implemented.
I can only think of using reflection, the method above, or producing duplicate functions in both types of a and b to model the symmetry.
I think (like Ed said) I would resolve this by using a Interface (i.e. IResolve), which is applied to each of the nodes in your hierarchy:
The problem is that you will have ambiguity with the resolution being behind an Interface. Its not perfect, and I dont like the lack of formal language notation (i.e. determinism of the tree), but it might be a place to start.