0

这就是我想要做的。

Class1:基类

    private void Update()
    {
       base.Update(Updated);
    }

    private void Updated(IRestResponse<thisClass> response)
    { 
        ....
    }

...

基类:

public void Update(<T> callback)
    {
        RestClient client = new RestClient("https://www.googleapis.com");
        client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(App.AuthenticationResult.access_token);
        var request = new RestRequest(path, Method.GET);
        client.ExecuteAsync<...T...>(request, callback);
    }

本质上,我希望能够在遵循相同结构的多个类中重用基类中的更新。

4

2 回答 2

0

目前您的基类Update方法有一些语法错误。

public void Update<T>(T callback)
{
    RestClient client = new RestClient("https://www.googleapis.com");
    client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(App.AuthenticationResult.access_token);
    var request = new RestRequest(path, Method.GET);
    client.ExecuteAsync<T>(request, callback);
}

如果参数是回调,它不应该是委托吗?

它不需要如此通用,但您可以执行以下操作:

public delegate T CallbackDelegate<T>();
public void Update<T>(CallbackDelegate<T> callback)
    {
        RestClient client = new RestClient("https://www.googleapis.com");
        client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(App.AuthenticationResult.access_token);
        var request = new RestRequest(path, Method.GET);
        client.ExecuteAsync<T>(request, callback);
    }
于 2012-06-27T02:24:11.597 回答
0

我从您的问题中收集到的是您想要共享/重用Update()Updated()在扩展类中有一个自定义......?

可能最简单的方法是在基类中创建一个虚拟方法,该方法在扩展类中被覆盖:

public class BaseClass
{
    public virtual void Updated() { }
    public void Update()
    {
        RestClient client = new RestClient("https://www.googleapis.com");
        client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(App.AuthenticationResult.access_token);
        var request = new RestRequest(path, Method.GET);

        //implement whatever logic you want with the response.

        this.Updated(); //include whatever parameters you need to pass.
    }
}

public class Class1 : BaseClass
{
    public override void Updated()
    {
        //implement logic specific to Class1...
    }
}
于 2012-06-27T04:36:09.443 回答