2
有沒有人對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;
}
'+ ='選項很有趣,但在這種情況下它仍然相當混亂,因爲你可能直覺地期望它將兩個y值相加。 –
@丹Bryant:是的,這也不是我的第一選擇,說實話。我喜歡'MergeFrom'的顯式性質。 –