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 IConvert开发者_运维百科ible
, 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
)
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. Solution for overloaded operator constraint in .NET generics
精彩评论