From ed6a0cbb06cec276a52423632b875f499f8866fb Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Sun, 22 Dec 2013 23:05:38 +0100 Subject: [PATCH] update encoder --- ReadSharp/Encodings/Encoder.cs | 38 +++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/ReadSharp/Encodings/Encoder.cs b/ReadSharp/Encodings/Encoder.cs index 23a9233..ff0d89b 100644 --- a/ReadSharp/Encodings/Encoder.cs +++ b/ReadSharp/Encodings/Encoder.cs @@ -37,22 +37,40 @@ namespace ReadSharp.Encodings if (!String.IsNullOrEmpty(encoding)) { + // try get encoding from string try { correctEncoding = Encoding.GetEncoding(encoding); - throw new Exception(); } - catch + catch (ArgumentException) { - KeyValuePair customEncoder = customEncodings.FirstOrDefault(item => item.Key == encoding.ToLower()); + // encoding not found in environment + // handled in finally block as it could also generate null without throwing exceptions + } + finally + { + // use a custom encoder + if (correctEncoding == null) + { + try + { + KeyValuePair customEncoder = customEncodings.FirstOrDefault(item => item.Key == encoding.ToLower()); - if (!String.IsNullOrEmpty(customEncoder.Value)) - { - correctEncoding = (Encoding)System.Activator.CreateInstance(Type.GetType("ReadSharp.Encodings." + customEncoder.Value)); - } - else - { - correctEncoding = null; + if (!String.IsNullOrEmpty(customEncoder.Value)) + { + Type encoderType = Type.GetType("ReadSharp.Encodings." + customEncoder.Value); + + if (encoderType != null) + { + correctEncoding = (Encoding)System.Activator.CreateInstance(Type.GetType("ReadSharp.Encodings." + customEncoder.Value)); + } + } + } + catch (Exception) + { + // couldn't create instance for whatever reason. + // nothing to do here, as the default encoder will be used in the program. + } } } }