如何使用Reflection调用自定义运算符

问题描述 投票:0回答:4

在我的小项目中,我使用

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

c# reflection operator-overloading operators system.reflection
4个回答
50
投票

C# 编译器将重载运算符转换为名称为

op_XXXX
的函数,其中
XXXX
是操作。例如,
operator +
编译为
op_Addition

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

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

因此,要检索

operator+
结构体的
DateTime
方法,您需要编写:

MethodInfo mi = typeof(DateTime).GetMethod("op_Addition",
    BindingFlags.Static | BindingFlags.Public );

8
投票
typeof(A).GetMethod("op_Addition").Invoke(null, instance1, instance2);

0
投票

考虑将您的自定义运算符设为您的

property
Class
。然后通过
property
访问
value
及其
reflection

喜欢

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

0
投票

将索引器运算符

[]
添加到 接受的答案

非数组类型和数组类型之间有区别:

非数组类型

对于非数组类型,它被实现为名为

Item
的公共实例属性,该属性需要一个额外的索引参数(这在一般 IL 和 VB.Net 中是允许的,但在 C# 中不允许)。

换句话说,当在

SetValue
(由
GetValue
返回)上调用
PropertyInfo
GetProperty()
时,您必须使用也带有索引的重载。

同样,当尝试使用底层

get
set
方法时,您需要记住,与标准属性相比,它们的签名多了一个参数。

即使在 VB.Net 代码中使用索引器,您也可以使用

.Item
属性,因为索引器运算符在 VB.Net 中不可用。

数组类型

数组类型没有上述

Item
属性(除了作为
IList
的显式实现之外),而是有两个名为
Get
Set
的公共方法(请注意,这些方法仅适用于实际数组类型,不适用于基本
System.Array
类型,但是还有其他
GetValue()
SetValue()
方法可用)。

© www.soinside.com 2019 - 2024. All rights reserved.