加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 这是通用委托和事件的正确语法吗?

发布时间:2020-12-16 01:38:16 所属栏目:百科 来源:网络整理
导读:我正在阅读有关 genrics的msdn库主题.有一个用泛型委托声明事件的例子,但它是否正确? // Code block 8. Generic event handlingpublic delegate void GenericEventHandlerS,A(S sender,A args);public class MyPublisher{ public event GenericEventHandler
我正在阅读有关 genrics的msdn库主题.有一个用泛型委托声明事件的例子,但它是否正确?

// Code block 8. Generic event handling

public delegate void GenericEventHandler<S,A>(S sender,A args);
public class MyPublisher
{
   public event GenericEventHandler<MyPublisher,EventArgs> MyEvent;
   public void FireEvent()
   {
      MyEvent(this,EventArgs.Empty);
   }
}
public class MySubscriber<A> //Optional: can be a specific type
{
   public void SomeMethod(MyPublisher sender,A args)
   {...}
}
MyPublisher publisher = new MyPublisher();
MySubscriber<EventArgs> subscriber = new MySubscriber<EventArgs>();
publisher.MyEvent += subscriber.SomeMethod;  // is this line correct?

我们可以直接将方法应用于事件,而不是先与我们的代表一起包装吗?

解决方法

是的,这是C#2.0中的新功能,它将为您创建委托.请注意,您仍在创建委托,但创建是不可见的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读