简体   繁体   中英

C# dynamic casting during operator evaluation

I currently have a user defined type

class MyType {
    double x;
    double y;
    double z;

    //This class has operator overloading implemented
    public static MyType operator + (double a,MyType b){
        ...
    }
}

At some point later I will have an array (object[]) some of them double and some MyType. I would like to do evaluation over them but compiler is not allowing operator '+' to be applied to type object and object. What should I do here?

Honestly, I would create a MyType constructor that took a single double parameter, and use that to convert each double to a MyType , so that you could just use a MyType[] (or List<MyType> ).

I understand that's really not what you're asking, but you'd still be able to handle the MyType creation such that the addition will perform properly, and you'd be more type-safe with your array.

When resolving a + in code the compiler needs to bind it to a specific operation. The type object has no + operation and the compiler has no idea that the underlying types are double and MyType and hence the compiler errors out.

In order to fix this you will need to either

  • statically convert the elements to double and MyType so the compiler can properly bind the + operator
  • use dynamic and rely on the runtime binding to the correct operator+ .

Here is an example of the latter method.

class MyType {
    double _value;

    internal MyType(double value) {
        _value = value;
    }

    public static MyType operator +(double left, MyType right) {
        return new MyType(left + right._value);
    }

    public static MyType operator +(MyType left, double right) {
        return new MyType(left._value + right);
    }

    public static MyType operator +(MyType left, MyType right) {
        return new MyType(left._value + right._value);
    }

    public static implicit operator MyType(double other) {
        return new MyType(other);
    }
}

class Program
{
    static void Main(string[] args) {
        object[] them = new object[] {
            42,
            new MyType(13),
            12,
            new MyType(12)
        };

        dynamic all = them;
        MyType sum = 0;
        foreach (dynamic element in all) {
            sum += element;
        }

        Console.WriteLine(sum);
    }
}

You should be more specific with your types - the reason you cannot add is because object could be anyting in the system.

Is there any way that you can change object for a more specific type?

If it absolutely has to be object then you will have to evaluate at run-time using casts or the as operator or some similar mechanism.

eg

    public static MyType operator +(object o, MyType b)
    {
        MyOtherType mot = o as MyOtherType;
        if(o!=null)
            ...

but be in no doubt that your solution here raises questions about your design choices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM