对于 C# 中对非静态隐式运算符的缺乏支持,有没有人有一个雄辩的解决方案?以下代码显示了我当前的问题:
class Foo
{
public int x { get; set; }
public int y { get; set; }
public Foo()
{
}
public static implicit operator Foo(Bar b)
{
Foo newFoo = new Foo();
newFoo.y = b.y;
return newFoo;
}
}
class Bar
{
public int y { get; set; }
public Bar()
{
}
}
Foo foo = new Foo();
foo.x = 42;
Bar bar = new Bar();
bar.y = 52;
foo = bar;
Console.WriteLine(foo.x); // THIS PRINTS 0
这是我正在寻找的功能:
public implicit operator Foo(Bar b)
{
this.y = b.y;
}