"Power" operator can not be overridden.

Mar 26, 2009 at 9:26 PM

I have created my own class to handle operations with complex numbers, but I have no idea how to override ^ operatior since it has been linked to Math.Pow method that doesn't accept anything except doubles.

In my opinion, the ArithmeticElement should try to find any suitable "power" method before it raises an exception.

Mar 27, 2009 at 2:24 AM
Edited Mar 27, 2009 at 2:25 AM
Flee supports an overloaded power operator.  The problem is that overloading that operator is only supported by VB.NET and IL; I don't know how you'd overload it in C#.

For example, the following expression will compile:
Public Class Complex
Public Shared Operator ^(ByVal a As Complex, ByVal b As Complex) As Integer
Return 100
End Operator
End Class

Dim context As New ExpressionContext()
context.Variables.Add("a", New Complex())
context.Variables.Add("b", New Complex())
Dim e As IDynamicExpression = context.CompileDynamic("a ^ b")
Mar 27, 2009 at 3:04 AM
Actually, it turns out there's an indirect way to define an overloaded power operator in C#.  The key is to put the SpecialName attribute onto a correctly named method:
public class Complex
static void Main(string[] args)
ExpressionContext context = new ExpressionContext();
context.Variables.Add("a", new Complex());
context.Variables.Add("b", new Complex());

IDynamicExpression e = context.CompileDynamic("a ^ b");
object result = e.Evaluate();

public static int op_Exponent(Complex a, Complex b)
return 100;
Mar 27, 2009 at 8:44 AM
Thanks, Eugene!