Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ internal void ToString(TraceFormat traceFormat, StringBuilder sb)
Type? declaringType = mb.DeclaringType;
string methodName = mb.Name;
bool methodChanged = false;
if (declaringType != null && declaringType.IsDefined(typeof(CompilerGeneratedAttribute), inherit: false))
if (declaringType != null && IsDefinedSafe(declaringType, typeof(CompilerGeneratedAttribute), inherit: false))
{
isAsync = declaringType.IsAssignableTo(typeof(IAsyncStateMachine));
if (isAsync || declaringType.IsAssignableTo(typeof(IEnumerator)))
Expand Down Expand Up @@ -384,31 +384,51 @@ private static bool ShowInStackTrace(MethodBase mb)
return false;
}

try
if (IsDefinedSafe(mb, typeof(StackTraceHiddenAttribute), inherit: false))
{
if (mb.IsDefined(typeof(StackTraceHiddenAttribute), inherit: false))
{
// Don't show where StackTraceHidden is applied to the method.
return 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))
{
// Don't show where StackTraceHidden is applied to the containing Type of the method.
return false;
}
Type? declaringType = mb.DeclaringType;
// Methods don't always have containing types, for example dynamic RefEmit generated methods.
if (declaringType != null &&
IsDefinedSafe(declaringType, typeof(StackTraceHiddenAttribute), inherit: false))
{
// Don't show where StackTraceHidden is applied to the containing Type of the method.
return false;
}

return true;
}

private static bool IsDefinedSafe(MemberInfo memberInfo, Type attributeType, bool inherit)
{
try
{
return memberInfo.IsDefined(attributeType, inherit);
}
catch
{
// 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.
// Checking for the attribute has failed, behave as if it was not present. One of
// the reasons can be that the member has attributes defined in an assembly that
// is missing.
return false;
}
}

return true;
private static Attribute[] GetCustomAttributesSafe(MemberInfo memberInfo, Type attributeType, bool inherit)
{
try
{
return Attribute.GetCustomAttributes(memberInfo, attributeType, inherit);
}
catch
{
// Getting the attributes has failed, return an empty array. One of the reasons
// can be that the member has attributes defined in an assembly that is missing.
return [];
}
}

private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type declaringType)
Expand Down Expand Up @@ -438,7 +458,7 @@ private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type

foreach (MethodInfo candidateMethod in methods)
{
StateMachineAttribute[]? attributes = (StateMachineAttribute[])Attribute.GetCustomAttributes(candidateMethod, typeof(StateMachineAttribute), inherit: false);
StateMachineAttribute[]? attributes = (StateMachineAttribute[])GetCustomAttributesSafe(candidateMethod, typeof(StateMachineAttribute), inherit: false);
if (attributes == null)
{
continue;
Expand Down