2013-04-24 91 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) ... 
相關問題