using System.Collections.Generic;
private static readonly Dictionary<Type, Func<Exception, string>> exceptionMappings = new Dictionary<Type, Func<Exception, string>>();
public static void Map<TException>(Func<TException, string> mapToError) where TException : Exception
exceptionMappings.Add(typeof(TException), x => mapToError((TException)x));
exceptionMappings.Add(typeof(TException), mapToError);
public static void Main()
Map<InvalidOperationException>(x => "Prefix: " + x.Message);
var invalidEx = new InvalidOperationException("My exception message");
var errorMessage = exceptionMappings[invalidEx.GetType()](invalidEx);
Console.WriteLine(errorMessage);