Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:System.Reflection.Assembly.SetEntryAssembly(System.Reflection.Assembly)</Target>
<Left>ref/net8.0/System.Private.CoreLib.dll</Left>
<Right>lib/net8.0/System.Private.CoreLib.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0015</DiagnosticId>
<Target>M:System.Diagnostics.Tracing.EventSource.Write``1(System.String,``0):[T:System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute]</Target>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
Expand Down Expand Up @@ -960,6 +961,12 @@
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:System.TypedReference.get_IsNull</Target>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:System.Reflection.Assembly.SetEntryAssembly(System.Reflection.Assembly)</Target>
<Left>ref/net8.0/System.Private.CoreLib.dll</Left>
<Right>lib/net8.0/System.Private.CoreLib.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0014</DiagnosticId>
<Target>M:System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(System.Object)-&gt;object?:[T:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute]</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<Compile Include="System\Configuration\ConfigurationElementTests.cs" />
<Compile Include="System\Configuration\ConfigurationPropertyAttributeTests.cs" />
<Compile Include="System\Configuration\ConfigurationPathTests.cs" />
<Compile Include="System\Configuration\CustomHostTests.cs" />
<Compile Include="System\Configuration\CustomHostTests.cs" Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'" />
<Compile Include="System\Configuration\ConfigurationPropertyTests.cs" />
<Compile Include="System\Configuration\ConfigurationTests.cs" />
<Compile Include="System\Configuration\BasicCustomSectionTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Sets the application's entry assembly to the provided assembly object.
/// </summary>
/// <param name="assembly">
/// Assembly object that represents the application's new entry assembly.
/// </param>
/// <remarks>
/// The assembly passed to this function has to be a runtime defined Assembly
/// type object. Otherwise, an exception will be thrown.
/// </remarks>
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();
}
Expand Down
26 changes: 26 additions & 0 deletions src/libraries/System.Reflection/tests/AssemblyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TestRuntime>true</TestRuntime>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- SYSLIB0013: Uri.EscapeUriString is obsolete
SYSLIB0037: AssemblyName members HashAlgorithm, ProcessorArchitecture, and VersionCompatibility are obsolete. -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:System.Reflection.Assembly.SetEntryAssembly(System.Reflection.Assembly)</Target>
<Left>ref/net8.0/System.Private.CoreLib.dll</Left>
<Right>lib/net8.0/System.Private.CoreLib.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0014</DiagnosticId>
<Target>M:System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(System.Object)-&gt;object?:[T:System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute]</Target>
Expand Down