Delegate.Clone 在 UWP 中
Delegate.Clone in UWP
我正在将一个外部库移植到我的库中,但它是在 UWP 环境下开发的。显然 windows 10 没有 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);
您可以使用静态 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
我正在将一个外部库移植到我的库中,但它是在 UWP 环境下开发的。显然 windows 10 没有 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);
您可以使用静态 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