Search code examples
c#reflectionoperator-overloadingoperatorssystem.reflection

How to call custom operator with Reflection


In my small project I'm using System.Reflection classes to produce executable code. I need to call the + operator of a custom type. Does anybody know how can I call customized operator of custom class using C# reflection?


Solution

  • C# compiler converts overloaded operator to functions with name op_XXXX where XXXX is the operation. For example, operator + is compiled as op_Addition.

    Here is the full list of overloadable operators and their respective method names:

    ┌──────────────────────────┬───────────────────────┬──────────────────────────┐
    │         Operator         │      Method Name      │       Description        │
    ├──────────────────────────┼───────────────────────┼──────────────────────────┤
    │ operator +               │ op_UnaryPlus          │ Unary                    │
    │ operator -               │ op_UnaryNegation      │ Unary                    │
    │ operator ++              │ op_Increment          │ Unary                    │
    │ operator --              │ op_Decrement          │ Unary                    │
    │ operator !               │ op_LogicalNot         │ Unary                    │
    │ operator +               │ op_Addition           │                          │
    │ operator -               │ op_Subtraction        │                          │
    │ operator *               │ op_Multiply           │                          │
    │ operator /               │ op_Division           │                          │
    │ operator &               │ op_BitwiseAnd         │                          │
    │ operator |               │ op_BitwiseOr          │                          │
    │ operator ^               │ op_ExclusiveOr        │                          │
    │ operator ~               │ op_OnesComplement     │ Unary                    │
    │ operator ==              │ op_Equality           │                          │
    │ operator !=              │ op_Inequality         │                          │
    │ operator <               │ op_LessThan           │                          │
    │ operator >               │ op_GreaterThan        │                          │
    │ operator <=              │ op_LessThanOrEqual    │                          │
    │ operator >=              │ op_GreaterThanOrEqual │                          │
    │ operator <<              │ op_LeftShift          │                          │
    │ operator >>              │ op_RightShift         │                          │
    │ operator %               │ op_Modulus            │                          │
    │ implicit operator <type> │ op_Implicit           │ Implicit type conversion │
    │ explicit operator <type> │ op_Explicit           │ Explicit type conversion │
    │ operator true            │ op_True               │                          │
    │ operator false           │ op_False              │                          │
    └──────────────────────────┴───────────────────────┴──────────────────────────┘
    

    So to retrieve the operator+ method of the DateTime struct, you need to write:

    MethodInfo mi = typeof(DateTime).GetMethod("op_Addition",
        BindingFlags.Static | BindingFlags.Public );