繁体   English   中英

检查对象是否为空或NULL

[英]Check if object is empty or NULL

我想检查对象是否为空或为NULL。

首先我有一个Web方法,该方法带有输入参数XML文档

[WebMethod(CacheDuration = 0, EnableSession=true, Description = "Učitaj dokument iz Aurore")]
public System.Xml.XmlDocument Load_DOK(System.Xml.XmlDocument XmlDoc)   //xml doc
{
}

在这种方法中,我必须检查XmlDoc是否为空,如果抛出错误。

我写了这样的东西:

try
{
    if( XmlDoc == null)
        errorMessage = "Input parameter is NULL!";
}
catch (Exception ex)
{
    WriteErrors.WriteToLogFile("WS.LOAD_DOK", ex.ToString());

    errorMessage = ex.Message;

    //Error exception
    soapEnvelop.LoadXml(@"<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body><Response_status>1</Response_status><Description>" + ex.Message + "</Description></soap:Body></soap:Envelope>");
    return soapEnvelop;
}

我想知道这是正确的方法还是有更简单的方法?

您的try块应该是这样的

try 
{ 
    if( XmlDoc == null) 
    {
        throw new ArgumentNullException("XmlDoc");
    } 
    // carry on processing here.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM