加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用Dynamic进行回调?

发布时间:2020-12-15 08:02:38 所属栏目:百科 来源:网络整理
导读:我一直收到这个错误,不知道如何纠正它 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.Conn
我一直收到这个错误,不知道如何纠正它

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)
{

}

解决方法

问题是这个调用是动态的:
userService.SignIn(openId,Callback);

它必须是,因为openId和email被推断为动态类型:

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

您不能在动态调用中使用这样的方法组转换 – 这只是使用动态的限制之一.

所以,选项:

>提供openId和电子邮件显式类型(如果userService不是动态的)将使调用非动态,方法组转换将起作用.这只是意味着明确指定类型,因为动态可以使用隐式转换:

string openId = meResult.id;
string email = meResult.emails.preferred;
userService.SignIn(openId,Callback);

>如果要保持SignIn调用动态,请从Callback方法创建特定的委托类型实例:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读