我有一种情况,它返回T类型的对象。我的代码看起来像这样。
public static T GetObjectsFromWebRequest<T>(string urlPath) where T : class
{
T modelObjects;
try
{
//SaveServiceDataIntoTextFile(urlPath);
WebRequest request = WebRequest.Create(urlPath);
WebResponse ws = request.GetResponse();
StreamReader responseStream = new StreamReader(ws.GetResponseStream());
//Get the response of the webrequest into a string
string response = responseStream.ReadToEnd();
modelObjects = XMLSerializeDeserialize.ConvertXMLToModel<T>(response);
}
catch (Exception)
{
throw;
}
return modelObjects;
}
在这种情况下,我没有任何选项,只能添加默认参数,如
public static T GetObjectsFromWebRequest<T>(string urlPath, T a = null) where T : class
我可以通过其他方式解决此违规行为吗?
答案 0 :(得分:0)
根据建议here,您可以使用out
参数来传达结果:
public static void GetObjectsFromWebRequest<T>(string urlPath, out T objects) ...