diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs index 72858d2b4914f7..3c6ba961c79d30 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs @@ -354,19 +354,28 @@ private static bool ShowInStackTrace(MethodBase mb) return false; } - if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false)) + try { - // Don't show where StackTraceHidden is applied to the method. - return false; - } + if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false)) + { + // Don't show where StackTraceHidden is applied to the method. + return false; + } - Type? declaringType = mb.DeclaringType; - // Methods don't always have containing types, for example dynamic RefEmit generated methods. - if (declaringType != null && - declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false)) + Type? declaringType = mb.DeclaringType; + // Methods don't always have containing types, for example dynamic RefEmit generated methods. + if (declaringType != null && + declaringType.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false)) + { + // Don't show where StackTraceHidden is applied to the containing Type of the method. + return false; + } + } + catch { - // Don't show where StackTraceHidden is applied to the containing Type of the method. - return false; + // Getting the StackTraceHiddenAttribute has failed, behave as if it was not present. + // One of the reasons can be that the method mb or its declaring type use attributes + // defined in an assembly that is missing. } return true;