Return 通用对象类型

Return generic object type

我们在 GitHub Gist 上提供了完整的代码,我只包括了我认为需要显示问题的部分。

执行方法:

  public object Execute()
        {
            var request = createWebRequest();
            request.Method = this.Method;
            applyPostData(ref request);

            request.ContentType = "application/json";
            request.UserAgent = "generic-http-dotnet-client/3.5/v1 (gzip)";

            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            try
            {
                return JsonConvert.DeserializeObject(responseString);
            }
            catch (Exception)
            {
                return new HttpResponse { message = responseString };
            }
        }

好的,这有点像。但是我希望能够做的是在调用方法后将其转换为适当的对象类型。

var response = (MetaDataResponse)request.Execute();

我知道应该有一些方法可能使用泛型(疯狂猜测)让我 json DeserializeObject 响应和 return 它?

我最终将 return 类型的执行方法更改为字符串,然后执行此操作。

public class MetaDataRequest : HttpRequestBase
    {
        public MetaDataResponse MetaDataItems { get; private set; }

        public MetaDataRequest() : base(new Uri("https://www.googleapis.com/analytics/v3/metadata/ga/columns"), "GET")
        {
            this.addParameter("key", "xxxx");
            var response = this.Execute();

            try
            {
                MetaDataItems =  JsonConvert.DeserializeObject<MetaDataResponse>(response);
            }
            catch (Exception)
            {
                var resultsx = new HttpResponse { message = response };
            }
        }
    }

哪个有用,但对我来说接缝很乱。请注意,此项目是 .Net framework 3.5,我无法更改它。随意添加您能想到的任何其他标签。

将泛型与 class MetaDataRequest

结合使用
class MetadataRequest<T>:HttpRequestBase
{
    public T Execute()
    {
        var request = createWebRequest();
        request.Method = this.Method;
        applyPostData(ref request);

        request.ContentType = "application/json";
        request.UserAgent = "generic-http-dotnet-client/3.5/v1 (gzip)";

        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

        return JsonConvert.DeserializeObject<T>(responseString);
    }
}

然后,要获取对象,可以这样调用方法:

MetadataResponse responseObject = (new MetadataRequest<MetadataResponse>()).Execute();

在这种情况下,您可以捕获 .Execute() 抛出的异常,因为它只能 return null 或 T 类型的实例。

编辑:您也可以按照 .

使用通用方法

首先,要点不是处理它的 IDisposables,这很草率。我会推荐这样的东西:

public T Execute<T>()
    {
        var request = createWebRequest();
        request.Method = this.Method;
        applyPostData(ref request);

        request.ContentType = "application/json";
        request.UserAgent = "generic-http-dotnet-client/3.5/v1 (gzip)";

        using(var response = (HttpWebResponse)request.GetResponse())
        using(var stream = response.GetResponseStream())
        using(var reader = new StreamReader(stream))
        {
            try
            {
                var responseString = reader.ReadToEnd();
                return JsonConvert.DeserializeObject<T>(responseString);
            }
            catch (Exception ex)
            {
                //log something with ex
                return default(T);
            }
        }
    }