32

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?

3 Answers 3

47

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 );
9
  • 2
    Just curious :) What if I have a static op_Addition method with the same signature? Nov 3, 2012 at 8:33
  • 1
    @ŞafakGür Then you'll get a compiler error saying "Type '<YourTypeName>' already defines a member called 'op_Addition' with the same parameter types". Because the defined operator method is exactly renamed to aforementioned name, you cannot have both in the same class. Nov 3, 2012 at 15:23
  • 1
    I've managed to find it out myself, but for future reference: casting operators are named op_Explicit and op_Implicit (I think these names are self-explanatory). Remember though that multiple casting operators can be defined, so one will need to narrow down the search by specifying either parameter type or return type (in respect to the 'direction' of cast).
    – Grx70
    Jan 24, 2015 at 9:27
  • 1
    Did you forgot ~ ?
    – Denis535
    Sep 3, 2018 at 18:55
  • 1
    @ChristabellaIrwanto I remember that I first decompiled a C# code, and later looked up the actual code in the C# compiler. This is not documented anywhere by MS. Nov 30, 2020 at 13:44
7
typeof(A).GetMethod("op_Addition").Invoke(null, instance1, instance2);
1
  • by me, parameters needs to be given via an array type.GetMethod("op_Subtraction").Invoke(null, new object[] { instance1, instance2 }); Jul 27, 2019 at 23:29
0

Consider to make your customized operator as property of your Class. And then access the property and its value through reflection.

like

PropertyInfo pinfo = obj.GetType().GetProperty("CustomOperator", BindingFlags.Public | BindingFlags.Instance);
string customOperator = pinfo.GetValue(obj,null) as string;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.