任何人都可以告訴我爲什麼這個代碼的行爲方式如此嗎?查看嵌入代碼中的註釋...當通過代理執行可覆蓋的方法時,Invoke()和BeginInvoke()的行爲有所不同
我在這裏錯過了一些非常明顯的東西嗎?
using System;
namespace ConsoleApplication3
{
public class Program
{
static void Main(string[] args)
{
var c = new MyChild();
c.X();
Console.ReadLine();
}
}
public class MyParent
{
public virtual void X()
{
Console.WriteLine("Executing MyParent");
}
}
delegate void MyDelegate();
public class MyChild : MyParent
{
public override void X()
{
Console.WriteLine("Executing MyChild");
MyDelegate md = base.X;
// The following two calls look like they should behave the same,
// but they behave differently!
// Why does Invoke() call the base class as expected here...
md.Invoke();
// ... and yet BeginInvoke() performs a recursive call within
// this child class and not call the base class?
md.BeginInvoke(CallBack, null);
}
public void CallBack(IAsyncResult iAsyncResult)
{
return;
}
}
}
我沒有嘗試這樣做,或者知道有問題,但我可以看到很多從這個未來的問題。也許有人可以解釋:) – leppie 2008-10-23 12:22:39