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
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 @@ -19,7 +19,8 @@ public void FilePathIsPopulatedCorrectly()
{
RemoteExecutor.Invoke(() =>
{
MakeAssemblyGetEntryAssemblyReturnNull();
Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());

string expectedFilePathEnding = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
"dotnet.exe.config" :
Expand All @@ -29,17 +30,5 @@ public void FilePathIsPopulatedCorrectly()
Assert.EndsWith(expectedFilePathEnding, config.FilePath);
}).Dispose();
}

/// <summary>
/// Makes Assembly.GetEntryAssembly() return null using private reflection.
/// </summary>
private static void MakeAssemblyGetEntryAssemblyReturnNull()
{
typeof(Assembly)
.GetField("s_forceNullEntryPoint", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, true);

Assert.Null(Assembly.GetEntryAssembly());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,14 @@ public void EntryAssemblyName_Null_NotIncludedInTrace()
{
RemoteExecutor.Invoke(() =>
{
MakeAssemblyGetEntryAssemblyReturnNull();
Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());

var listener = new TestDefaultTraceListener();
Trace.Listeners.Add(listener);
Trace.TraceError("hello world");
Assert.Equal("Error: 0 : hello world", listener.Output.Trim());
}).Dispose();
}

/// <summary>
/// Makes Assembly.GetEntryAssembly() return null using private reflection.
/// </summary>
private static void MakeAssemblyGetEntryAssemblyReturnNull()
{
typeof(Assembly)
.GetField("s_forceNullEntryPoint", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, true);

Assert.Null(Assembly.GetEntryAssembly());
}
}
}
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
1 change: 1 addition & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11157,6 +11157,7 @@ public virtual void GetObjectData(System.Runtime.Serialization.SerializationInfo
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types and members the loaded assembly depends on might be removed")]
[System.ObsoleteAttribute("ReflectionOnly loading is not supported and throws PlatformNotSupportedException.", DiagnosticId="SYSLIB0018", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public static System.Reflection.Assembly ReflectionOnlyLoadFrom(string assemblyFile) { throw null; }
public static void SetEntryAssembly(System.Reflection.Assembly? assembly) { throw null; }
public override string ToString() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types and members the loaded assembly depends on might be removed")]
public static System.Reflection.Assembly UnsafeLoadFrom(string assemblyFile) { throw null; }
Expand Down
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,32 @@ public void GetEntryAssembly()
Assert.True(correct, $"Unexpected assembly name {assembly}");
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void SetEntryAssembly()
{
Assert.NotNull(Assembly.GetEntryAssembly());

RemoteExecutor.Invoke(() =>
{
Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());

Assembly testAssembly = typeof(AssemblyTests).Assembly;

Assembly.SetEntryAssembly(testAssembly);
Assert.Equal(Assembly.GetEntryAssembly(), testAssembly);

var invalidAssembly = new PersistedAssemblyBuilder(
new AssemblyName("NotaRuntimeAssemblyTest"),
typeof(object).Assembly
);

Assert.Throws<ArgumentException>(
() => Assembly.SetEntryAssembly(invalidAssembly)
);
}).Dispose();
}

[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