开发者

How to check if two types can be compared, summed etc.?

开发者 https://www.devze.com 2022-12-24 08:54 出处:网络
if given two types (Type a, Type b), is there any \"nice\" way to find out if those two can be compared, summed etc.?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消