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# 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 │ 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 │ │
└──────────────────────────┴───────────────────────┴──────────────────────────┘
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 );
op_Explicit
and op_Implicit
(I think these names are self-explanatory). Remember though that multiple casting operators can be defined, so one will need to narrow down the search by specifying either parameter type or return type (in respect to the 'direction' of cast). –
Mcdougall ~
? –
Remanent typeof(A).GetMethod("op_Addition").Invoke(null, instance1, instance2);
type.GetMethod("op_Subtraction").Invoke(null, new object[] { instance1, instance2 });
–
Wabash 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;
Adding the indexer operator []
to the accepted answer.
There is a difference between non array types and array types:
Non array types
For Non array types, it is implemented as a public instance property named Item
that takes an extra argument for the index (something that is allowed in general IL and VB.Net but not in C#).
In other words, when calling SetValue
or GetValue
on the PropertyInfo
(returned by GetProperty()
) you have to use the overload that also takes an index.
Similarly when trying to use the underlying get
and set
methods you need to remember that their signature has one more argument compared to standard properties.
Even when using the indexer from VB.Net code you have the use the .Item
property as the indexer operator is not available in VB.Net.
Array Types
Array types do not have the above Item
property (besides as an explicit implementation for IList
) instead there are two public methods called Get
and Set
(note that these are only available on the actual array type not the base System.Array
type, however there are other GetValue()
and SetValue()
methods available there).
© 2022 - 2024 — McMap. All rights reserved.
op_Addition
method with the same signature? – Dockery