using System; using System.Collections.Generic; using System.Linq; using System.Reflection; // ReSharper disable InconsistentNaming // ReSharper disable IdentifierTypo // ReSharper disable StringLiteralTypo namespace UFIDA.U8.Framework.Api.ControlObject.Common { public class BEHelper { private readonly dynamic _be; private readonly PropertyInfo[] _props; public BEHelper(dynamic be) { _be = be; var tp = be.GetType(); _props = tp.GetProperties(); } public void SetValue(Dictionary dictionary) { foreach (var key in dictionary.Keys) { if (key.Contains("define") && !key.Contains("chdefine")) { var defineCode = int.Parse(key.Replace("define", "")); if (dictionary.TryGetValue(key, out var bValue) && !string.IsNullOrWhiteSpace($"{bValue}")) { _be.UserDefinedItem[defineCode] = dictionary[key]; } } else if (key.Contains("chdefine")) { var defineCode = int.Parse(key.Replace("chdefine", "")); if (dictionary.TryGetValue(key, out var bValue) && !string.IsNullOrWhiteSpace($"{bValue}")) { _be.ExtendUserDefineItem[defineCode] = dictionary[key]; } } else if (key != "body") { if (!string.IsNullOrWhiteSpace($"{dictionary[key]}") && _props.Any(a => string.Equals(a.Name, key, StringComparison.OrdinalIgnoreCase))) { var prop = _props.First(a => string.Equals(a.Name, key, StringComparison.OrdinalIgnoreCase)); if (prop.PropertyType == typeof(string)) { prop.SetValue(_be, dictionary[key]); } if (prop.PropertyType == typeof(DateTime)) { prop.SetValue(_be, DateTime.Parse($"{dictionary[key]}")); } if (prop.PropertyType == typeof(byte)) { prop.SetValue(_be, byte.Parse($"{dictionary[key]}")); } if (prop.PropertyType == typeof(int)) { prop.SetValue(_be, int.Parse($"{dictionary[key]}")); } if (prop.PropertyType == typeof(long)) { prop.SetValue(_be, long.Parse($"{dictionary[key]}")); } if (prop.PropertyType == typeof(decimal)) { prop.SetValue(_be, decimal.Parse($"{dictionary[key]}")); } } } } } } }