问题描述
在C#中,如果用户定义的转换运算符不明确,是否可以从调用站点中选择其中之一?
请考虑以下示例(类似于Ambiguous implicit user defined conversions in .NET和Ambiguous user defined conversions,when converting from X to Y. C# .Net中的示例)
using System;
static class Program {
class A {
static public explicit operator A(B value) {
Console.WriteLine("A");
return new A();
}
}
class B {
static public explicit operator A(B value) {
Console.WriteLine("B");
return new A();
}
}
public static void Main() {
A a = (A)new B();
}
}
If one of the operators is commented out,the code will compile。这是一个理论问题(实际的解决方案是不要有模棱两可的转换运算符),但是在调用站点是否有一些特殊的语法来专门调用其中一个(通过编辑(A)new B()
)?还是不可能并强迫改写类?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)