views:

76

answers:

2

Hi, if given two types (Type a, Type b), is there any "nice" way to find out if those two can be compared, summed etc.?

I was thinking if the types implement IConvertible, one could convert both to lets say decimal and perform a "Convert.ToDecimal(a) > Convert.ToDecimal(b)" ?

I am building an expression evaluator and want to be able to work with any kind of object and thus need to know if a type can be compared to another type (it DOESN'T have to be the same types on both sides. eg. double > int)

A: 

Here is my question on the same problem.
http://stackoverflow.com/questions/2093230/how-to-check-that-i-can-sum-values-of-given-type

Sergey Mirvoda
+1  A: 

Wether you can add to types depends on the binding rules of your programming language. Specifically implicit conversions and overload resolution.

Since .net 3.5 there the class "Expression" and related helper classes which can do that. http://stackoverflow.com/questions/147646/solution-for-overloaded-operator-constraint-in-net-generics

Winner