问题
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