Added GetEncoding() override.

This commit is contained in:
2017-10-12 22:55:50 +01:00
parent 5832c3c487
commit 19698c7191
2 changed files with 33 additions and 1 deletions

View File

@@ -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");
}
}
}

View File

@@ -70,6 +70,24 @@ namespace Claunia.Encoding
return encodings.ToArray();
}
/// <summary>
/// Returns the encoding associated with the specified code page name.
/// </summary>
/// <returns>The encoding associated with the specified code page.</returns>
/// <param name="name">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.</param>
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);
}
/// <summary>
/// Gets a value indicating whether the current encoding can be used by browser clients for displaying content.
/// </summary>