Drew Noakes
Drew Noakes

Reputation: 311245

Shift-JIS encoding for a netstandard library

In net45, Encoding.GetEncoding("Shift-JIS") works fine, but under netstandard it throws:

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.

The documentation it refers to mentions support via CodePagesEncodingProvider for .NET Core Native under UWP, but nothing for general usage with netstandard.

So, is it possible to use Shift-JIS encoding within a netstandard library?

Upvotes: 8

Views: 4873

Answers (1)

Deilan
Deilan

Reputation: 4886

Yes, it is possible. Reference System.Text.Encoding.CodePages package in project.json:

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

Invoke the following code prior to getting the Shift-JIS encoding:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

And you're good to go with:

Encoding.GetEncoding("Shift-JIS")

UPDATE:

System.Text.Encoding.CodePages is not bundled in NETStandard.Library 1.6 package, but there is no problem to reference System.Text.Encoding.CodePages from your netstandard class library (until your class library targets netstandard1.2 or lower).

Here is a sample solution with the code. There is a class library, which targets netstandard1.3 and a consuming console application, which targets netcoreapp1.0 and references the class library. The class library contains the code corresponding to retrieval of Shift-JIS encoding. It may also be referenced and consumed from apps targeting other frameworks.

Upvotes: 10

Related Questions