C#代理

来源:互联网 发布:淘宝怎么下架宝贝 编辑:程序博客网 时间:2024/05/22 12:22
代理:
C#中的一种类型,类似于c++中的函数指针,但是更安全,因为定义一个代理,需要定义代理类型,
即用返回值和参数来确定这个代理的类型。
C#中的代理支持多播,即可以有一个或多个方法注册这个代理


class A
{
public delegate void Void_Void ();


private Void_Void m_paySucCallBack;

public void RegisterPaymentSuc(Void_Void callBack)
{
m_paySucCallBack += callBack;
}


public void UnRegisterPaymentSuc(Void_Void callBack)
{
m_paySucCallBack -= callBack;
}


if(m_paySucCallBack != null)
{
m_paySucCallBack();//当调用这个方法的时候会通知监听它的所有对象即会调用OnPaymentSuc方法
   
}
}


class B
{
PaymentManager.Instance.RegisterPaymentSuc(OnPaymentSuc);


PaymentManager.Instance.UnRegisterPaymentSuc(OnPaymentSuc);

private void OnPaymentSuc()
{
m_vipLevel.text = VipManager.Instance.CurPlayerVipLevel.ToString();//玩家的vip等级
}
}
0 0