gpt4 book ai didi

c# - 参数化通用事件系统

转载 作者:行者123 更新时间:2023-11-30 17:51:11 25 4
gpt4 key购买 nike

我不确定以前是否有人问过这个问题,但我真的不知道如何查找它,因为我不确定这件事/我正在尝试做的事情到底叫什么.. .

我有一个在 Unity3D 中使用的基于委托(delegate)的消息传递通用系统 - 摘自 here .

[ UA Crosslink ]

它是这样使用的:

 // Writing an event listener

void OnSpeedChanged(float speed)
{
this.speed = speed;
}

// Registering an event listener

void OnEnable()
{
Messenger<float>.AddListener("speed changed", OnSpeedChanged);
}

// Unregistering an event listener

void OnDisable()
{
Messenger<float>.RemoveListener("speed changed", OnSpeedChanged);
}

我遇到的问题是,代码目前非常不干(有很多复制粘贴),我想干它,希望通过参数化它,使其更通用。

我将发布相关代码 - 请注意,您不必真正详细了解代码及其作用即可回答问题。

这是一个在幕后做事的类:

static internal class MessengerInternal
{
static public Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
static public readonly MessengerMode DEFAULT_MODE = MessengerMode.REQUIRE_LISTENER;

static public void OnListenerAdding(string eventType, Delegate listenerBeingAdded)
{
if (!eventTable.ContainsKey(eventType)) {
eventTable.Add(eventType, null);
}

Delegate d = eventTable[eventType];
if (d != null && d.GetType() != listenerBeingAdded.GetType()) {
throw new ListenerException(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, d.GetType().Name, listenerBeingAdded.GetType().Name));
}
}

static public void OnListenerRemoving(string eventType, Delegate listenerBeingRemoved)
{
if (eventTable.ContainsKey(eventType)) {
Delegate d = eventTable[eventType];

if (d == null) {
throw new ListenerException(string.Format("Attempting to remove listener with for event type {0} but current listener is null.", eventType));
}
else if (d.GetType() != listenerBeingRemoved.GetType()) {
throw new ListenerException(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, d.GetType().Name, listenerBeingRemoved.GetType().Name));
}
}
else {
throw new ListenerException(string.Format("Attempting to remove listener for type {0} but Messenger doesn't know about this event type.", eventType));
}
}

static public void OnListenerRemoved(string eventType)
{
if (eventTable[eventType] == null) {
eventTable.Remove(eventType);
}
}

static public void OnBroadcasting(string eventType, MessengerMode mode)
{
if (mode == MessengerMode.REQUIRE_LISTENER && !eventTable.ContainsKey(eventType)) {
throw new BroadcastException(string.Format("Broadcasting message {0} but no listener found.", eventType));
}
}
}

现在,我有通用的信使类,它们有一个、两个、三个甚至没有参数 - 因此用户可以选择合适的事件处理程序来订阅事件。

这是没有通用参数的版本:

// No parameters
static public class Messenger {
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable;

static public void AddListener(string eventType, Callback handler) {
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback)eventTable[eventType] + handler;
}

static public void RemoveListener(string eventType, Callback handler) {
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
}

static public void Broadcast(string eventType) {
Broadcast(eventType, MessengerInternal.DEFAULT_MODE);
}

static public void Broadcast(string eventType, MessengerMode mode) {
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback callback = d as Callback;
if (callback != null) {
callback();
} else {
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
}

这是采用一个 arg 的版本,(我只是复制粘贴并添加一个 T):

// One parameter
static public class Messenger<T> {
private static Dictionary<string, Delegate> eventTable = MessengerInternal.eventTable;

static public void AddListener(string eventType, Callback<T> handler) {
MessengerInternal.OnListenerAdding(eventType, handler);
eventTable[eventType] = (Callback<T>)eventTable[eventType] + handler;
}

static public void RemoveListener(string eventType, Callback<T> handler) {
MessengerInternal.OnListenerRemoving(eventType, handler);
eventTable[eventType] = (Callback<T>)eventTable[eventType] - handler;
MessengerInternal.OnListenerRemoved(eventType);
}

static public void Broadcast(string eventType, T arg1) {
Broadcast(eventType, arg1, MessengerInternal.DEFAULT_MODE);
}

static public void Broadcast(string eventType, T arg1, MessengerMode mode) {
MessengerInternal.OnBroadcasting(eventType, mode);
Delegate d;
if (eventTable.TryGetValue(eventType, out d)) {
Callback<T> callback = d as Callback<T>;
if (callback != null) {
callback(arg1);
} else {
throw MessengerInternal.CreateBroadcastSignatureException(eventType);
}
}
}
}

正如您可能已经猜到的那样,需要两个参数的那个,我只是再次复制粘贴,并添加另一个泛型类型,比如 <T, U>等等

这是我要消除的部分 - 但我还不知道如何消除。更准确地说,我正在寻找的是: 只有一个 Messenger类,但我能够做到:

Messenger<float>.Subscribe("player dead", OnDead);
Messenger<int, bool>.Subscribe("on something", OnSomething);
Messenger<bool, float, MyType>.Subscribe( stuff );

或者,(哪个并不重要)

Messenger.Subscribe<float> ("player dead", OnDead);

你明白了......

我该怎么做,我怎么能写一个通用的信使,当我想添加另一个通用的 arg 时,我不必复制粘贴并编写一个完整的其他版本,只是因为我需要一个额外的参数?

非常感谢!

最佳答案

您有一个 Messenger,但您似乎没有发送任何消息!您试图在没有合适的信封的情况下发送内容。将您要发送的值包装在一个委托(delegate)您的实际消息的类中,然后您可以订阅消息的类型,其中将包含您尝试发送的所有值。

public class PlayerSpeedChangedMessage {
public Guid PlayerId { get; set; }
public int OldSpeed { get; set; }
public int NewSpeed { get; set; }
}

public class MyMessageHandler {

public MyMessageHandler() {
Messenger<PlayerSpeedChangedMessage>.Subscribe(OnDead);
}

HandleSpeedChange(PlayerSpeedChangedMessage message) {
// Do stuff with the message
}
}

关于c# - 参数化通用事件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19816117/

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