Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix 'compiler error - cannot convert from method group to System.Delegate'?

Tags:

c#

delegates

 public MainWindow()
 {
    CommandManager.AddExecutedHandler(this, ExecuteHandler);
 }

 void ExecuteHandler(object sender, ExecutedRoutedEventArgs e)
 {
 }

Error 1 Argument 2: cannot convert from 'method group' to 'System.Delegate'

like image 337
Tim Lovell-Smith Avatar asked Mar 19 '10 18:03

Tim Lovell-Smith


2 Answers

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);
like image 194
Achim Avatar answered Nov 16 '22 21:11

Achim


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.

like image 32
bielawski Avatar answered Nov 16 '22 22:11

bielawski