diff --git a/src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml b/src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml index 276be3a543e343..ed348d066b9961 100644 --- a/src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml +++ b/src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml @@ -1,5 +1,12 @@  + + + CP0002 + M:System.Reflection.Assembly.SetEntryAssembly(System.Reflection.Assembly) + ref/net8.0/System.Private.CoreLib.dll + lib/net8.0/System.Private.CoreLib.dll + CP0015 M:System.Diagnostics.Tracing.EventSource.Write``1(System.String,``0):[T:System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute] diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/CompatibilitySuppressions.xml b/src/coreclr/nativeaot/System.Private.CoreLib/src/CompatibilitySuppressions.xml index 229085a10afaa0..78543b6e04a4e0 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/CompatibilitySuppressions.xml +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/CompatibilitySuppressions.xml @@ -1,4 +1,5 @@  + CP0001 @@ -960,6 +961,12 @@ CP0002 M:System.TypedReference.get_IsNull + + CP0002 + M:System.Reflection.Assembly.SetEntryAssembly(System.Reflection.Assembly) + ref/net8.0/System.Private.CoreLib.dll + lib/net8.0/System.Private.CoreLib.dll + CP0014 M:System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(System.Object)->object?:[T:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute] diff --git a/src/libraries/System.Configuration.ConfigurationManager/tests/System.Configuration.ConfigurationManager.Tests.csproj b/src/libraries/System.Configuration.ConfigurationManager/tests/System.Configuration.ConfigurationManager.Tests.csproj index e7881df77eb922..d4f8b92692a2db 100644 --- a/src/libraries/System.Configuration.ConfigurationManager/tests/System.Configuration.ConfigurationManager.Tests.csproj +++ b/src/libraries/System.Configuration.ConfigurationManager/tests/System.Configuration.ConfigurationManager.Tests.csproj @@ -59,7 +59,7 @@ - + diff --git a/src/libraries/System.Configuration.ConfigurationManager/tests/System/Configuration/CustomHostTests.cs b/src/libraries/System.Configuration.ConfigurationManager/tests/System/Configuration/CustomHostTests.cs index e8f69c198cf82d..7d103225cd5e51 100644 --- a/src/libraries/System.Configuration.ConfigurationManager/tests/System/Configuration/CustomHostTests.cs +++ b/src/libraries/System.Configuration.ConfigurationManager/tests/System/Configuration/CustomHostTests.cs @@ -36,8 +36,8 @@ public void FilePathIsPopulatedCorrectly() private static void MakeAssemblyGetEntryAssemblyReturnNull() { typeof(Assembly) - .GetField("s_forceNullEntryPoint", BindingFlags.NonPublic | BindingFlags.Static) - .SetValue(null, true); + .GetMethod("SetEntryAssembly", BindingFlags.Public | BindingFlags.Static) + .Invoke(null, new object[]{ null }); Assert.Null(Assembly.GetEntryAssembly()); } diff --git a/src/libraries/System.Diagnostics.TraceSource/tests/System.Diagnostics.TraceSource.Tests/DefaultTraceListenerClassTests.cs b/src/libraries/System.Diagnostics.TraceSource/tests/System.Diagnostics.TraceSource.Tests/DefaultTraceListenerClassTests.cs index efbb26a53a7a9e..d449a90e6dead8 100644 --- a/src/libraries/System.Diagnostics.TraceSource/tests/System.Diagnostics.TraceSource.Tests/DefaultTraceListenerClassTests.cs +++ b/src/libraries/System.Diagnostics.TraceSource/tests/System.Diagnostics.TraceSource.Tests/DefaultTraceListenerClassTests.cs @@ -174,8 +174,8 @@ public void EntryAssemblyName_Null_NotIncludedInTrace() private static void MakeAssemblyGetEntryAssemblyReturnNull() { typeof(Assembly) - .GetField("s_forceNullEntryPoint", BindingFlags.NonPublic | BindingFlags.Static) - .SetValue(null, true); + .GetMethod("SetEntryAssembly", BindingFlags.Public | BindingFlags.Static) + .Invoke(null, new object[]{ null }); Assert.Null(Assembly.GetEntryAssembly()); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs index 15e753aaee3792..ec817ea84a6afe 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs @@ -217,13 +217,36 @@ public override string ToString() return type.Module?.Assembly; } - // internal test hook - private static bool s_forceNullEntryPoint; + private static object? s_overriddenEntryAssembly; + + /// + /// Sets the application's entry assembly to the provided assembly object. + /// + /// + /// Assembly object that represents the application's new entry assembly. + /// + /// + /// The assembly passed to this function has to be a runtime defined Assembly + /// type object. Otherwise, an exception will be thrown. + /// + public static void SetEntryAssembly(Assembly? assembly) + { + if (assembly is null) + { + s_overriddenEntryAssembly = string.Empty; + return; + } + + if (assembly is not RuntimeAssembly) + throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly); + + s_overriddenEntryAssembly = assembly; + } public static Assembly? GetEntryAssembly() { - if (s_forceNullEntryPoint) - return null; + if (s_overriddenEntryAssembly is not null) + return s_overriddenEntryAssembly as Assembly; return GetEntryAssemblyInternal(); } diff --git a/src/libraries/System.Reflection/tests/AssemblyTests.cs b/src/libraries/System.Reflection/tests/AssemblyTests.cs index 600a4733b40c9c..be2dd954f393bc 100644 --- a/src/libraries/System.Reflection/tests/AssemblyTests.cs +++ b/src/libraries/System.Reflection/tests/AssemblyTests.cs @@ -7,10 +7,12 @@ using System.Globalization; using System.IO; using System.Linq; +using System.Reflection.Emit; using System.Reflection.Tests; using System.Runtime.CompilerServices; using System.Security; using System.Text; +using Microsoft.DotNet.RemoteExecutor; using Xunit; [assembly: @@ -181,6 +183,30 @@ public void GetEntryAssembly() Assert.True(correct, $"Unexpected assembly name {assembly}"); } + [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] + public void TestSetEntryAssembly() + { + Assert.NotNull(Assembly.GetEntryAssembly()); + + RemoteExecutor.Invoke(() => + { + SetEntryAssembly(null); + Assert.Null(Assembly.GetEntryAssembly()); + + Assembly testAssembly = typeof(AssemblyTests).Assembly; + + SetEntryAssembly(testAssembly); + Assert.Equal(Assembly.GetEntryAssembly(), testAssembly); + }).Dispose(); + } + + private static void SetEntryAssembly(Assembly assembly) + { + typeof(Assembly) + .GetMethod("SetEntryAssembly", BindingFlags.Public | BindingFlags.Static) + .Invoke(null, new object[]{ assembly }); + } + [Fact] public void GetFile() { diff --git a/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj b/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj index 85de60a759bf85..a5cf23579908ec 100644 --- a/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj +++ b/src/libraries/System.Reflection/tests/System.Reflection.Tests.csproj @@ -2,6 +2,7 @@ $(NetCoreAppCurrent) true + true true diff --git a/src/mono/System.Private.CoreLib/CompatibilitySuppressions.xml b/src/mono/System.Private.CoreLib/CompatibilitySuppressions.xml index ed9aefadda0057..cc3ab5d098e221 100644 --- a/src/mono/System.Private.CoreLib/CompatibilitySuppressions.xml +++ b/src/mono/System.Private.CoreLib/CompatibilitySuppressions.xml @@ -1,5 +1,12 @@  + + + CP0002 + M:System.Reflection.Assembly.SetEntryAssembly(System.Reflection.Assembly) + ref/net8.0/System.Private.CoreLib.dll + lib/net8.0/System.Private.CoreLib.dll + CP0014 M:System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(System.Object)->object?:[T:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute]