如何摆脱烧制锅炉板代码的事件?

我正在开发一个事件驱动的应用程序并运行multithreading,所以我有很多事件被解雇并以“保存方式”触发它们我按照以下方式执行此操作:

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 { /// Collection of common extensions. public static class EventExtensions { /// Raises the specified event. /// The event. /// The sender. /// The  instance containing the event data. public static void Raise(this EventHandler theEvent, object sender, EventArgs args = null) { if (theEvent != null) theEvent(sender, args); } /// Raises the specified event. /// The type of the event argument. /// The event. /// The sender. /// The arguments. public static void Raise(this EventHandler theEvent, object sender, T args) where T : EventArgs { if (theEvent != null) theEvent(sender, args); } /// Raises the specified event. /// The value type contained in the EventArgs. /// The event. /// The sender. /// The arguments. public static void Raise(this EventHandler> theEvent, object sender, T args) { if (theEvent != null) theEvent(sender, new EventArgs(args)); } /// Raises the specified event. /// The value type contained in the EventArgs. /// The event. /// The sender. /// The new value. /// The old value. public static void Raise(this EventHandler> theEvent, object sender, T newValue, T oldValue) { if (theEvent != null) theEvent(sender, new ValueChangedEventArgs(newValue, oldValue)); } /// Raises the specified event for every handler on its own thread. /// The value type contained in the EventArgs. /// The event. /// The sender. /// The arguments. public static void RaiseAsync(this EventHandler> theEvent, object sender, T args) { if (theEvent != null) { var eventArgs = new EventArgs(args); foreach (EventHandler> action in theEvent.GetInvocationList()) action.BeginInvoke(sender, eventArgs, null, null); } } } } 

和那里使用的ValueChangedEventArgs类:

 /// EventArgs for notifying about changed values. /// The type of the contained value. public class ValueChangedEventArgs : EventArgs { /// Initializes a new instance of the  class. /// The new value. /// The old value. public ValueChangedEventArgs(T newValue, T oldValue) { NewValue = newValue; OldValue = oldValue; } /// Gets the new value. /// The new value. public T NewValue { get; private set; } /// Gets the old value. /// The old value. public T OldValue { get; private set; } }