0

我有一个案例,它返回 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

有没有其他方法可以解决此违规行为?

4

1 回答 1

0

正如这里所建议的,您可以使用一个out参数来传达您的结果:

public static void GetObjectsFromWebRequest<T>(string urlPath, out T objects) ...
于 2016-05-02T15:08:40.580 回答