问题
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)
{
}
回答1:
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
openIdandemailexplicit types, which (ifuserServiceisn'tdynamic) 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 fromdynamic:string openId = meResult.id; string email = meResult.emails.preferred; userService.SignIn(openId, email, Callback);Create a specific delegate type instance from the
Callbackmethod, if you want to keep theSignIncall 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));
来源:https://stackoverflow.com/questions/22466344/how-to-do-callbacks-with-dynamic