From 47aa8c2aaaa3029d6b36560e8b3b6818642b60d9 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Mon, 7 Jun 2021 01:35:02 +0200 Subject: [PATCH] Extracting custom field conversion for use in other places --- TelegramBotBase/SessionBase.cs | 30 ++--------------------- TelegramBotBase/Tools/Conversion.cs | 38 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 TelegramBotBase/Tools/Conversion.cs diff --git a/TelegramBotBase/SessionBase.cs b/TelegramBotBase/SessionBase.cs index 7096000..410de80 100644 --- a/TelegramBotBase/SessionBase.cs +++ b/TelegramBotBase/SessionBase.cs @@ -198,7 +198,7 @@ namespace TelegramBotBase catch (ArgumentException ex) { - CustomConversionChecks(form, p, f); + Tools.Conversion.CustomConversionChecks(form, p, f); } catch @@ -240,33 +240,7 @@ namespace TelegramBotBase } - private static void CustomConversionChecks(FormBase form, KeyValuePair p, System.Reflection.PropertyInfo f) - { - //Newtonsoft Int64/Int32 converter issue - if (f.PropertyType == typeof(Int32)) - { - int i = 0; - if(int.TryParse(p.Value.ToString(), out i)) - { - f.SetValue(form, i); - } - return; - } - - //Newtonsoft Double/Decimal converter issue - if(f.PropertyType == typeof(Decimal) | f.PropertyType == typeof(Nullable)) - { - decimal d = 0; - if(decimal.TryParse(p.Value.ToString(), out d)) - { - f.SetValue(form, d); - } - return; - } - - - } - + /// diff --git a/TelegramBotBase/Tools/Conversion.cs b/TelegramBotBase/Tools/Conversion.cs new file mode 100644 index 0000000..b9e40da --- /dev/null +++ b/TelegramBotBase/Tools/Conversion.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Text; +using TelegramBotBase.Form; + +namespace TelegramBotBase.Tools +{ + public static class Conversion + { + public static void CustomConversionChecks(FormBase form, KeyValuePair p, System.Reflection.PropertyInfo f) + { + //Newtonsoft Int64/Int32 converter issue + if (f.PropertyType == typeof(Int32)) + { + int i = 0; + if (int.TryParse(p.Value.ToString(), out i)) + { + f.SetValue(form, i); + } + return; + } + + //Newtonsoft Double/Decimal converter issue + if (f.PropertyType == typeof(Decimal) | f.PropertyType == typeof(Nullable)) + { + decimal d = 0; + if (decimal.TryParse(p.Value.ToString(), out d)) + { + f.SetValue(form, d); + } + return; + } + + + } + + } +}