如何摆脱锅炉板上的事件触发代码
本文关键字:事件 代码 何摆脱 | 更新日期: 2023-09-27 17:59:42
我正在开发一个由事件驱动并运行多线程的应用程序,因此我有很多事件被触发,并"以保存的方式"触发它们
public static void OnEVENT(EventArgs args)
{
var theEvent = EVENT;
if (theEvent != null)
theEvent(this, args);
}
此模式在我的整个应用程序中重复多次。
有没有办法摆脱这种模式的重复,并以通用的方式实现它
我创建了一个包含多个扩展的Helper类,所以我根本不需要OnEVENT
方法,只需调用EVENT.Raise(this, args);
:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
namespace System
{
/// <summary>Collection of common extensions.</summary>
public static class EventExtensions
{
/// <summary>Raises the specified event.</summary>
/// <param name="theEvent">The event.</param>
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
public static void Raise(this EventHandler theEvent, object sender, EventArgs args = null)
{
if (theEvent != null)
theEvent(sender, args);
}
/// <summary>Raises the specified event.</summary>
/// <typeparam name="T">The type of the event argument.</typeparam>
/// <param name="theEvent">The event.</param>
/// <param name="sender">The sender.</param>
/// <param name="args">The arguments.</param>
public static void Raise<T>(this EventHandler<T> theEvent, object sender, T args) where T : EventArgs
{
if (theEvent != null)
theEvent(sender, args);
}
/// <summary>Raises the specified event.</summary>
/// <typeparam name="T">The value type contained in the EventArgs.</typeparam>
/// <param name="theEvent">The event.</param>
/// <param name="sender">The sender.</param>
/// <param name="args">The arguments.</param>
public static void Raise<T>(this EventHandler<EventArgs<T>> theEvent, object sender, T args)
{
if (theEvent != null)
theEvent(sender, new EventArgs<T>(args));
}
/// <summary>Raises the specified event.</summary>
/// <typeparam name="T">The value type contained in the EventArgs.</typeparam>
/// <param name="theEvent">The event.</param>
/// <param name="sender">The sender.</param>
/// <param name="newValue">The new value.</param>
/// <param name="oldValue">The old value.</param>
public static void Raise<T>(this EventHandler<ValueChangedEventArgs<T>> theEvent, object sender, T newValue, T oldValue)
{
if (theEvent != null)
theEvent(sender, new ValueChangedEventArgs<T>(newValue, oldValue));
}
/// <summary>Raises the specified event for every handler on its own thread.</summary>
/// <typeparam name="T">The value type contained in the EventArgs.</typeparam>
/// <param name="theEvent">The event.</param>
/// <param name="sender">The sender.</param>
/// <param name="args">The arguments.</param>
public static void RaiseAsync<T>(this EventHandler<EventArgs<T>> theEvent, object sender, T args)
{
if (theEvent != null)
{
var eventArgs = new EventArgs<T>(args);
foreach (EventHandler<EventArgs<T>> action in theEvent.GetInvocationList())
action.BeginInvoke(sender, eventArgs, null, null);
}
}
}
}
以及其中使用的ValueChangedEventArgs
类:
/// <summary>EventArgs for notifying about changed values.</summary>
/// <typeparam name="T">The type of the contained value.</typeparam>
public class ValueChangedEventArgs<T> : EventArgs
{
/// <summary>Initializes a new instance of the <see cref="ValueChangedEventArgs{T}" /> class.</summary>
/// <param name="newValue">The new value.</param>
/// <param name="oldValue">The old value.</param>
public ValueChangedEventArgs(T newValue, T oldValue)
{
NewValue = newValue;
OldValue = oldValue;
}
/// <summary>Gets the new value.</summary>
/// <value>The new value.</value>
public T NewValue { get; private set; }
/// <summary>Gets the old value.</summary>
/// <value>The old value.</value>
public T OldValue { get; private set; }
}