From b83bc7d61fe1eea7a7cc1ac1c9df5ecf5aea6508 Mon Sep 17 00:00:00 2001 From: Huei Feng <34702552+hueifeng@users.noreply.github.com> Date: Mon, 1 Jun 2020 22:02:06 +0800 Subject: [PATCH 1/2] Delete `StackTraceHelper` code that won't fire These changes can be clearly seen that the corresponding processing has been done above the code block, so it will not be executed in every deleted code block. --- .../StackTrace/StackFrame/StackTraceHelper.cs | 33 +++---------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs b/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs index 48f72438eb2c..1d9a7ff33d52 100644 --- a/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs +++ b/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs @@ -29,12 +29,6 @@ public static IList GetFrames(Exception exception, out Aggregate var stackTrace = new System.Diagnostics.StackTrace(exception, needFileInfo); var stackFrames = stackTrace.GetFrames(); - if (stackFrames == null) - { - error = default; - return frames; - } - List exceptions = null; for (var i = 0; i < stackFrames.Length; i++) @@ -59,12 +53,6 @@ public static IList GetFrames(Exception exception, out Aggregate frames.Add(stackFrame); } - if (exceptions != null) - { - error = new AggregateException(exceptions); - return frames; - } - error = default; return frames; } @@ -118,22 +106,19 @@ internal static MethodDisplayInfo GetMethodDisplayString(MethodBase method) { prefix = "out"; } - else if (parameterType != null && parameterType.IsByRef) + else if (parameterType.IsByRef) { prefix = "ref"; } var parameterTypeString = "?"; - if (parameterType != null) + if (parameterType.IsByRef) { - if (parameterType.IsByRef) - { - parameterType = parameterType.GetElementType(); - } - - parameterTypeString = TypeNameHelper.GetTypeDisplayName(parameterType, fullName: false, includeGenericParameterNames: true); + parameterType = parameterType.GetElementType(); } + parameterTypeString = TypeNameHelper.GetTypeDisplayName(parameterType, fullName: false, includeGenericParameterNames: true); + return new ParameterDisplayInfo { Prefix = prefix, @@ -205,18 +190,10 @@ private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type } var methods = parentType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); - if (methods == null) - { - return false; - } foreach (var candidateMethod in methods) { var attributes = candidateMethod.GetCustomAttributes(); - if (attributes == null) - { - continue; - } foreach (var asma in attributes) { From dce26288f44819838e5bc936062d93572ac9b8a6 Mon Sep 17 00:00:00 2001 From: Huei Feng <34702552+hueifeng@users.noreply.github.com> Date: Wed, 3 Jun 2020 23:29:48 +0800 Subject: [PATCH 2/2] Added `#nullable enable` with `StackTraceHelper` --- .../StackTrace/StackFrame/StackTraceHelper.cs | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs b/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs index 1d9a7ff33d52..27aaf67ea178 100644 --- a/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs +++ b/src/Shared/StackTrace/StackFrame/StackTraceHelper.cs @@ -10,12 +10,13 @@ using System.Runtime.CompilerServices; using System.Runtime.ExceptionServices; using Microsoft.Extensions.Internal; +#nullable enable namespace Microsoft.Extensions.StackTrace.Sources { internal class StackTraceHelper { - public static IList GetFrames(Exception exception, out AggregateException error) + public static IList GetFrames(Exception exception, out AggregateException? error) { var frames = new List(); @@ -29,7 +30,13 @@ public static IList GetFrames(Exception exception, out Aggregate var stackTrace = new System.Diagnostics.StackTrace(exception, needFileInfo); var stackFrames = stackTrace.GetFrames(); - List exceptions = null; + if (stackFrames == null) + { + error = default; + return frames; + } + + List? exceptions = null; for (var i = 0; i < stackFrames.Length; i++) { @@ -53,11 +60,17 @@ public static IList GetFrames(Exception exception, out Aggregate frames.Add(stackFrame); } + if (exceptions != null) + { + error = new AggregateException(exceptions); + return frames; + } + error = default; return frames; } - internal static MethodDisplayInfo GetMethodDisplayString(MethodBase method) + internal static MethodDisplayInfo? GetMethodDisplayString(MethodBase? method) { // Special case: no method available if (method == null) @@ -106,18 +119,21 @@ internal static MethodDisplayInfo GetMethodDisplayString(MethodBase method) { prefix = "out"; } - else if (parameterType.IsByRef) + else if (parameterType != null && parameterType.IsByRef) { prefix = "ref"; } var parameterTypeString = "?"; - if (parameterType.IsByRef) + if (parameterType != null) { - parameterType = parameterType.GetElementType(); - } + if (parameterType.IsByRef) + { + parameterType = parameterType.GetElementType(); + } - parameterTypeString = TypeNameHelper.GetTypeDisplayName(parameterType, fullName: false, includeGenericParameterNames: true); + parameterTypeString = TypeNameHelper.GetTypeDisplayName(parameterType, fullName: false, includeGenericParameterNames: true); + } return new ParameterDisplayInfo { @@ -130,7 +146,7 @@ internal static MethodDisplayInfo GetMethodDisplayString(MethodBase method) return methodDisplayInfo; } - private static bool ShowInStackTrace(MethodBase method) + private static bool ShowInStackTrace(MethodBase? method) { Debug.Assert(method != null); @@ -176,7 +192,7 @@ private static bool ShowInStackTrace(MethodBase method) return true; } - private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type declaringType) + private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type? declaringType) { Debug.Assert(method != null); Debug.Assert(method.DeclaringType != null); @@ -190,10 +206,18 @@ private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type } var methods = parentType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); + if (methods == null) + { + return false; + } foreach (var candidateMethod in methods) { var attributes = candidateMethod.GetCustomAttributes(); + if (attributes == null) + { + continue; + } foreach (var asma in attributes) {