From 2510f246d37e256e3b87ef7a5c0d926f7db301b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Tue, 21 Apr 2026 21:43:41 +0900 Subject: [PATCH] Fix trimming warnings in System.Diagnostics.Process tests Replace obj.GetType().GetField(...) and assembly.GetType(...) with Type.GetType("FullName, AssemblyName") patterns that the trim analyzer can statically analyze. Re-enable the trim analyzer in the test project. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../tests/ProcessStartInfoTests.cs | 3 +-- .../System.Diagnostics.Process/tests/ProcessTests.Unix.cs | 6 +++--- .../tests/System.Diagnostics.Process.Tests.csproj | 2 -- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs index ecd5a5b3b0bf7e..ba1eb8445aa33c 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs @@ -391,8 +391,7 @@ private string[] ExecuteProcessAndReturnParsedOutput(Dictionary // To mimic this behaviour, we can't use Environment.SetEnvironmentVariable here as it's case-insensitive on Windows. // We also can't use p.StartInfo.Environment as it's comparer is set to OrdinalIgnoreCAse. // But we can overwrite it using reflection to mimic the CreateProcess behaviour and avoid having this test call CreateProcess directly. - p.StartInfo.Environment - .GetType() + Type.GetType("System.Collections.Specialized.DictionaryWrapper, System.Diagnostics.Process")! .GetField("_contents", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance) .SetValue(p.StartInfo.Environment, envVars); diff --git a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs index b4588bd89eca29..1e7399eba08ae0 100644 --- a/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs @@ -960,8 +960,7 @@ Process CreateNonChildProcess() private static IDictionary GetWaitStateDictionary(bool childDictionary) { - Assembly assembly = typeof(Process).Assembly; - Type waitStateType = assembly.GetType("System.Diagnostics.ProcessWaitState"); + Type waitStateType = Type.GetType("System.Diagnostics.ProcessWaitState, System.Diagnostics.Process")!; FieldInfo dictionaryField = waitStateType.GetField(childDictionary ? "s_childProcessWaitStates" : "s_processWaitStates", BindingFlags.NonPublic | BindingFlags.Static); return (IDictionary)dictionaryField.GetValue(null); } @@ -974,7 +973,8 @@ private static object GetProcessWaitState(Process p) private static int GetWaitStateReferenceCount(object waitState) { - FieldInfo referenCountField = waitState.GetType().GetField("_outstandingRefCount", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo referenCountField = Type.GetType("System.Diagnostics.ProcessWaitState, System.Diagnostics.Process")! + .GetField("_outstandingRefCount", BindingFlags.NonPublic | BindingFlags.Instance); return (int)referenCountField.GetValue(waitState); } diff --git a/src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj b/src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj index b967b041ab6d21..ee26045c6cbf20 100644 --- a/src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj +++ b/src/libraries/System.Diagnostics.Process/tests/System.Diagnostics.Process.Tests.csproj @@ -5,8 +5,6 @@ true $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-maccatalyst true - - false