可以使用靜態Combine
method來實現這一目標:
delegate void MyDelegate(string s);
event MyDelegate TestEvent;
private void TestCloning()
{
TestEvent += Testing;
TestEvent += Testing2;
var eventClone = (MyDelegate)Delegate.Combine(TestEvent.GetInvocationList());
TestEvent("original");
eventClone("clone");
Debug.WriteLine("Removing handler from original...");
TestEvent -= Testing2;
TestEvent("original");
eventClone("clone");
}
private void Testing2(string s)
{
Debug.WriteLine("Testing2 was called with {0}", s);
}
void Testing(string s)
{
Debug.WriteLine("Testing was called with {0}", s);
}
輸出:
Testing was called with original
Testing2 was called with original
Testing was called with clone
Testing2 was called with clone
Removing handler from original...
Testing was called with original
Testing was called with clone
Testing2 was called with clone