0

これが私がやろうとしていることです。

クラス1:ベースクラス

    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);
    }

基本的に、同じ構造に従う複数のクラスのベースクラスでUpdateを再利用できるようにしたいと考えています。

4

2 に答える 2

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 に答える
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 に答える