diff --git a/src/libraries/System.Private.CoreLib/src/System/AppContext.cs b/src/libraries/System.Private.CoreLib/src/System/AppContext.cs index 35e45f472bb8b1..6b28c7b2a561d7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/AppContext.cs +++ b/src/libraries/System.Private.CoreLib/src/System/AppContext.cs @@ -45,6 +45,12 @@ public static partial class AppContext return data; } + /// + /// Sets the value of the named data element assigned to the current application domain. + /// + /// The name of the data element + /// The value of + /// If is public static void SetData(string name, object? data) { if (name == null) diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index f9462613a698b3..435594f5b9402e 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -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; } } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj index 805d3610003be0..92eb65f2c0ebce 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj @@ -144,8 +144,9 @@ - - + + + diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.Switch.Validation.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.Switch.Validation.cs similarity index 100% rename from src/libraries/System.Runtime/tests/System/AppContext/AppContext.Switch.Validation.cs rename to src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.Switch.Validation.cs diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.Switch.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.Switch.cs similarity index 100% rename from src/libraries/System.Runtime/tests/System/AppContext/AppContext.Switch.cs rename to src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.Switch.cs diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs new file mode 100644 index 00000000000000..9b47feed876c80 --- /dev/null +++ b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs @@ -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("name", () => AppContext.SetData(null, 123)); + } + } +}