Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 234
user35443 Avatar asked Jun 20 '12 05:06

user35443


1 Answers

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          │                          │ │ operator --              │ op_Decrement          │                          │ │ operator !               │ op_LogicalNot         │                          │ │ operator +               │ op_Addition           │                          │ │ operator -               │ op_Subtraction        │                          │ │ operator *               │ op_Multiply           │                          │ │ operator /               │ op_Division           │                          │ │ operator &               │ op_BitwiseAnd         │                          │ │ operator |               │ op_BitwiseOr          │                          │ │ operator ^               │ op_ExclusiveOr        │                          │ │ operator ~               │ op_OnesComplement     │                          │ │ 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 ); 
like image 143
Mohammad Dehghan Avatar answered Oct 11 '22 12:10

Mohammad Dehghan