Skip to content

Commit

Permalink
Expose AppContext.SetData in ref assembly (dotnet#62996)
Browse files Browse the repository at this point in the history
* Expose AppContext.SetData in ref assembly

* Implement test for Set/GetData

* Add doc comment
  • Loading branch information
deeprobin authored Dec 21, 2021
1 parent 1c09d36 commit 6a9bfee
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public static partial class AppContext
return data;
}

/// <summary>
/// Sets the value of the named data element assigned to the current application domain.
/// </summary>
/// <param name="name">The name of the data element</param>
/// <param name="data">The value of <paramref name="name"/></param>
/// <exception cref="ArgumentNullException">If <paramref name="name"/> is <see langword="null"/></exception>
public static void SetData(string name, object? data)
{
if (name == null)
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 @@ -112,6 +112,7 @@ public static partial class AppContext
public static string BaseDirectory { get { throw null; } }
public static string? TargetFrameworkName { get { throw null; } }
public static object? GetData(string name) { throw null; }
public static void SetData(string name, object? data) { }
public static void SetSwitch(string switchName, bool isEnabled) { }
public static bool TryGetSwitch(string switchName, out bool isEnabled) { throw null; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@
<Compile Include="System\ValueTypeTests.cs" />
<Compile Include="System\VersionTests.cs" />
<Compile Include="System\WeakReferenceTests.cs" />
<Compile Include="System\AppContext\AppContext.Switch.cs" />
<Compile Include="System\AppContext\AppContext.Switch.Validation.cs" />
<Compile Include="System\AppContext\AppContextTests.cs" />
<Compile Include="System\AppContext\AppContextTests.Switch.cs" />
<Compile Include="System\AppContext\AppContextTests.Switch.Validation.cs" />
<Compile Include="System\Collections\Generic\KeyNotFoundExceptionTests.cs" />
<Compile Include="System\Collections\Generic\KeyValuePairTests.cs" />
<Compile Include="System\Collections\ObjectModel\CollectionTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.Tests
{
public partial class AppContextTests
{
[Theory]
[InlineData("AppContext_Case1", 123)]
[InlineData("AppContext_Case2", "")]
[InlineData("AppContext_Case3", null)]
public void AppContext_GetSetDataTest(string dataKey, object value)
{
// Set data
AppContext.SetData(dataKey, value);

// Get previously set data
object actual = AppContext.GetData(dataKey);

// Validate instance equality
Assert.Same(value, actual);
}

[Fact]
public void AppContext_ThrowTest()
{
AssertExtensions.Throws<ArgumentNullException>("name", () => AppContext.SetData(null, 123));
}
}
}

0 comments on commit 6a9bfee

Please sign in to comment.