diff --git a/src/Ben.Demystifier/EnhancedStackTrace.Frames.cs b/src/Ben.Demystifier/EnhancedStackTrace.Frames.cs index f83b54b..5897f70 100644 --- a/src/Ben.Demystifier/EnhancedStackTrace.Frames.cs +++ b/src/Ben.Demystifier/EnhancedStackTrace.Frames.cs @@ -136,19 +136,26 @@ namespace System.Diagnostics { if (methodName == ".cctor") { - var fields = type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - foreach (var field in fields) + if (type.IsGenericTypeDefinition && !type.IsConstructedGenericType) { - var value = field.GetValue(field); - if (value is Delegate d) + // TODO: diagnose type's generic type arguments from frame's "this" or something + } + else + { + var fields = type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + foreach (var field in fields) { - if (ReferenceEquals(d.Method, originMethod) && - d.Target.ToString() == originMethod.DeclaringType.ToString()) + var value = field.GetValue(field); + if (value is Delegate d) { - methodDisplayInfo.Name = field.Name; - methodDisplayInfo.IsLambda = false; - method = originMethod; - break; + if (ReferenceEquals(d.Method, originMethod) && + d.Target.ToString() == originMethod.DeclaringType.ToString()) + { + methodDisplayInfo.Name = field.Name; + methodDisplayInfo.IsLambda = false; + method = originMethod; + break; + } } } } diff --git a/test/Ben.Demystifier.Test/GenericMethodDisplayStringTests.cs b/test/Ben.Demystifier.Test/GenericMethodDisplayStringTests.cs new file mode 100644 index 0000000..2e69164 --- /dev/null +++ b/test/Ben.Demystifier.Test/GenericMethodDisplayStringTests.cs @@ -0,0 +1,42 @@ +using System; +using System.Diagnostics; +using System.Threading.Tasks; +using Xunit; + +namespace Ben.Demystifier.Test +{ + public class GenericMethodDisplayStringTests + { + private static class Example + { + // ReSharper disable once StaticMemberInGenericType + public static readonly StackFrame StackFrame; + + static Example() + { + var fun = new Func(() => new StackFrame(0, true)); + + StackFrame = fun(); + + } + + } + + [Fact] + public void DiagnosesGenericMethodDisplayString() + { + var sf = Example.StackFrame; + + try + { + var s = EnhancedStackTrace.GetMethodDisplayString(sf.GetMethod()); + Assert.True(true, "Does not throw exception when diagnosing generic method display string."); + } + catch (Exception ioe) + { + Assert.True(false, "Must not throw an exception when diagnosing generic method display string."); + } + + } + } +}