これはスレッドセーフですか?
PrismのEventAggregatorは、メソッドが1つしかない非常に単純なクラスです。nullチェックの周りにロックがなく、private_eventsコレクションに追加する新しいタイプが作成されていることに気付いたときは驚きました。2つのスレッドが同じタイプに対して同時にGetEventを呼び出した場合(_eventsに存在する前)、これによりコレクションに2つのエントリが作成されるように見えます。
/// <summary>
/// Gets the single instance of the event managed by this EventAggregator. Multiple calls to this method with the same <typeparamref name="TEventType"/> returns the same event instance.
/// </summary>
/// <typeparam name="TEventType">The type of event to get. This must inherit from <see cref="EventBase"/>.</typeparam>
/// <returns>A singleton instance of an event object of type <typeparamref name="TEventType"/>.</returns>
public TEventType GetEvent<TEventType>() where TEventType : EventBase
{
TEventType eventInstance = _events.FirstOrDefault(evt => evt.GetType() == typeof(TEventType)) as TEventType;
if (eventInstance == null)
{
eventInstance = Activator.CreateInstance<TEventType>();
_events.Add(eventInstance);
}
return eventInstance;
}