如何调用事件访问器? [英] How to call event accessor?

查看:57
本文介绍了如何调用事件访问器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个未完成的代码用于调用事件访问器,除了调用它之外它几乎完成了。希望它可以填补。

I have a uncompleted code for calling event accessor, it is almost finished except to call it. Hopefully it can be filled.

class Program
    {
        static void Main(string[] args)
        {
            Pub p = new Pub();
            p.Raise();
        }
    }
    public class Pub
    {
        public event EventHandler<MyArgs> onChange = delegate { };
        public event EventHandler<MyArgs> OnChange
        {
            add
            {
                lock(onChange)
                {
                    onChange += value;
                }
            }
            remove
            {
                lock (onChange)
                {
                    onChange -= value;
                }
            }
        }
        public void Raise()
        {
            onChange(this, new MyArgs(42));
        }
    }
    public class MyArgs : EventArgs
    {
        public MyArgs(int value)
        {
            Value = value;
        }
        public int Value { get; set; }
    }

推荐答案

这是我在教学中使用的替代版本:
Here's an alternate version that I use in teaching:
using System;

namespace Code_Samples
{
    public class EventDemo
    {
        // custom EventArgs
        public class EventDemoArgs : EventArgs
        {
            public int someInt { private set; get; }
            public string someString { private set; get; }

            public EventDemoArgs(int anint, string astring)
            {
                someInt = anint;
                someString = astring;
            }
        }

        // private Properties
        private int SomeInt {  set; get; }

        private string SomeString { set; get; }

        // ctor
        public EventDemo(int anint, string astring)
        {
            SomeInt = anint;
            SomeString = astring;
        }

        // shortcut for Delegate syntax in the case of an Event with custom data
        
        /* MSDN: The EventHandler<TEventArgs> delegate is a predefined delegate that
        represents an event handler method for an event that generates data.
        The advantage of usingEventHandler<TEventArgs> is that you do not need
        to code your own custom delegate if your event generates event data.
        You simply provide the type of the event data object as the generic parameter.*/
        
        public event EventHandler<EventDemoArgs> DemoEvent;

        // void method used to test firing Event
        public void TestEvent()
        {
           OnDemoEvent(new EventDemoArgs(SomeInt, SomeString));
        }

        // virtual method to dispatch the Event to all subscribed handlers
        protected virtual void OnDemoEvent(EventDemoArgs e)
        {
            EventHandler<EventDemoArgs> handler = DemoEvent;
            
            if (handler != null) handler(this, e);
        }
    }
}

示例测试用途:

public void Test()
{
    Code_Samples.EventDemo eDemo = new Code_Samples.EventDemo(999, "test event demo");
    
    // test with multiple handlers
    eDemo.DemoEvent += eDemo_DemoEvent1;   
    eDemo.DemoEvent += eDemo_DemoEvent2;
    
    // raise Test Event on class instance
    eDemo.TestEvent();
}

private void eDemo_DemoEvent1(object sender, Code_Samples.EventDemo.EventDemoArgs e)
{
    MessageBox.Show(string.Format("handler 1: {0} {1}", e.someString, e.someInt));
}

private void eDemo_DemoEvent2(object sender, Code_Samples.EventDemo.EventDemoArgs e)
{
    MessageBox.Show("handler 2 ...");
}


不要将 onChange 标记为 event 。如果您将其标记为事件,则无法在尝试时调用它。此外,添加事件处理程序时不必使用 lock 。如果 OnChange 是公开的,则不必将 onChange 也公开,除非您需要在其他位置使用它(但我不认为你这样做,因为你已经有了OnChange)。因此,请将其替换为:

Don't mark onChange as event. If you mark it as event, you cannot call it as you tried. Also, it's not necessary to use lock while adding an event handler. And if OnChange is public, it's not necessary to make onChange also public, unless you need to use it at another location (but I don't think you do, because you already have OnChange). So, replace it with this:
private EventHandler<MyArgs> onChange;
public event EventHandler<MyArgs> OnChange
{
    add
    {
        onChange += value;
    }
    remove
    {
        onChange -= value;
    }
}



并称之为:


And to call it:

public void Raise()
{
    if (onChange != null)
    {
        onChange(this, new MyArgs(42));
    }
}


这篇关于如何调用事件访问器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆