How to do Callbacks with Dynamic?

I keep getting this error and not sure how to correct it

Error 1 Cannot use 'Callback' as an argument to a dynamically dispatched operation because it is a method group. Did you intend to invoke the method?

    //...
    if (e.Status == LiveConnectSessionStatus.Connected)
    {              
        client = new LiveConnectClient(e.Session);
        LiveOperationResult operationResult = await client.GetAsync("me");
        try
        {

            dynamic meResult = operationResult.Result;

            var openId = meResult.id;
            var email = meResult.emails.preferred;
            //MessageBox.Show(openId);
            //MessageBox.Show(email);
           userService.SignIn(openId, email, Callback);


        }
        catch (LiveConnectException exception)
        {
            MessageBox.Show("Error calling API: " + exception.Message);
        }
    }
}

private void Callback(ErrorModel error)
{
    if (error != null)
    {
        MessageBox.Show(error.FriendlyErrorMsg, error.Caption, MessageBoxButton.OK);
    }
    else
    {

    }
}

public void SignIn(string id, string email, Action<ErrorModel> callBack)
{

}
Jon Skeet
people
quotationmark

The problem is that this call is dynamic:

userService.SignIn(openId, email, Callback);

It has to be, because openId and email are inferred to be of type dynamic:

var openId = meResult.id;
var email = meResult.emails.preferred;

You can't use a method group conversion like this in a dynamic call - it's just one of the restrictions of using dynamic.

So, options:

  • Give openId and email explicit types, which (if userService isn't dynamic) will make the call non-dynamic, at which the method group conversion will work. This just means specifying the types explicitly, as there's an implicit conversion available from dynamic:

    string openId = meResult.id;
    string email = meResult.emails.preferred;
    userService.SignIn(openId, email, Callback);
    
  • Create a specific delegate type instance from the Callback method, if you want to keep the SignIn call dynamic:

    var openId = meResult.id;
    var email = meResult.emails.preferred;
    // Or use whichever delegate type is actually appropriate for SignIn
    userService.SignIn(openId, email, new Action<ErrorModel>(Callback));
    

people

See more on this question at Stackoverflow