Bất cứ ai có thể cho tôi biết tại sao mã này hoạt động theo cách đó? Xem các nhận xét được nhúng trong mã ...Gọi() và BeginInvoke() hoạt động khác khi thực hiện một phương thức có thể ghi đè thông qua một đại biểu
Tôi có thiếu thứ gì đó thực sự rõ ràng ở đây không?
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;
}
}
}
tôi đã không cố gắng này, hoặc đã biết có một vấn đề, nhưng tôi có thể nhìn thấy rất nhiều vấn đề đến từ này. Có lẽ ai đó có thể giải thích :) – leppie