我怎样才能从这个 C# 代码中创建一个子例程?
How can I make a subroutine out of this C# code?
我有以下 C# 代码,并且在我的代码中重复使用了大约 20 倍。我不知道如何将它变成一个子程序,这样我就可以重新使用它 b/c 对象类型每次都基于不同的 class。
代码的目的是发送 HTTP 请求,收集 JSON 响应,然后将 JSON(使用 DataContract)序列化为 class 以用于其他领域.
这是我得到的:
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 classes 是:
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 个不同的 class 设置了相同的设置,例如 ResponseImages
、ResponseProperties
、ResponseProperty
等
有什么帮助吗?
我相信你想要的是一个通用方法:
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>("", "");
我会改变使用泛型的方法。像...
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;
}
}
我有以下 C# 代码,并且在我的代码中重复使用了大约 20 倍。我不知道如何将它变成一个子程序,这样我就可以重新使用它 b/c 对象类型每次都基于不同的 class。
代码的目的是发送 HTTP 请求,收集 JSON 响应,然后将 JSON(使用 DataContract)序列化为 class 以用于其他领域.
这是我得到的:
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 classes 是:
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 个不同的 class 设置了相同的设置,例如 ResponseImages
、ResponseProperties
、ResponseProperty
等
有什么帮助吗?
我相信你想要的是一个通用方法:
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>("", "");
我会改变使用泛型的方法。像...
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;
}
}