diff --git a/src/Compilers/Core/Portable/EncodedStringText.cs b/src/Compilers/Core/Portable/EncodedStringText.cs index 743fdfaa83804b629faac15ed321cdf6e972a513..e2cd5186d4b2165371c805df9e0cf2f029fb9f77 100644 --- a/src/Compilers/Core/Portable/EncodedStringText.cs +++ b/src/Compilers/Core/Portable/EncodedStringText.cs @@ -30,22 +30,17 @@ private static Encoding GetFallbackEncoding() { try { - if (CoreClrShim.IsCoreClr) + if (CoreClrShim.CodePagesEncodingProvider.Type != null) { - // If we're running on CoreCLR there is no "default" codepage but - // we should be able to grab 1252 from System.Text.Encoding.CodePages + // If we're running on CoreCLR we have to register the CodePagesEncodingProvider + // first CoreClrShim.Encoding.RegisterProvider(CoreClrShim.CodePagesEncodingProvider.Instance); - // We should now have 1252 from the CodePagesEncodingProvider - return PortableShim.Encoding.GetEncoding(1252); - } - else - { - // If we're running on the desktop framework we should be able - // to get the default ANSI code page in the operating system's - // regional and language settings, - return PortableShim.Encoding.GetEncoding(0) - ?? PortableShim.Encoding.GetEncoding(1252); } + + // Try to get the default ANSI code page in the operating system's + // regional and language settings, and fall back to 1252 otherwise + return PortableShim.Encoding.GetEncoding(0) + ?? PortableShim.Encoding.GetEncoding(1252); } catch (NotSupportedException) { diff --git a/src/Compilers/Helpers/CoreClrShim.cs b/src/Compilers/Helpers/CoreClrShim.cs index b1be31233644e3b009004c24ab4740ca4c35cb73..a933bd3be0757246027030f2c5f37c368432400f 100644 --- a/src/Compilers/Helpers/CoreClrShim.cs +++ b/src/Compilers/Helpers/CoreClrShim.cs @@ -14,36 +14,19 @@ namespace Roslyn.Utilities /// internal static class CoreClrShim { - internal static bool IsCoreClr { get; } = - Type.GetType("System.Runtime.Loader.AssemblyLoadContext, System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", - throwOnError: false) != null; - - internal static void Initialize() - { - // This method provides a way to force the static initializers of each type below - // to run. This ensures that the static field values will be computed eagerly - // rather than lazily on demand. If you add a new nested class below to access API - // surface area, be sure to "touch" the Type field here. - - Touch(CodePagesEncodingProvider.Type); - } - - private static void Touch(Type type) - { - // Do nothing. - } - internal static class CodePagesEncodingProvider { internal static readonly Type Type = - Type.GetType("System.Text.CodePagesEncodingProvider, System.Text.Encoding.CodePages, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", - throwOnError: false); + Type.GetType("System.Text.CodePagesEncodingProvider, " + + "System.Text.Encoding.CodePages, " + + "Version=4.0.0.0, Culture=neutral, " + + "PublicKeyToken=b03f5f7f11d50a3a", throwOnError: false); private static PropertyInfo s_instance = Type - .GetTypeInfo() + ?.GetTypeInfo() .GetDeclaredProperty("Instance"); - internal static object Instance => s_instance.GetValue(null); + internal static object Instance => s_instance?.GetValue(null); } internal static class Encoding