29

在我的小项目中,我使用System.Reflection类来生成可执行代码。我需要调用+自定义类型的运算符。有人知道如何使用 C# 反射调用自定义类的自定义运算符吗?

4

3 回答 3

42

C# 编译器将重载的运算符转换为名称为op_XXXXwhere的函数XXXX。例如,operator +编译为op_Addition.

以下是可重载运算符及其各自方法名称的完整列表:

┌──────────────────────────┬───────────────────────┬──────────────────────────┐
│         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              │                          │
└──────────────────────────┴───────────────────────┴──────────────────────────┘

因此,要检索结构的operator+方法DateTime,您需要编写:

MethodInfo mi = typeof(DateTime).GetMethod("op_Addition",
    BindingFlags.Static | BindingFlags.Public );
于 2012-06-20T05:43:13.800 回答
7
typeof(A).GetMethod("op_Addition").Invoke(null, instance1, instance2);
于 2012-06-20T05:37:59.840 回答
0

考虑将您的自定义运算符设为property您的Class. 然后访问property及其value通过reflection

PropertyInfo pinfo = obj.GetType().GetProperty("CustomOperator", BindingFlags.Public | BindingFlags.Instance);
string customOperator = pinfo.GetValue(obj,null) as string;
于 2012-06-20T05:32:49.690 回答