我正在开发一个事件驱动的应用程序并运行多线程,因此我有很多事件被解雇并以“保存方式”解雇它们“我按照以下方式执行此操作:
public static void OnEVENT(EventArgs args)
{
var theEvent = EVENT;
if (theEvent != null)
theEvent(this, args);
}
此模式在我的应用程序中重复多次。
有没有办法摆脱这种模式的重复并以通用的方式实现这一点?
答案 0 :(得分:0)
我创建了一个包含多个扩展程序的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; }
}