Class DelegateInvoker<TBaseArg, TResult>
- Namespace
- Utils.Reflection
- Assembly
- Utils.dll
A utility class that allows dynamic invocation of delegate methods based on the runtime type of an argument.
public class DelegateInvoker<TBaseArg, TResult> : IEnumerable<Delegate>, IEnumerable
Type Parameters
TBaseArgThe base type for the argument of the delegates.
TResultThe return type of the delegates.
- Inheritance
-
DelegateInvoker<TBaseArg, TResult>
- Implements
- Inherited Members
- Extension Methods
Remarks
Dispatch order: exact type match first, then base classes from most- to least-derived, then interfaces. Interface matches are ambiguous when two registered interfaces are both implemented by the runtime type without one being a sub-interface of the other; an AmbiguousMatchException is thrown in that case.
Methods
Add<T>(Func<T, TResult>)
Registers a delegate function that takes a specific argument type derived from TBaseArg and returns TResult.
public void Add<T>(Func<T, TResult> function) where T : TBaseArg
Parameters
functionFunc<T, TResult>The delegate function to register.
Type Parameters
TThe specific argument type derived from TBaseArg.
GetEnumerator()
Returns an enumerator that iterates through the registered delegates.
public IEnumerator<Delegate> GetEnumerator()
Returns
- IEnumerator<Delegate>
An enumerator for the registered delegates.
Invoke(TBaseArg)
Invokes the delegate registered for the runtime type of the provided argument. Throws an exception if no matching delegate is found.
public TResult Invoke(TBaseArg arg)
Parameters
argTBaseArgThe argument of type TBaseArg to use for delegate invocation.
Returns
- TResult
The result of the delegate invocation.
Exceptions
- MissingMethodException
Thrown when no delegate is registered for the runtime type of the argument.
TryInvoke(TBaseArg, out TResult)
Attempts to invoke the registered delegate for the runtime type of the provided argument.
public bool TryInvoke(TBaseArg arg, out TResult result)
Parameters
argTBaseArgThe argument of type TBaseArg to use for delegate invocation.
resultTResultThe result of the delegate invocation if successful.
Returns
- bool
True if a matching delegate is found and invoked, otherwise false.
Exceptions
- AmbiguousMatchException
Thrown when two or more registered interfaces match the runtime type without a clear specificity ordering (neither is a sub-interface of the other).