2013-04-24 121 views
0

我有一种情况,它返回类型T的对象。我的代码看起来像这样。在这种情况下避免fxcop错误CA1004

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

是什么CA1006'DoNotNestGenericTypesInMemberSignatures'得到了与此代码吗? – adrianm 2013-04-24 09:52:42

+1

看起来像@Laxmi意味着[CA1004](http://msdn.microsoft.com/en-us/library/ms182150.aspx) – dtb 2013-04-24 09:58:12

+0

在上面的情况下,我没有使用T作为参数。为了解决这个问题,我必须使用虚拟参数T a = null。是的,它是CA1004 – Laxmi 2013-04-24 09:58:51

回答

0

至于建议here,你可以使用一个out参数来传达你的结果:

public static void GetObjectsFromWebRequest<T>(string urlPath, out T objects) ... 
相关问题