【问题标题】:Avoid fxcop error CA1004 in this case在这种情况下避免 fxcop 错误 CA1004
【发布时间】:2013-04-17 19:28:46
【问题描述】:

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

我还有其他方法可以解决此违规问题吗?

【问题讨论】:

  • CA1006 DoNotNestGenericTypesInMemberSignatures 与这段代码有什么关系?
  • 看起来@Laxmi 的意思是CA1004
  • 在上述情况下,我没有使用 T 作为参数。为了解决这个问题,我必须使用虚拟参数 T a = null。是的..它是CA1004

标签: c# warnings fxcop


【解决方案1】:

按照here 的建议,您可以使用out 参数来传达您的结果:

public static void GetObjectsFromWebRequest<T>(string urlPath, out T objects) ...

【讨论】:

    猜你喜欢
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多