Skip to content

Conversation

LonghronShen
Copy link

@LonghronShen LonghronShen commented May 4, 2018

Add support for .NET Standard 1.x and Portable Class Library
Note Since .NET Standard 2.0 has added support for ANSI based encodings for asian languages, so this package is not required for that platform.

@Beyley
Copy link

Beyley commented Oct 7, 2021

Sorry for the few year bump, but i was wondering if this could be merged an uploaded to Nuget, as my project uses dotnet 5.0 and i get a build warning because the project technechally isnt "net5.0 compatible" when it works just fine

@jstedfast
Copy link
Owner

@Beyley why do you need this for net50? Why not use https://www.nuget.org/packages/System.Text.Encoding.CodePages ?

@Beyley
Copy link

Beyley commented Oct 7, 2021

@Beyley why do you need this for net50? Why not use https://www.nuget.org/packages/System.Text.Encoding.CodePages ?

well I didn't know that existed lol thanks for letting me know

@jstedfast
Copy link
Owner

jstedfast commented Oct 7, 2021

@Beyley

Add a reference to that nuget and then, in your "Main()" method (or wherever you do initialization for your app), do:

System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);

Or, if your project is a multi-targeted library, what I do is this:

#if NETSTANDARD || NET5_0_OR_GREATER
	var encodingProviderType = typeof (Encoding).Assembly.GetType ("System.Text.EncodingProvider");
	var registerProvider = typeof (Encoding).GetMethod ("RegisterProvider", new Type[] { encodingProviderType });
	if (registerProvider != null) {
		try {
			var assembly = Assembly.Load ("System.Text.Encoding.CodePages");
			if (assembly != null) {
				var providerType = assembly.GetType ("System.Text.CodePagesEncodingProvider");
				var property = providerType.GetProperty ("Instance").GetGetMethod ();
				var instance = property.Invoke (providerType, new object[0]);

				registerProvider.Invoke (typeof (Encoding), new object[] { instance });
			}
		} catch (FileNotFoundException) {
		}
	}
#endif

@jstedfast jstedfast closed this Oct 7, 2021
@jstedfast jstedfast reopened this Oct 7, 2021
@jstedfast
Copy link
Owner

Oops, didn't mean to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants