This question is partly about delegates, and partly about generics.
Given the simplified code:
internal sealed class TypeDispatchProcessor
{
private
I strongly suspect that wrapping the calls would be a lot more efficient than using DynamicInvoke
. Your code would then be:
internal sealed class TypeDispatchProcessor
{
private readonly Dictionary> _actionByType
= new Dictionary>();
public void RegisterProcedure(Action action)
{
_actionByType[typeof(T)] = item => action((T) item);
}
public void ProcessItem(object item)
{
Action
It's worth benchmarking it, but I think you'll find this a lot more efficient. DynamicInvoke
has to check all the arguments with reflection etc, instead of the simple cast in the wrapped delegate.