简体   繁体   English

event 关键字的真正作用是什么?

[英]What does the event keyword really do?

public delegate void SecondChangedHandler(
        object clock, 
        TimeInfoEventArgs timeInformation);

    public event SecondChangedHandler SecondChanged;

I have written a clock based on thisarticle .我根据这篇文章写了一个时钟。 Now if i remove the event keyword i get the same result, so what does event really do?现在,如果我删除 event 关键字,我会得到相同的结果,那么 event 到底做了什么?

It's compiled differently.它的编译方式不同。 It makes it so someone can't do它让别人做不到

mySecondChangedHandler.SecondChanged = SomeMethod(...); //overwrite
mySecondChangedHandler.SecondChanged(...); //invoke

but only但只有

mySecondChangedHandler.SecondChanged += SomeMethod(...);
mySecondChangedHandler.SecondChanged -= SomeMethod(...);

The event keyword creates a private delegate field, and a pair of public event accessors called add_EventName and remove_EventName . event关键字创建一个私有委托字段,以及一对称为add_EventNameremove_EventName的公共事件访问器。 ( details ) 详情

This means that writing EventName inside the class returns the delegate instance, allowing you to call or inspect the event handlers.这意味着在 class 中写入EventName会返回委托实例,从而允许您调用或检查事件处理程序。

Outside the class, EventName doesn't really exist;在 class 之外, EventName并不真正存在; all you can do is write EventName += something and EventName -= something , and the compiler will convert it into calls to the accessors.您所能做的就是编写EventName += somethingEventName -= something ,编译器会将其转换为对访问器的调用。 (like a property) (如财产)

For more details, see this series of blog posts .有关更多详细信息,请参阅此系列博客文章

The event keyword does two things event 关键字做了两件事

  • It supplied permissions.它提供了权限。 Only the class can raise the event, however any external method can invoke the raw delegate只有 class 可以引发事件,但是任何外部方法都可以调用原始委托
  • It provides metadata which can be used for designers and the like它提供了可供设计师等使用的元数据

The event keywords means only methods on the instance that hosts the SecondChanged field can invoke it. event关键字意味着只有承载 SecondChanged 字段的实例上的方法才能调用它。 External attempts will fail.外部尝试将失败。

The event keyword creates a pair of accessors for a delegate. event关键字为委托创建一对访问器 These are effectively two methods ( add and remove ) that are called when you subscribe or unsubscribe from the event.这实际上是订阅取消订阅事件时调用的两种方法( addremove )。

In your case, you're creating a "field-like event".在您的情况下,您正在创建一个“类似字段的事件”。 The compiler makes a delegate behind the scenes, and allows you to subscribe and unsubscribe from it's invocation list.编译器在幕后创建一个委托,并允许您订阅和取消订阅它的调用列表。 This means that you have all of the functionality of a delegate, but you're restricting access so that that outside world can "handle" the event, but not raise the event (invoke the delegate).这意味着您拥有委托的所有功能,但您正在限制访问,以便外部世界可以“处理”事件,但不能引发事件(调用委托)。

You can, however, also explicitly create your own accessors for an event, and these can do other things (though that's not typically recommended unless there is a good reason to do so).但是,您也可以为事件显式创建自己的访问器,这些访问器可以做其他事情(尽管通常不建议这样做,除非有充分的理由这样做)。

Quote from C# lang reference about keyword event ,引用C# 语言参考关于关键字event

Events are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared (the publisher class).事件是一种特殊的多播委托,只能从 class 或声明它们的结构(发布者类)中调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM