Delegate 与 MulticastDelegate 委托的单波与多波模式
2011-07-21 08:44
323 查看
class TestDelegate
{
public delegate void EventHanding();
public event EventHanding OnFireEvnet;
protected void RegistEvent()
{
Delegate d1 = new EventHanding(test1);
Delegate d2 = new EventHanding(test2);
Delegate d3 = new EventHanding(test3);
Delegate d4 = new EventHanding(test4);
Delegate newEvent1 = Delegate.Combine(d1, d2);
Delegate newEvent2 = Delegate.Combine(d3, d4);
//新合并的委托是一个新的对象
if (Object.ReferenceEquals(d2, newEvent1))
{
string str = string.Empty;
}
if (d2.Method.ToString().CompareTo(newEvent1.Method.ToString())==0)
{
string str = string.Empty;
}
EventHanding newEvent = (EventHanding)Delegate.Combine(newEvent1, newEvent2);
OnFireEvnet += newEvent;
}
protected void FireEvent()
{
//单波
MulticastDelegate del = (MulticastDelegate)OnFireEvnet;
del.Method.Invoke(this, null);
//多波
OnFireEvnet();
}
int i = 0;
public void test1()
{
i++;
}
public void test2()
{
i++;
}
public void test3()
{
i++;
}
public void test4()
{
i++;
}
public static void Test()
{
TestDelegate test = new TestDelegate();
test.RegistEvent();
test.FireEvent();
}
}
{
public delegate void EventHanding();
public event EventHanding OnFireEvnet;
protected void RegistEvent()
{
Delegate d1 = new EventHanding(test1);
Delegate d2 = new EventHanding(test2);
Delegate d3 = new EventHanding(test3);
Delegate d4 = new EventHanding(test4);
Delegate newEvent1 = Delegate.Combine(d1, d2);
Delegate newEvent2 = Delegate.Combine(d3, d4);
//新合并的委托是一个新的对象
if (Object.ReferenceEquals(d2, newEvent1))
{
string str = string.Empty;
}
if (d2.Method.ToString().CompareTo(newEvent1.Method.ToString())==0)
{
string str = string.Empty;
}
EventHanding newEvent = (EventHanding)Delegate.Combine(newEvent1, newEvent2);
OnFireEvnet += newEvent;
}
protected void FireEvent()
{
//单波
MulticastDelegate del = (MulticastDelegate)OnFireEvnet;
del.Method.Invoke(this, null);
//多波
OnFireEvnet();
}
int i = 0;
public void test1()
{
i++;
}
public void test2()
{
i++;
}
public void test3()
{
i++;
}
public void test4()
{
i++;
}
public static void Test()
{
TestDelegate test = new TestDelegate();
test.RegistEvent();
test.FireEvent();
}
}
相关文章推荐
- Delegate 与 MulticastDelegate 委托的单波与多波模式
- Delegate 与 MulticastDelegate 委托的单波与多波模式
- IOS-设计模式之委托(delegate)
- 类的继承,类的密封sealed,类的抽象abstrac,virtual虚化和复写,oveload重载,delegate委托,单例模式
- 如何理解 iOS Delegate (委托)设计模式
- Strategy模式与Delegate委托
- 委托(delegate)模式
- 观察者模式(observer)之委托(delegate) c#简单例子
- 委托模式-Delegate
- iOS监听模式系列之关于delegate(代理,委托)的学习
- 基于C++的多播委托(MulticastDelegate)
- 使用蓝图(Blueprint)绑定多播委托(Multicast Delegate)【UE4】【C++】
- iOS 多播委托(GCDMulticastDelegate)
- 委托模式( delegate)
- iOS设计模式——委托(delegate)
- 如何理解 iOS Delegate (委托)设计模式 .
- iOS设计模式——委托(delegate)
- Objective_C的语言特性,以及委托模式delegate
- Unity3D 利用C#实现简单的代理模式Delegate(委托)
- Observer模式,delegate(委托)模式