[英]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. 稍后,我将有一个数组(object []),其中一些加倍,另一些是MyType。 I would like to do evaluation over them but compiler is not allowing operator '+' to be applied to type object and object.
我想对它们进行评估,但编译器不允许将运算符'+'应用于类型object和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>
). 老实说,我将创建一个
MyType
构造函数,该构造函数采用单个double
参数,并将其用于将每个double
转换为MyType
,以便您可以仅使用MyType[]
(或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. 我知道这实际上不是您要的内容,但是您仍然可以处理
MyType
创建,以便添加操作可以正确执行,并且对数组更安全。
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. 类型
object
没有+
操作,编译器也不知道基础类型是double
和MyType
,因此编译器会出错。
In order to fix this you will need to either 为了解决这个问题,您将需要
double
and MyType
so the compiler can properly bind the +
operator double
和MyType
以便编译器可以正确绑定+
运算符 dynamic
and rely on the runtime binding to the correct operator+
. dynamic
并依赖于运行时绑定到正确的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. 如果绝对必须是对象,那么您将必须在运行时使用强制转换或as运算符或某种类似的机制进行评估。
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 但毫无疑问,这里的解决方案会引发有关设计选择的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.