Skip to content

Commit 0e7923c

Browse files
authored
Fix null reference (#2400)
that we catch internally but would pop up in Debug mode, because the default config is to stop at null ref
1 parent 970d175 commit 0e7923c

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/testhost.x86/TestHostTraceListener.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,26 @@ public static void Setup()
3131

3232
EqtTrace.Verbose("TestPlatformTraceListener.Setup: Added test platform trace listener.");
3333

34-
#if NETCOREAPP2_1
35-
try
34+
// this is a netcoreapp2.1 only fix, but because we always compile against netcoreapp2.1
35+
// and upgrade the executable as necessary this needs to be a runtime check and not a compile time
36+
// check. This call returns ".NET Core 4.6.xxx" on netcore 2.1 and older, and ".NET Core 3.1.xxx"
37+
// or the respective version on the newer runtimes
38+
if (System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith(".NET Core 4.6"))
3639
{
37-
// workaround for netcoreapp2.1 where the trace listener api is not called when
38-
// Debug.Assert fails. This method is internal, but the class is on purpose keeping the
39-
// callback settable so tests can set the callback
40-
var field = typeof(Debug).GetField("s_ShowDialog", BindingFlags.Static | BindingFlags.NonPublic);
41-
var value = field.GetValue(null);
42-
field.SetValue(null, (Action<string, string, string, string>)ShowDialog);
43-
}
44-
catch (Exception ex)
45-
{
46-
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);
40+
try
41+
{
42+
// workaround for netcoreapp2.1 where the trace listener api is not called when
43+
// Debug.Assert fails. This method is internal, but the class is on purpose keeping the
44+
// callback settable so tests can set the callback
45+
var field = typeof(Debug).GetField("s_ShowDialog", BindingFlags.Static | BindingFlags.NonPublic);
46+
var value = field.GetValue(null);
47+
field.SetValue(null, (Action<string, string, string, string>)ShowDialog);
48+
}
49+
catch (Exception ex)
50+
{
51+
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);
52+
}
4753
}
48-
#endif
4954
}
5055

5156
public override void Fail(string message)

0 commit comments

Comments
 (0)