如何使用反射调用自定义运算符 [英] How to call custom operator with Reflection

查看:22
本文介绍了如何使用反射调用自定义运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

C# 编译器将重载运算符转换为名称为 op_XXXX 的函数,其中 XXXX 是操作.例如,operator + 被编译为 op_Addition.

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

所以要检索DateTime结构体的operator+方法,需要这样写:

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 );

这篇关于如何使用反射调用自定义运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆