public MainWindow()
{
CommandManager.AddExecutedHandler(this, ExecuteHandler);
}
void ExecuteHandler(object sender, ExecutedRoutedEventArgs e)
{
}
Error 1 Argument 2: cannot convert from 'method group' to 'System.Delegate'
-
What if you want the method to accept different signature delegates?mireazma– mireazma2021年02月13日 12:19:00 +00:00Commented Feb 13, 2021 at 12:19
-
@mireazma make it generic?Tim Lovell-Smith– Tim Lovell-Smith2021年02月13日 17:51:28 +00:00Commented Feb 13, 2021 at 17:51
-
Reading your comment I realized I didn't phrase the question correctly. I meant "What if you want the method to accept arbitrary signature delegates". As in having an unknown signature delegate as argument. Generics would have worked if C# had supported variadics. Otherwise it's beyond my view.mireazma– mireazma2021年02月15日 07:24:09 +00:00Commented Feb 15, 2021 at 7:24
2 Answers 2
I guess there are multiple ExecuteHandler with different signatures. Just cast your handler to the version you want to have:
CommandManager.AddExecuteHandler(this, (Action<object,ExecutedRoutedEventArgs>)ExecuteHandler);
1 Comment
I got this error due to a completely different problem.
var engine = new Ingest(GetOperationType, GetSqlConnection);
private static SqlConnection GetSqlConnection(string instanceCode, string defaultDB)
=> new SqlConnection($"Server={InstanceMap[instanceCode]};Database={defaultDB};Trusted_Connection=True;");
private static Type GetOperationType(string operationName)
=> Type.GetType(typeof(BaseOperation).Namespace + "." + operationName + ", ConditioningEngine.EnginePlugins");
Both params to 'new Ingest...' are different types of delegate. The GetOperationType param had no problem while GetSqlConnection got the 'cannot convert from method group' error.
After trying the casting trick mentioned in the other answers the error changed to System.Data.SqlClient not referenced. After fixing the reference problem I could get rid of the cast. That is, the error was false. The casting trick was useful in letting me see what the real error was but the cast itself wasn't necessary. It seems the true error could be almost anything.