I want to be able to store a function in a dictionary with the Key being the object type. All functions will accept objects derived from the same base class. I want to be able to convert (based on a registered function) a class into a DbCommand. I’ve listed my code below in the form that seems correct but something is wrong with the syntax, etc. I think I’m missing something really simple… Any help will be greatly appreciated!
private Dictionary<object, Func<TrackingRecord, DbCommand>> conversionFunctions = new Dictionary<object, Func<TrackingRecord, DbCommand>>();
public void RegisterConversionFunction<T>(Func<T, DbCommand> conversionFunction) where T : TrackingRecord
{
conversionFunctions.Add(typeof(T), conversionFunction);
}
public DbCommand GetConverstion<T>(T trackingRecord) where T : TrackingRecord
{
DbCommand command = null;
if (conversionFunctions.ContainsKey(trackingRecord.GetType()))
{
Func<T, DbCommand> conversionFunction;
if (conversionFunctions.TryGetValue(trackingRecord.GetType(), out conversionFunction))
{
command = conversionFunction.Invoke(trackingRecord);
}
}
else
{
command = DefaultConversion(trackingRecord);
}
return command;
}
The problem with your original solution is that the method signature
Func<T, DbCommand>doesn’t match the expected valueFunc<TrackingRecord, DbCommand>. To fix this, because you know that T is constrained to inherit from TrackingRecord, you can create a wrapping function that matches the expected signature (Func<TrackingRecord, DbCommand>) and cast the argument to T.Try this: