zoukankan      html  css  js  c++  java
  • 事件访问器

    事件声明通常省略事件访问器声明。但会有一些特殊情况,例如,为每个事件设置一个字段所造成的内存开销,有时会变得不可接受。在这种情况下,可以在类中使用事件访问器声明,并采用专用机制来存储事件处理程序列表。

    事件的事件访问器声明指定与添加和移除事件处理程序相关联的可执行语句。

    访问器声明由一个添加访问器声明和一个移除访问器声明组成。每个访问器声明包含标记 add 或 remove,后跟一个块。与添加访问器声明相关联的块指定添加事件处理程序时要执行的语句,而与移除访问器声明相关联的块指定移除事件处理程序时要执行的语句。

    每个添加访问器声明和移除访问器声明相当于一个方法,它具有一个属于事件类型的值参数并且其返回类型为 void。事件访问器的隐式参数名为 value。当事件用在事件赋值中时,就会调用适当的事件访问器。具体说来,如果赋值运算符为 +=,则使用添加访问器,而如果赋值运算符为 -=,则使用移除访问器。在两种情况下,赋值运算符的右操作数都用作事件访问器的参数。添加访问器声明或移除访问器声明的块必须遵循第 10.5.8 节中所描述的适用于 void 方法的规则。具体说来,不允许此类块中的 return 语句指定表达式。

    由于事件访问器隐式具有一个名为 value 的参数,因此在事件访问器中声明的局部变量或常数若使用该名称,就会导致一个编译时错误。

    下面的示例:

    class Control : Component
    {
        // Unique keys for events
        static readonly object mouseDownEventKey = new object();
        static readonly object mouseUpEventKey = new object();
        // Return event handler associated with key
        protected Delegate GetEventHandler(object key) 
        {
            //...
        }
        // Add event handler associated with key
        protected void AddEventHandler(object key, Delegate handler) 
        {
            //...
        }
        // Remove event handler associated with key
        protected void RemoveEventHandler(object key, Delegate handler) 
        {
            //...
        }
        // MouseDown event
        public event MouseEventHandler MouseDown
        {
            add { AddEventHandler(mouseDownEventKey, value); }
            remove { RemoveEventHandler(mouseDownEventKey, value); }
        }
        // MouseUp event
        public event MouseEventHandler MouseUp
        {
            add { AddEventHandler(mouseUpEventKey, value); }
            remove { RemoveEventHandler(mouseUpEventKey, value); }
        }
        // Invoke(调用) the MouseUp event
        protected void OnMouseUp(MouseEventArgs args)
        {
            MouseEventHandler handler;
            handler = (MouseEventHandler)GetEventHandler(mouseUpEventKey);
            if (handler != null)
                handler(this, args);
        }
    
    }

     

    Control 类为事件实现了一个内部存储机制。AddEventHandler 方法将委托值与键关联,GetEventHandler 方法返回当前与键关联的委托,而 RemoveEventHandler 方法将移除一个委托使它不再成为指定事件的一个事件处理程序。可以推断:在这样设计的基础存储机制下,当一个键所关联的委托值为 null 时,不会有存储开销,从而使未处理的事件不占任何存储空间。 

    ===== 

    下面的示例演示一个具有自定义 add 和 remove 访问器的事件。

    //C#
    class Events : IDrawingObject
    {
        event EventHandler PreDrawEvent;
    
        event EventHandler IDrawingObject.OnDraw
        {
            add
            {
                lock (PreDrawEvent)
                {
                    PreDrawEvent += value;
                }
            }
            remove
            {
                lock (PreDrawEvent)
                {
                    PreDrawEvent -= value;
                }
            }
        }
    }

     

    通常不需要提供自己的自定义事件访问器。 在大多数情况下,使用在声明事件时由编译器自动生成的访问器就足够了。

     

  • 相关阅读:
    Stream 和 byte[] 之间的转换
    C# Process类_进程_应用程序域与上下文之间的关系
    C# Process类_进程管理器Demo
    C# attribute_特性
    SqlDataAdapter类
    SqlDataReader类
    SqlCommand类
    SqlConnection类
    DataTable类
    C# 语法技巧_三目运算_switch_case
  • 原文地址:https://www.cnblogs.com/gsk99/p/4988170.html
Copyright © 2011-2022 走看看