如何从这个C#代码中创建子程序?

时间:2016-08-03 01:27:54

标签: c# json rest datacontractserializer

我有以下C#代码,我在代码中重复使用了大约20倍。我无法弄清楚如何将它变成一个子程序,这将允许我重新使用它b / c的对象类型每次都基于一个不同的类。

代码的目的是发送HTTP请求,收集JSON响应,然后将JSON(使用DataContract)序列化到类中以便在其他区域中使用。

这就是我所拥有的:

    public static ResponseAttachmentIds MakeRequestAttachmentId(string requestUrl, string strToken)
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            request.Headers["Authorization"] = "OAuth " + strToken;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(ResponseAttachmentIds));
                object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                ResponseAttachmentIds jsonResponse = objResponse as ResponseAttachmentIds;
                response.Close();
                return jsonResponse;
            }
        }
        catch (Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message);
            return null;
        }

    }

此特定用途的DataContract类是:

public class ResponseAttachmentIds
{
    [DataMember(Name = "done")]
    public bool attachmentIds_done;
    [DataMember(Name = "records")]
    public List<ResponseAttachmentId> attachmentIds_records;
}
[DataContract]
public class ResponseAttachmentId
{
    [DataMember(Name = "Id")]
    public string attachmentId_strId { get; set; }
    [DataMember(Name = "Image_Attachment_Id__c")]
    public string attachmentId_strAttachmentId { get; set; }
}

问题在于,我也为大约20个不同的类设置了相同的设置,例如ResponseImagesResponsePropertiesResponseProperty等。

任何帮助?

2 个答案:

答案 0 :(得分:2)

我相信你想要的是一般的方法:

public static T MakeRequestAttachmentId<T>(string requestUrl, string strToken) where T : class
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            request.Headers["Authorization"] = "OAuth " + strToken;
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
                object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                var jsonResponse = (T)objResponse;
                response.Close();
                return jsonResponse;
            }
        }
        catch (Exception e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message);
            return default(T);
        }

    }

你可以这样称呼它:

MakeRequestAttachmentId<ResponseAttachmentIds>("", "");

答案 1 :(得分:2)

我会改变使用泛型的方法。有点像...

public static T MakeResponseObject<T>(string requestUrl, string strToken)
{
    try
    {
        HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
        request.Headers["Authorization"] = "OAuth " + strToken;
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
                throw new Exception(String.Format(
                "Server error (HTTP {0}: {1}).",
                response.StatusCode,
                response.StatusDescription));
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
            object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
            var jsonResponse = objResponse as T;
            response.Close();
            return jsonResponse;
        }
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show(e.Message);
        return null;
    }
}