我有一个案例,它返回 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
有没有其他方法可以解决此违规行为?