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.
-
6Operators can't be *overridden*. They can be *overloaded*. There's a big difference. – Jon Skeet Aug 09 '09 at 15:11
-
1To be clearer, override lets you redefine the implementation, overloading lets you add another method with the same name but with different parameters. Thanks for the remark. – Sébastien Ros - MSFT Aug 12 '09 at 08:24
-
Replied to comment and added a non-typed example. – Marc Gravell Aug 12 '09 at 08:27
5 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));
}

- 146,575
- 32
- 257
- 321

- 141
- 1
- 2
-
This worked beautifully in my application. First time I've really used "dynamic" too. I like it! – Boinst Jun 21 '13 at 05:19
-
1
-
Yes, the custom operators are invokable using reflection (they have special names, such as op_Addition), but System.Int32 doesn't define them, as fundamental, built-in, types are handled directly by IL opcodes like add
, rather than method calls.

- 41,404
- 5
- 117
- 189
-
1
-
1Answer own subquestion - turns out they are "op_Implicit" and "op_Explicit" and can have multiple versions with same name, differing only in return type. – Tim Lovell-Smith Aug 03 '10 at 23:42
What exactly is it you want to do? Dealing with the various meanings of operators (primitive (mapped to specific IL instructions), custom (mapped to static methods), and lifted (provided as a pattern by the compiler)) makes this painful. If you just want to use the operators, then it is possible to write code that provides operator support via generics. I have some code for this that is freely available in MiscUtil (description and examples).
As an untyped example (an note that this isn't hugely efficient, but works):
object x = 123, y = 345; // now forget that we know that these are ints...
object result = Expression.Lambda<Func<object>>(
Expression.Convert(Expression.Add(
Expression.Constant(x), Expression.Constant(y)),
typeof(object))).Compile()();

- 1,026,079
- 266
- 2,566
- 2,900
-
Actually I wanted to implement an Add(object left, object right) where the returned value is using the type of the left argument. This is for an expression evaluation, and as it is dynamic there is no way to write the code directly. – Sébastien Ros - MSFT Aug 12 '09 at 08:21
-
have to disagree on opting for 4.0 as Seb's work is focused and quite a performance ahievement when compared to Expression Trees bloat and invention. Judging the performance of DLR and its containing bits it only makes the imminent PDC realease the slowest framework in the history of mankind.. – rama-jka toti Oct 14 '09 at 08:26
-
The DLR solved my problem. I tried the lambdas on two Enum types and didn't work, since Linq Expressions use reflection, and the Enum operators are not declared in code, rather by assembly compiled code or something who knows. – Shimmy Weitzhandler Jan 12 '12 at 01:47
It would be very inefficient if adding or comparing two integers required a method call so these simple operations on the fundamental types are code-generated as explained in another answer and cannot be invoked using reflection. One interesting built-in value type is decimal
(or System.Decimal
). This struct
has support for literal values in C#, but behaves a lot like a real value type and exposes a lot of operators that can be invoked via reflection.
If you are curious you can use Reflector to browse all members exposed by System.Int32
.

- 104,481
- 22
- 209
- 256
There are good answers here, so just to add something not mentioned yet:
A struct might have an operator overloaded. This means more options to check if you try to create some programmatic approach.
One nice thing to try out is, to try the expression tree approach. Here's a small sample. Of course, performance isn't too nice, but we know what we're getting into with Reflection, anyway.

- 13,955
- 3
- 43
- 48