From 2af5996668eecdeb111f2a89a2f4efbd69cb2b11 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Tue, 14 Apr 2020 12:45:03 +0200 Subject: [PATCH] Fix null reference that we catch internally but would pop up in Debug mode, because the default config is to stop at null ref --- src/testhost.x86/TestHostTraceListener.cs | 31 +++++++++++++---------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/testhost.x86/TestHostTraceListener.cs b/src/testhost.x86/TestHostTraceListener.cs index 0c3fab7790..dda5876dd9 100644 --- a/src/testhost.x86/TestHostTraceListener.cs +++ b/src/testhost.x86/TestHostTraceListener.cs @@ -31,21 +31,26 @@ public static void Setup() EqtTrace.Verbose("TestPlatformTraceListener.Setup: Added test platform trace listener."); -#if NETCOREAPP2_1 - try + // this is a netcoreapp2.1 only fix, but because we always compile against netcoreapp2.1 + // and upgrade the executable as necessary this needs to be a runtime check and not a compile time + // check. This call returns ".NET Core 4.6.xxx" on netcore 2.1 and older, and ".NET Core 3.1.xxx" + // or the respective version on the newer runtimes + if (System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith(".NET Core 4.6")) { - // workaround for netcoreapp2.1 where the trace listener api is not called when - // Debug.Assert fails. This method is internal, but the class is on purpose keeping the - // callback settable so tests can set the callback - var field = typeof(Debug).GetField("s_ShowDialog", BindingFlags.Static | BindingFlags.NonPublic); - var value = field.GetValue(null); - field.SetValue(null, (Action)ShowDialog); - } - catch (Exception ex) - { - EqtTrace.Error("TestPlatformTraceListener.Setup: Failed to replace inner callback to ShowDialog in Debug.Assert. Calls to Debug.Assert with crash the test host process. {0}", ex); + try + { + // workaround for netcoreapp2.1 where the trace listener api is not called when + // Debug.Assert fails. This method is internal, but the class is on purpose keeping the + // callback settable so tests can set the callback + var field = typeof(Debug).GetField("s_ShowDialog", BindingFlags.Static | BindingFlags.NonPublic); + var value = field.GetValue(null); + field.SetValue(null, (Action)ShowDialog); + } + catch (Exception ex) + { + EqtTrace.Error("TestPlatformTraceListener.Setup: Failed to replace inner callback to ShowDialog in Debug.Assert. Calls to Debug.Assert with crash the test host process. {0}", ex); + } } -#endif } public override void Fail(string message)