From 19698c7191de10c6df5ca774ec78a5d2e904f8be Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 12 Oct 2017 22:55:50 +0100 Subject: [PATCH] Added GetEncoding() override. --- Claunia.Encoding.Tests/GetEncs.cs | 16 +++++++++++++++- Claunia.Encoding/Encoding.cs | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Claunia.Encoding.Tests/GetEncs.cs b/Claunia.Encoding.Tests/GetEncs.cs index b086823..04a1fea 100644 --- a/Claunia.Encoding.Tests/GetEncs.cs +++ b/Claunia.Encoding.Tests/GetEncs.cs @@ -52,5 +52,19 @@ namespace Claunia.Encoding.Tests Console.WriteLine("{0,-8} {1,-8} ", e.IsSingleByte, e.IsReadOnly); } } - } + + [Test] + // Well basically this is taken from MSDN's documentation :p + public void GetInternalEncoding() + { + System.Text.Encoding e = Encoding.GetEncoding("lisa"); + } + + [Test] + // Well basically this is taken from MSDN's documentation :p + public void GetExternalEncoding() + { + System.Text.Encoding e = Encoding.GetEncoding("shift_jis"); + } + } } diff --git a/Claunia.Encoding/Encoding.cs b/Claunia.Encoding/Encoding.cs index 4cb3a5a..e3673b8 100644 --- a/Claunia.Encoding/Encoding.cs +++ b/Claunia.Encoding/Encoding.cs @@ -70,6 +70,24 @@ namespace Claunia.Encoding return encodings.ToArray(); } + /// + /// Returns the encoding associated with the specified code page name. + /// + /// The encoding associated with the specified code page. + /// The code page name of the preferred encoding. Any value returned by the WebName property is valid. Possible values are listed in the Name column of the table that appears in the Encoding class topic. + public static System.Text.Encoding GetEncoding(string name) + { + foreach(Type type in Assembly.GetExecutingAssembly().GetTypes()) { + if(type.IsSubclassOf(typeof(Encoding))) { + Encoding encoding = (Encoding)type.GetConstructor(new Type[] { }).Invoke(new object[] { }); + if(encoding.BodyName == name.ToLowerInvariant()) + return encoding; + } + } + + return System.Text.Encoding.GetEncoding(name); + } + /// /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. ///