如何在三个类中处理事件

时间:2018-01-08 13:48:18

标签: c# eventhandler

看看我的代码吧。我有一个ThirdClass来解决一个事件。在第二课我处理那个事件。但是如何在我的程序类中处理该事件。在这个类中,我没有订阅事件的ThirdClass对象。我是否必须在第二类中声明另一个事件为了分解MyPurpose()方法?

public class Program
{        
    static void Main(string[] ars)
    {
        Program myProgram = new Program();

        SecondClass second = new SecondClass();
        second.LaunchSecondClass();

        //A want to run this method when OnCounted event fired
        //...
        //myProgram.MyPurpose();
        //...
    }


    public void MyPurpose()
    {
        Console.WriteLine("Program Class here!");
        Console.ReadLine();
    }
}

public class SecondClass
{
    public void LaunchSecondClass()
    {
        ThirdClass third = new ThirdClass();
        third.myEvent += this.OnCounted;

        third.count();
    }

    private void OnCounted(object sender, EventArgs e)
    {
        Console.WriteLine("Second Class Here.");
        //Console.ReadLine();
    }
}



public class ThirdClass
{
    public event EventHandler myEvent;

    public void count()
    {
        for (int i = 0; i < 3; i++)
        {
            //Only for testing
            Thread.Sleep(1000);
        }
        OnCounted();
    }

    protected virtual void OnCounted()
    {
        if (myEvent != null)
            myEvent(this, EventArgs.Empty);
    }
}

1 个答案:

答案 0 :(得分:0)

正如我所说,有很多方法可以做你想做的事。尽管如此,我还是强烈建议你注意@Glubus的评论。

以下三种方式:

选项1 - 在SecondClass

上公开新活动
public class SecondClass
{
    public event EventHandler SecondEvent;

    public void LaunchSecondClass()
    {
        ThirdClass third = new ThirdClass();
        third.myEvent += this.OnCounted;

        third.count();
    }

    private void OnCounted(object sender, EventArgs e)
    {
        Console.WriteLine("Second Class Here.");
        //Console.ReadLine();
        SecondEvent?.Invoke(); // Based on C# 6
    }
}

在你的主程序中:

static void Main(string[] ars)
{
    Program myProgram = new Program();

    SecondClass second = new SecondClass();
    second.SecondEvent += MyPurpose;
    second.LaunchSecondClass();
}

选项2 - 将ThirdClass公开SecondClass作为财产

public class SecondClass
{
    public ThirdClass ThirdClass { get; }

    public SecondClass()
    {
        ThirdClass = new ThirdClass();
        ThirdClass.myEvent += this.OnCounted;
    }

    public void LaunchSecondClass()
    {
        if(ThirdClass == null)
            ThirdClass = new ThirdClass();

        ThirdClass.count();
    }

    private void OnCounted(object sender, EventArgs e)
    {
        Console.WriteLine("Second Class Here.");
        //Console.ReadLine();
    }
}

在你的主程序中:

static void Main(string[] ars)
{
    Program myProgram = new Program();

    SecondClass second = new SecondClass();
    second.ThirdClass.myEvent += MyPurpose;
    second.LaunchSecondClass();
}

选项3 - 通过Action方法执行并SecondClass(代表)

public class SecondClass
{
    public void LaunchSecondClass(Action action)
    {
        ThirdClass third = new ThirdClass();
        third.myEvent += this.OnCounted;

        if(action != null)
            third.myEvent += (o, a) => action.Invoke();

        third.count();
    }

    private void OnCounted(object sender, EventArgs e)
    {
        Console.WriteLine("Second Class Here.");
        //Console.ReadLine();
    }
}

在你的主程序中:

static void Main(string[] ars)
{
    Program myProgram = new Program();

    SecondClass second = new SecondClass();
    second.LaunchSecondClass(MyPurpose);
}

请记住,如果不了解您打算应用它的真实场景,则无法保证最佳实践选择。因此,您可能必须search up a design pattern解决您的问题并在规划解决方案时遵循SOLID原则

这是获得干净,高效代码的最佳方式。

以下是关于此主题的精彩内容:

我希望它对你有帮助,对不起我的坏英语