We are using IoC and have our logging exposed with it. We are using Common.Logging and I have written a matching delegate for Common.Logging.FormatMessageHandler but I do not know how to convert from our version of that delegate to the one that the Common.Logging api is expecting.
This question appears to be similar but I do not understand how to convert from my implemented type to the known type that I want to call.
Dynamically casting one type of delegate to another
Here is my delegate signature:
public delegate string FormatMessageHandler(string format, params object[] args)
Here is Common.Logging’s:
public delegate string FormatMessageHandler(string format, params object[] args)
Same name (not that is matters) and same number of parameters. Both are known at compile time so it should be something obvious but I am not seeing it.
Why are you not using Common.Logging’s delegate in the first place if it is exactly the same?
However, a solution to your problem is to either use the dynamic cast explained in the article linked in the question you mentioned, or you do it like this:
UPDATE:
According to your comment, you want something like that:
This will create a new action with one parameter
hof typeCommon.Logging.FormatMessageHandlerwhich calls the supplied actionformatMessageCallbackwith a new delegate ofYour.FormatMessageHandlerthat accepts two parametersfanda. This new delegate in turn callshwith the two supplied parameters.