【问题标题】:Shift-JIS encoding for a netstandard library用于网络标准库的 Shift-JIS 编码
【发布时间】:2017-03-12 22:23:09
【问题描述】:

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

System.ArgumentException : 'Shift-JIS' 不是受支持的编码名称。有关定义自定义编码的信息,请参阅 Encoding.RegisterProvider 方法的文档。

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

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

【问题讨论】:

  • CodePagesEncodingProvider 附带 System.Text.Encoding.CodePages 包,它不是 netstandard (github.com/dotnet/corefx/blob/master/pkg/NETStandard.Library/…) 的一部分,而是 netcore 的一部分,所以我猜,答案是否定的,除非你写您自己的 Encoding 和 EncodingProvider。我相信 System.Text.Encoding.CodePages 不属于 netstandard 的原因是因为它现在依赖于 Windows API/资源。

标签: .net character-encoding .net-core .net-standard


【解决方案1】:

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

"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 package 中,但从您的netstandard 类库中引用System.Text.Encoding.CodePages 没有问题(直到您的类库以netstandard1.2 或更低版本为目标)。

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

【讨论】:

  • Simon Mourier 已经指出了这一点。 .NET Core 支持它,但 netstandard 不支持。我正在创作一个netstandard 库,所以不能使用它。尽管问题中对此进行了解释,但我将更新问题的标题以使其更加清晰。另外,我已经在我的问题中提到了你的答案。
  • “不支持”似乎是一个 SO 似乎没有人理解的短语。急性阅读理解问题不是你要解决的问题,它们不是真正的问题。很好的答案。
  • @Drew Noakes 我已根据您的评论更新了答案。
  • @HansPassant 不知道你在说什么,抱歉。
  • @Delian,非常感谢您的更新。我会试一试,但它看起来很完美。你真的在那个编辑上加倍努力。我相信metadata extractor 的日本用户也会感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-25
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多