views:

107

answers:

2

I have an UGLY logic tree that I'm looking at ways replace with some code generated from a table. It branches based on several thing:

  • the length of a List<AbstractType>
  • the actual types in the list
  • a flags enum

My basic idea is to build some kind of decision tree from by input table. For the list length and flags that's easy (a switch), but what about the types bit?

This question suggests that polymorphism is a good idea but that would mix concerns and strew code to the four winds in my case (and doesn't lend its self to generation anyway). The other suggested solution (IDictionary<Type, DelegateType>) might work but seem a little ugly.

Does anyone have any suggestions.

+1  A: 

One way would be to concatenate the type names (separated e.g. by colon), and then switch by string, e.g.

 switch(colon_separated_typenames(list)) {
   case "int:int": //foo
   case "double:String:double": //bar
 }
Martin v. Löwis
Nice and clean +1, but string building and (is suspect) switches are a little on the slow side. With a little more effort up front I think I can get away without any allocations.
BCS
A: 

Why don't you try the ExpressionTree?

Subodh
hyper Link ????
BCS