2017-09-24 3 views
1

Je transfère une bibliothèque externe dans ma bibliothèque, mais elle est en cours de développement dans un environnement UWP. apparemment, il n'y a pas Delegate.Clone pour Windows 10, comment puis-je obtenir la même fonctionnalité, y at-il des solutions de contournement pour cela?Delegate.Clone dans UWP

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

Est-ce correct?

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

Répondre

2

Vous pouvez utiliser le Combine method statique pour y parvenir:

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); 
} 

Sortie:

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