如何使异步方法调用泛型

时间:2012-02-08 22:31:01

标签: c# windows-phone-7 restsharp

我正在尝试将令牌验证提取为单独的方法,然后在else语句中的代码块我也想要通用。

public void Subscribe(string s1, string s2, string s3)
    {
        if (token == null || token.IsExpired)
        {                
            RestClient client = CreateRestClient();
            RestRequest treq = CreateTokenRequest();                

            client.ExecuteAsync(treq, (response) =>
            {
                CreateNewToken(response.Content);
                SubscribeToNotifications(s1, s2, s3);
            });
        }
        else
        {
            var c = new RestClient(ServiceAddress);
            var r = new RestRequest("Subscribe?");
            r.AddParameter("P1", s1);
            r.AddParameter("P2", s2);
            r.AddParameter("P3", s3);
            r.AddHeader("Authorization", token.TokenString);
            c.ExecuteAsync(r, (response) =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                    SubcribeCompleted(this, new GeneralEventArgs(true, string.Empty));
                else if (response.StatusCode == HttpStatusCode.Unauthorized)
                    SubcribeCompleted(this, new GeneralEventArgs(false, "Unauthorized!"));
                else
                    SubcribeCompleted(this, new GeneralEventArgs(false, "Error"));
            });
        }
    }

问题是如何指定在回调处理程序中调用哪个方法? 我尝试过使用Func<>,但我使用通用令牌方法的其他方法不一定具有相同的签名。

提前感谢任何输入:)

1 个答案:

答案 0 :(得分:0)

Action<RestResponse>RestClient.ExecuteAsync的类型。

当有返回值时使用

Func<>,因为没有,使用Action<>