patterncsharpMinor
Removing all delegates from an event dispatcher
Viewed 0 times
removingalldelegatesfromdispatcherevent
Problem
Given a class which is an event dispatcher whose underlying store is a dictionary of events whose key is the event type
Or is there a better way? I have included the whole class below for clarity.
```
using System;
using System.Collections.Generic;
internal class ApplicationEventDispatcher : IApplicationEventDispatcher, IDisposable
{
private bool _disposed;
private readonly Dictionary _applicationEventHandlers;
public ApplicationEventDispatcher()
{
_applicationEventHandlers = new Dictionary();
}
~ApplicationEventDispatcher()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
// free other managed objects that implement IDisposable only
RemoveAllListeners();
}
// release any unmanaged objects
// set the object references to null
_disposed = true;
}
public void AddListener(ApplicationEventHandlerDelegate handler) where T : IApplicationEvent
{
if (_applicationEventHandlers.ContainsKey(typeof(T)))
{
Delegate handlersForType = _applicationEventHandlers[typeof(T)];
_applicationEventHandlers[typeof(T)] = Delegate.Combine(handlersForType, handler);
}
else
{
_applicationEventHandlers[typeof(T)] = handler;
}
}
public void RemoveListener(ApplicationEventHandlerDelegate handle
private readonly Dictionary _applicationEventHandlers;, is this the correct way to remove all remaining delegates when the class needs to be disposed?private void RemoveAllListeners()
{
foreach (Type handlerType in _applicationEventHandlers.Keys)
{
Delegate handlers = _applicationEventHandlers[handlerType];
Delegate.RemoveAll(handlers, handlers);
}
}Or is there a better way? I have included the whole class below for clarity.
```
using System;
using System.Collections.Generic;
internal class ApplicationEventDispatcher : IApplicationEventDispatcher, IDisposable
{
private bool _disposed;
private readonly Dictionary _applicationEventHandlers;
public ApplicationEventDispatcher()
{
_applicationEventHandlers = new Dictionary();
}
~ApplicationEventDispatcher()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
// free other managed objects that implement IDisposable only
RemoveAllListeners();
}
// release any unmanaged objects
// set the object references to null
_disposed = true;
}
public void AddListener(ApplicationEventHandlerDelegate handler) where T : IApplicationEvent
{
if (_applicationEventHandlers.ContainsKey(typeof(T)))
{
Delegate handlersForType = _applicationEventHandlers[typeof(T)];
_applicationEventHandlers[typeof(T)] = Delegate.Combine(handlersForType, handler);
}
else
{
_applicationEventHandlers[typeof(T)] = handler;
}
}
public void RemoveListener(ApplicationEventHandlerDelegate handle
Solution
Delegate.RemoveAll has no effect on existing delegates. It actually constructs a new one. Delegates are immutable and cannot be changed after construction.You can remove this line. I would not implement IDisposable or any cleanup code here at all. Just release reference on event dispatcher (it makes sense to do it anyway after calling
Dispose().Context
StackExchange Code Review Q#118109, answer score: 2
Revisions (0)
No revisions yet.