首页 > 代码库 > 判断Exception类中是否有InnerException属性

判断Exception类中是否有InnerException属性

public static class ExceptionExtend
{
/// <summary>
/// 利用反射来判断对象是否包含某个属性
/// </summary>
/// <param name="instance">object</param>
/// <param name="propertyName">需要判断的属性</param>
/// <returns>是否包含</returns>
public static bool ContainProperty(this object instance, string propertyName)
{
if (instance != null && !string.IsNullOrEmpty(propertyName))
{
PropertyInfo _findedPropertyInfo = instance.GetType().GetProperty(propertyName);
return (_findedPropertyInfo != null);
}
return false;
}
/// <summary>
/// 获取错误信息
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
public static string GetExceptionMessage(Exception ex)
{
return ContainProperty(ex, "InnerException") == true ? ex.InnerException.Message : ex.Message;
}
}

判断Exception类中是否有InnerException属性