Class PerTypeDispatcher<TBase, TResult>
Calls different function delegates (with return values) based on the runtime types of objects. Types must be exact matches. Inheritance is not considered.
public class PerTypeDispatcher<TBase, TResult> where TBase : class
Type Parameters
TBase
The common base type of all objects to be dispatched.
TResult
The return value of the delegates.
- Inheritance
-
objectPerTypeDispatcher<TBase, TResult>
- Extension Methods
Methods
Add<TSpecific>(Func<TSpecific, TResult>)
Adds a dispatch delegate.
public PerTypeDispatcher<TBase, TResult> Add<TSpecific>(Func<TSpecific, TResult> function) where TSpecific : TBase
Parameters
function
Func<TSpecific, TResult>The delegate to call.
Returns
- PerTypeDispatcher<TBase, TResult>
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>(Func<TSpecific, TResult>) first.
public IEnumerable<TResult> Dispatch(IEnumerable<TBase> elements)
Parameters
elements
IEnumerable<TBase>The elements to be dispatched.
Returns
- IEnumerable<TResult>
The values returned by the matching delegates.
Exceptions
- KeyNotFoundException
No delegate matching one of the element types was Add<TSpecific>(Func<TSpecific, TResult>)ed.
Dispatch(TBase)
Dispatches an element to the delegate matching the type. Set up with Add<TSpecific>(Func<TSpecific, TResult>) first.
public TResult Dispatch(TBase element)
Parameters
element
TBaseThe element to be dispatched.
Returns
- TResult
The value returned by the matching delegate.
Exceptions
- KeyNotFoundException
No delegate matching the
element
type was Add<TSpecific>(Func<TSpecific, TResult>)ed.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<Type, Func<TBase, TResult>>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<Type, Func<TBase, TResult>>>
An enumerator that can be used to iterate through the collection.