网络标准库的 Shift-JIS 编码

Shift-JIS encoding for a netstandard library

net45 中,Encoding.GetEncoding("Shift-JIS") 工作正常,但在 netstandard 下它抛出:

System.ArgumentException : 'Shift-JIS' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

它引用的文档提到通过 CodePagesEncodingProvider 支持 UWP 下的 .NET Core Native,但没有提及 netstandard.

的一般用法

那么,是否可以在 netstandard 库中使用 Shift-JIS 编码?

是的,这是可能的。 project.json 中的参考 System.Text.Encoding.CodePages 包:

"System.Text.Encoding.CodePages": "4.0.1"

在获取 Shift-JIS 编码之前调用以下代码:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

你很适合:

Encoding.GetEncoding("Shift-JIS")

更新:

System.Text.Encoding.CodePages 未捆绑在 NETStandard.Library 1.6 包 中,但从您的 [=14= 引用 System.Text.Encoding.CodePages 没有问题] class 库(直到你的 class 库目标 netstandard1.2 或更低)。

这里是sample solution with the code。有一个 class 库,它以 netstandard1.3 为目标,还有一个消费控制台应用程序,它以 netcoreapp1.0 为目标并引用 class 库。 class 库包含检索 Shift-JIS 编码对应的代码。它也可以从针对其他框架的应用程序中引用和使用。