我正嘗試在我的應用程序中使用自定義運算符進行自定義類的運算,該運算符與ClearScript接口。下面是我的榜樣自定義類的一個片段:在ClearScript中重載操作符
public class Vector3 {
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
public Vector3(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public static Vector3 operator +(Vector3 a, Vector3 b) {
return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
}
}
我ClearScript引擎正確初始化,並且我能正確使用JavaScript初始化Vector3
對象,並修改相應的屬性。但是,如果我在Javascript環境中初始化2個Vector3
對象,並嘗試使用Javascript添加運算符,它最終會將添加運算符評估爲字符串連接,而不是我的自定義運算符。
實施例:
var a = new Vector3(1, 1, 1);
var b = new Vector3(0, 2, -1);
var c = a + b;
print(typeof a); //returns "function" (which is correct)
print(typeof b); //returns "function" (which is also correct)
print(typeof c); //returns "string" (should return function)
可變c
僅包含string
([object HostObject][object HostObject]
),代替Vector3
對象。
我該如何讓JavaScript引擎知道調用我的自定義運算符而不是使用使用ClearScript的默認Javascript運算符?
Darn,這就是我認爲的情況,但我希望可能在ClearScript的一邊做了一些奇怪的反射來使我的用例成爲可能。我會用你的解決方案,謝謝! –