Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -31,14 +31,12 @@ public void FilePathIsPopulatedCorrectly()
}

/// <summary>
/// Makes Assembly.GetEntryAssembly() return null using private reflection.
/// Makes Assembly.GetEntryAssembly() return null by passing the null literal
/// to Assembly.SetEntryAssembly().
/// </summary>
private static void MakeAssemblyGetEntryAssemblyReturnNull()
{
typeof(Assembly)
.GetField("s_overriddenEntryAssembly", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, true);

Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,12 @@ public void EntryAssemblyName_Null_NotIncludedInTrace()
}

/// <summary>
/// Makes Assembly.GetEntryAssembly() return null using private reflection.
/// Makes Assembly.GetEntryAssembly() return null by passing the null literal
/// to Assembly.SetEntryAssembly().
/// </summary>
private static void MakeAssemblyGetEntryAssemblyReturnNull()
{
typeof(Assembly)
.GetField("s_overriddenEntryAssembly", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, true);

Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public override string ToString()
return type.Module?.Assembly;
}

private static Assembly? s_overriddenEntryAssembly;
private static object? s_overriddenEntryAssembly;

/// <summary>
/// Sets the application's entry assembly to the provided assembly object
Expand All @@ -228,10 +228,17 @@ public override string ToString()
/// </param>
/// <remarks>
/// It is important to mention that the assembly passed to this function
/// has to be a RuntimeAssembly type. Otherwise, an exception will be thrown.
/// 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 = new object();
return;
}

if (assembly is not RuntimeAssembly)
throw new ArgumentException(SR.Argument_MustBeRuntimeAssembly);

Expand All @@ -241,7 +248,7 @@ public static void SetEntryAssembly(Assembly? assembly)
public static Assembly? GetEntryAssembly()
{
if (s_overriddenEntryAssembly is not null)
return s_overriddenEntryAssembly;
return s_overriddenEntryAssembly as Assembly;

return GetEntryAssemblyInternal();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ public void GetEntryAssembly()
Assert.True(correct, $"Unexpected assembly name {assembly}");
}

[Fact]
public void SetEntryAssembly()
{
Assembly? originalAssembly = Assembly.GetEntryAssembly();
Assert.NotNull(originalAssembly);

Assembly.SetEntryAssembly(null);
Assert.Null(Assembly.GetEntryAssembly());

Assembly.SetEntryAssembly(originalAssembly);
Assert.Equal(Assembly.GetEntryAssembly(), originalAssembly);
}

[Fact]
public void GetFile()
{
Expand Down