Beispiel #1
0
 public void Initialise(IDataTransferObject dto)
 {
     if (dto.ContainsKey($"{Name}ID") || dto.ContainsKey("ID"))
     {
         var key = dto.ContainsKey($"{Name}ID") ?
                   (int)dto[$"{Name}ID"] : (int)dto["ID"];
         var value = dto.ContainsKey($"{Name}Name") ?
                     dto[$"{Name}Name"].ToString() : dto["Name"].ToString();
         Add(key, value);
     }
 }
Beispiel #2
0
        private void SetProperty(IDataTransferObject dto, PropertyInfo p)
        {
            var key = p.Name;

            if (!dto.ContainsKey(key))
            {
                return;
            }
            if (dto[key] is null)
            {
                return;
            }
            var propType  = p.PropertyType;
            var fieldType = dto[key].GetType();

            dynamic value = null;

            if (propType == fieldType)
            {
                value = dto[key];
            }
            else if (propType == typeof(decimal) && fieldType.IsNumeric())
            {
                value = decimal.Parse(dto[key].ToString());
            }
            else if (propType == typeof(string))
            {
                value = dto[key].ToString().Replace(" 00:00:00", string.Empty);
            }
            try
            {
                if (p.CanWrite)
                {
                    p.SetValue(this, value);
                }
                else
                {
                    TrySetReadOnlyPropertyValue(p, value);
                }
            }
            catch (InvalidCastException ex)
            {
                throw new Exception(
                          "Data type mismatch error on model key: " +
                          $"{key} for property: {p.Name}", ex);
            }
        }