gpt4 book ai didi

c# - 如何在三个类中处理事件

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:40 24 4
gpt4 key购买 nike

请看我的代码。我有 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);
}
}

最佳答案

正如我所说,有很多方法可以做你想做的事。目标是可以实现的,尽管如此,我强烈建议您关注@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 - 将 SecondClass 上的 ThirdClass 作为属性公开

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(委托(delegate))由 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 原则

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

这里有一本关于这个主题的好书:

希望对你有帮助,抱歉我的英语不好

关于c# - 如何在三个类中处理事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48151641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com