From 0a03aa9b81eeac93a6c6fa238d4123a60009d0f6 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Tue, 1 Jan 2019 21:13:31 +0100 Subject: [PATCH] Moves AppContext to shared location (#21734) --- .../System.Private.CoreLib.csproj | 2 +- .../shared/System.Private.CoreLib.Shared.projitems | 1 + .../{src => shared}/System/AppContext.cs | 29 +++++----------------- .../src/System/AppContext.CoreCLR.cs | 29 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 24 deletions(-) rename src/System.Private.CoreLib/{src => shared}/System/AppContext.cs (82%) create mode 100644 src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs diff --git a/src/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/System.Private.CoreLib/System.Private.CoreLib.csproj index be9dabd..0541c4b 100644 --- a/src/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -125,7 +125,7 @@ - + diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 7364bb2..6a93908 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -29,6 +29,7 @@ + diff --git a/src/System.Private.CoreLib/src/System/AppContext.cs b/src/System.Private.CoreLib/shared/System/AppContext.cs similarity index 82% rename from src/System.Private.CoreLib/src/System/AppContext.cs rename to src/System.Private.CoreLib/shared/System/AppContext.cs index d559e88..11760ad 100644 --- a/src/System.Private.CoreLib/src/System/AppContext.cs +++ b/src/System.Private.CoreLib/shared/System/AppContext.cs @@ -2,9 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. - using System.Collections.Generic; -using System.IO; using System.Reflection; using System.Runtime.ExceptionServices; using System.Runtime.Loader; @@ -13,18 +11,11 @@ using System.Threading; namespace System { - public static class AppContext + public static partial class AppContext { private static readonly Dictionary s_dataStore = new Dictionary(); private static Dictionary s_switches; - - internal static unsafe void Setup(char** pNames, char** pValues, int count) - { - for (int i = 0; i < count; i++) - { - s_dataStore.Add(new string(pNames[i]), new string(pValues[i])); - } - } + private static string s_defaultBaseDirectory; public static string BaseDirectory { @@ -32,15 +23,8 @@ namespace System { // The value of APP_CONTEXT_BASE_DIRECTORY key has to be a string and it is not allowed to be any other type. // Otherwise the caller will get invalid cast exception - string baseDirectory = (string)GetData("APP_CONTEXT_BASE_DIRECTORY"); - if (baseDirectory != null) - return baseDirectory; - - // Fallback path for hosts that do not set APP_CONTEXT_BASE_DIRECTORY explicitly - string directory = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); - if (directory != null && !PathInternal.EndsInDirectorySeparator(directory)) - directory += Path.DirectorySeparatorChar; - return directory; + return (string)GetData("APP_CONTEXT_BASE_DIRECTORY") ?? + (s_defaultBaseDirectory ?? (s_defaultBaseDirectory = GetBaseDirectoryCore())); } } @@ -116,10 +100,9 @@ namespace System } string value = GetData(switchName) as string; - if (value != null) + if (value != null && bool.TryParse(value, out isEnabled)) { - if (bool.TryParse(value, out isEnabled)) - return true; + return true; } isEnabled = false; diff --git a/src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs b/src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs new file mode 100644 index 0000000..4d11c16 --- /dev/null +++ b/src/System.Private.CoreLib/src/System/AppContext.CoreCLR.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.IO; +using System.Reflection; + +namespace System +{ + public static partial class AppContext + { + internal static unsafe void Setup(char** pNames, char** pValues, int count) + { + for (int i = 0; i < count; i++) + { + s_dataStore.Add(new string(pNames[i]), new string(pValues[i])); + } + } + + private static string GetBaseDirectoryCore() + { + // Fallback path for hosts that do not set APP_CONTEXT_BASE_DIRECTORY explicitly + string directory = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location); + if (directory != null && !PathInternal.EndsInDirectorySeparator(directory)) + directory += Path.DirectorySeparatorChar; + return directory; + } + } +} -- 2.7.4