How to call custom operator with Reflection

后端 未结 3 643
温柔的废话
温柔的废话 2020-12-05 23:36

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 k

相关标签:
3条回答
  • 2020-12-06 00:24

    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;
    
    0 讨论(0)
  • 2020-12-06 00:29

    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 );
    
    0 讨论(0)
  • 2020-12-06 00:33
    typeof(A).GetMethod("op_Addition").Invoke(null, instance1, instance2);
    
    0 讨论(0)
提交回复
热议问题