How can I use SoapException Method on object of type Exception?

断了今生、忘了曾经 提交于 2019-12-13 04:39:11

问题


I have a Method that get paramater of type - Exception

WriteException(Exception ex, int index, string s)
{
   // my code here...
}

sometimes the method gets an Exception object and sometimes SoapException
every time the exeption is of kind SoapException I want print: ex.Detail.InnerText
but if ex is of type Exception.
so after I recognize the type, how can I do SoapException ex.Detail.InnerText?


回答1:


WriteException(Exception ex, int index, string s)
{
    var soapEx = ex as SoapException;

    if(null != soapEx)
    {
      Console.WriteLine(soapEx.Detail.InnerText);
      return;
    }

    Console.WriteLine(ex.Message);

}

another possible solution uses the dynamic keyword:

WriteException(Exception ex, int index, string s)
{
    dynamic soapEx = ex;

    Console.WriteLine(soapEx.Detail.InnerText);
    Console.WriteLine(ex.Message);

}


来源:https://stackoverflow.com/questions/8922400/how-can-i-use-soapexception-method-on-object-of-type-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!