Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call value type operators via reflection?

Tags:

c#

reflection

As C# operators e.g. +, +=, == are overridable. It lets me think they are sort of methods, thus wonder if there is a way to call them using reflection, on Int32 for instance.

like image 369
Sébastien Ros - MSFT Avatar asked Aug 09 '09 14:08

Sébastien Ros - MSFT


1 Answers

What about this, it's simple, small and works :)

public T Add<T>(object x, object y)
{
    return (T)Convert.ChangeType((dynamic)x + (dynamic)y, typeof(T));
}
like image 98
FdW Avatar answered Oct 12 '22 17:10

FdW