【发布时间】:2020-06-23 00:58:54
【问题描述】:
一直在尝试学习 c# 并尝试事件。已经尝试过了,并得到了一些功能。查看我的代码时,它看起来很奇怪,因为我必须为每个订阅者创建新对象,然后订阅发布者。我是否需要创建新对象,然后使用该对象订阅发布者?
程序.cs
namespace ConsoleApp1
{
class Program
{
public static void Main()
{
ExternalClass potato = new ExternalClass();
potato.Start();
}
}
}
外部类.cs
using System;
namespace ConsoleApp1
{
class ExternalClass
{
//This is the event, which is the actual event you call to trigger all of the other method/function calls.
public void Start()
{
string SubscribeMessage = "Subscribing...";
string UnsubscribeMessage = "Unsubscribing";
Apple potato = new Apple();
Orange beet = new Orange();
//adding a function to an event
Console.WriteLine(SubscribeMessage);
potato.MyEvent += potato.helloWorld;
potato.MyEvent += beet.DisplayOrange;
potato.OnEventSuccess();
//unsubscribing from an event
Console.WriteLine(UnsubscribeMessage);
potato.MyEvent -= beet.DisplayOrange;
potato.MyEvent -= potato.helloWorld;
potato.OnEventSuccess();
}
}
}
Apple.cs
using System;
namespace ConsoleApp1
{
class Apple
{
public event Action MyEvent;
//This is the function that you wish to call when you call the event. All other function/method calls must have the same shape as the delegate
public void helloWorld()
{
Console.WriteLine("Hello world!");
}
public void OnEventSuccess()
{
//myEvent?.Invoke();
if (MyEvent != null)
{
MyEvent?.Invoke();
}
else
{
Console.WriteLine("Event is empty!");
}
}
}
}
Orange.cs
using System;
namespace ConsoleApp1
{
public class Orange
{
public void DisplayOrange()
{
Console.WriteLine("Orange is functioning");
}
}
}
样本输出:
Subscribing...
Hello world!
Orange is functioning
Unsubscribing
Event is empty!
【问题讨论】:
-
你觉得创建发布者很奇怪还是创建订阅者很奇怪?
-
@LouisGo 可能在 start 方法中创建订阅者感觉很奇怪?
-
您不必为每个事件都实例化一个全新的对象,您可以向该事件注册任何您想要的方法,只要签名匹配即可。
-
@entropic 你是说从一个对象到一个事件的多种方法吗?还是一个对象订阅多个事件?我很确定第一个是可能的,第二个我知道是可能的。
-
我是说如果你愿意,你根本不需要
Orange类,你可以在ExternalClass中编写DisplayOrange()函数并将其注册到事件中