提交 2eb0ed0b 编写于 作者: A Andy Gocke

Merge pull request #5490 from agocke/ChangeGetEncodingCallToUse0

Always use GetEncoding(0) to try getting the default encoding
......@@ -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)
{
......
......@@ -14,36 +14,19 @@ namespace Roslyn.Utilities
/// </summary>
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册