Exemplo n.º 1
0
        public static void SetPropertyValue(this PropertyInfo p, IPassModel input, object result, bool deleteExistFile = true, List <string> oldFiles = null, List <string> newFiles = null)
        {
            var propertyPost = input.Properties.Find(b => b.Key == p.Name);

            if (propertyPost == null)
            {
                return;
            }
            dynamic value          = null;
            var     stringValue    = !String.IsNullOrEmpty(propertyPost.Value) ? propertyPost.Value : "";
            var     keyType        = p.PropertyType;
            var     inputAttribute = p.GetCustomAttribute <InputTypeAttribute>();

            if (inputAttribute != null)
            {
                switch (inputAttribute.EditorType)
                {
                case EnumInputType.DropDwon:
                    value = GetDropDownValue(inputAttribute, p, propertyPost);
                    break;

                case EnumInputType.FileUpload:
                    var files = propertyPost;
                    if (files.File != null)
                    {
                        files.File.Save(out var filePath, $"{p.DeclaringType.FullName}_{p.Name}");
                        if (deleteExistFile)
                        {
                            files.Value.DeleteFile(out var deleted);
                        }
                        value = filePath;
                    }
                    else
                    {
                        value = files.Value;
                    }
                    break;

                default:
                    value = stringValue.MyTryConvert(keyType);
                    break;
                }
            }
            else
            {
                //normal type switch and set
                value = stringValue.MyTryConvert(keyType);
            }
            if (value != null)
            {
                p.SetValue(result, value);
            }
        }
Exemplo n.º 2
0
        public static void SetIPassModel(this IPassModel model, object obj)
        {
            if (model == null || obj == null)
            {
                return;
            }
            var type = obj.GetType();

            foreach (var p in type.GetProperties())
            {
                p.SetPropertyValue(model, obj);
            }
        }
Exemplo n.º 3
0
        public static IPassModel ConvertObjectToIPassModel(this object input, IPassModel model = null)
        {
            if (input == null)
            {
                return(null);
            }
            if (model == null)
            {
                model = new BasePassModel();
            }
            var type = input.GetType();

            foreach (var p in type.GetProperties())
            {
                var value = p.GetContentPropertyByPropertyInfo(input);
                if (value != null)
                {
                    model.Properties.Add(value);
                }
            }
            return(model);
        }
Exemplo n.º 4
0
        public static void SetPropertyValue(this PropertyInfo p, IPassModel input, object result, bool deleteExistFile = true, List <string> oldFiles = null, List <string> newFiles = null)
        {
            var propertyPost = input.Properties.Find(b => b.Key == p.Name);

            if (propertyPost == null)
            {
                return;
            }
            object value       = null;
            var    stringValue = !String.IsNullOrEmpty(propertyPost.Value) ? propertyPost.Value : "";

            propertyPost.MultiValue = propertyPost.MultiValue.Where(b => !string.IsNullOrEmpty(b));

            var keyType        = p.PropertyType;
            var inputAttribute = p.GetCustomAttribute <InputTypeAttribute>();

            if (inputAttribute != null)
            {
                switch (inputAttribute.EditorType)
                {
                case EnumInputType.DropDwon:
                    if (String.IsNullOrEmpty(stringValue) && propertyPost.MultiSelect == false && propertyPost.MultiValue.Count() > 0)
                    {
                        propertyPost.Value = propertyPost.MultiValue.FirstOrDefault();
                    }
                    value = GetDropDownValue(inputAttribute, p, propertyPost);
                    break;

                case EnumInputType.FileUpload:
                    var files = propertyPost;
                    if (files.File != null)
                    {
                        var saveSuccess = GetSaveFile(files.File, out var filePath, $"{p.DeclaringType.FullName}_{p.Name}");
                        if (saveSuccess)
                        {
                            if (deleteExistFile && !string.IsNullOrEmpty(files.Value))
                            {
                                GetSaveFile(files.Value, out var deleted);
                            }
                            value = filePath;
                        }
                        else
                        {
                            value = files.Value;
                        }
                    }
                    else
                    {
                        value = files.Value;
                    }
                    break;

                default:
                    value = stringValue.MyTryConvert(keyType);
                    break;
                }
            }
            else
            {
                //normal type switch and set
                value = stringValue.MyTryConvert(keyType);
            }
            try
            {
                //todo 需要想办法做nullcheck
                p.SetValue(result, value);
            }
            catch { }
        }