I have 2 overloaded C# functions like this:
private void _Insert(Hashtable hash, string tablename, Func<string, object[], SqlCommand> command)
private void _Insert(Hashtable hash, string tablename, Func<string, object[], OleCommand> command)
Basically one using OleCommand and the other SqlCommand for the return value of the function.
But the ugly thing about this is that I have to cast the function pointer to the correct type even though i feel the compiler should be able to resolve it without problems:
class RemoteDatabase
{
public SqlCommand GetCommand(string query, object[] values);
}
_Insert(enquiry, "Enquiry", (Func<string, object[], SqlCommand>)(_RemoteDatabase.GetCommand));
Is there any way tell the compiler to be smarter so that I don’t have to do the type casting? Or did I do anything wrong?
EDIT:
Added a bounty because I am really interested to learn. Thanks for any advice.
While not answering your question directly, I did come across the following while writing up a test case, you can get it to compile by wrapping the call in another lambda. Which removes the explicit cast at the cost of another method call (at least I think so, haven’t looked at the IL yet)
EDIT-
I found this post by Eric Lippert that answers this question