2017-09-24 31 views
1

我將一個外部庫移植到我的庫中,但它正在UWP環境下開發。顯然沒有Delegate.Clone爲Windows 10,我怎麼能實現相同的功能,有沒有任何解決方法呢?UWP中的Delegate.Clone

_listChanging.Clone() // list changing is instance of some delegate. 
         // unfortunately this method does not exist in UWP 

這是正確的嗎?

_listChanging.GetMethodInfo().CreateDelegate(_listChanging.GetType(), _listChanging.Target); 

回答

2

可以使用靜態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