我有一个获取类型为- Exception的参数的方法
WriteException(Exception ex, int index, string s)
{
// my code here...
}该方法有时获取Exception对象,有时获取SoapException对象
每次exeption是SoapException类型的时候,我都想打印出来:ex.Detail.InnerText
但是如果ex是Exception类型的话。
那么在我识别了类型之后,我该如何执行SoapException ex.Detail.InnerText呢
发布于 2012-01-19 15:27:56
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);
}另一种可能的解决方案是使用dynamic关键字:
WriteException(Exception ex, int index, string s)
{
dynamic soapEx = ex;
Console.WriteLine(soapEx.Detail.InnerText);
Console.WriteLine(ex.Message);
}https://stackoverflow.com/questions/8922400
复制相似问题