Class PerTypeDispatcher<TBase>
Calls different action delegates based on the runtime types of objects. Types must be exact matches. Inheritance is not considered.
public class PerTypeDispatcher<TBase> where TBase : class
Type Parameters
TBase
The common base type of all objects to be dispatched.
- Inheritance
-
objectPerTypeDispatcher<TBase>
- Extension Methods
Constructors
PerTypeDispatcher(bool)
Calls different action delegates based on the runtime types of objects. Types must be exact matches. Inheritance is not considered.
public PerTypeDispatcher(bool ignoreMissing)
Parameters
ignoreMissing
booltrue
to silently ignore dispatch attempts on unknown types;false
to throw exceptions.
Methods
Add<TSpecific>(Action<TSpecific>)
Adds a dispatch delegate.
public PerTypeDispatcher<TBase> Add<TSpecific>(Action<TSpecific> action) where TSpecific : TBase
Parameters
action
Action<TSpecific>The delegate to call.
Returns
- PerTypeDispatcher<TBase>
The "this" pointer for use in a "Fluent API" style.
Type Parameters
TSpecific
The specific type to call the delegate for. Does not match subtypes
Dispatch(IEnumerable<TBase>)
Dispatches for each element in a collection. Set up with Add<TSpecific>(Action<TSpecific>) first.
public void Dispatch(IEnumerable<TBase> elements)
Parameters
elements
IEnumerable<TBase>The elements to be dispatched.
Exceptions
- KeyNotFoundException
No delegate matching one of the element types was Add<TSpecific>(Action<TSpecific>)ed.
Dispatch(TBase)
Dispatches an element to the delegate matching the type. Set up with Add<TSpecific>(Action<TSpecific>) first.
public void Dispatch(TBase element)
Parameters
element
TBaseThe element to be dispatched.
Exceptions
- KeyNotFoundException
No delegate matching the
element
type was Add<TSpecific>(Action<TSpecific>)ed.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<Type, Action<object>>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<Type, Action<object>>>
An enumerator that can be used to iterate through the collection.