Ejemplo n.º 1
0
 /// <summary>
 /// 获取交集(不返回null)
 /// </summary>
 public static List <T> GetInterSection <T>(IEnumerable <T> a, IEnumerable <T> b)
 {
     if (!ValidateHelper.IsPlumpList(a) || !ValidateHelper.IsPlumpList(b))
     {
         return(new List <T>());
     }
     return(a.Where(x => b.Contains(x)).ToList());
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 满足部门权限
 /// </summary>
 public static bool HasAnyPermission(int user_permission, params int[] permission_to_valid)
 {
     if (!ValidateHelper.IsPlumpList(permission_to_valid))
     {
         throw new ArgumentException(nameof(permission_to_valid));
     }
     return(permission_to_valid.Any(x => HasPermission(user_permission, x)));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 多个集合里取交集(不返回null)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lists"></param>
        /// <returns></returns>
        public static List <T> GetInterSectionFromAny <T>(params List <T>[] lists)
        {
            if (!ValidateHelper.IsPlumpList(lists) || lists.Count() < 2)
            {
                throw new Exception("至少两个集合才能获取交集");
            }

            return(lists.Reduce((x, y) => Com.GetInterSection(x, y)));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取交集(不返回null)
        /// </summary>
        public static List <T> GetInterSection <T>(IEnumerable <T> a, IEnumerable <T> b, IEqualityComparer <T> comparer = null)
        {
            if (!ValidateHelper.IsPlumpList(a) || !ValidateHelper.IsPlumpList(b))
            {
                return(new List <T>());
            }

            return(a.Where(x => b.ContainsItem_(x, comparer)).ToList());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 通过空格 中英文逗号分隔(结果按照orderby排序)
        /// </summary>
        /// <param name="tags"></param>
        /// <returns></returns>
        public static List <string> SplitTags(string tags)
        {
            var list = StringHelper.Split(tags, ' ', ',', ',');

            if (!ValidateHelper.IsPlumpList(list))
            {
                return(list);
            }
            return(list.Where(x => ValidateHelper.IsLenInRange(x, 1, 30)).OrderBy(x => x).ToList());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 弹出索引最大的一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static T Pop <T>(ref List <T> list)
        {
            if (!ValidateHelper.IsPlumpList(list))
            {
                return(default(T));
            }
            var obj = list[list.Count - 1];

            list.Remove(obj);
            return(obj);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 根据attribute验证model
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List <string> CheckEntity_ <T>(T model) where T : IDBTable
        {
            var list = new List <string>();

            if (model == null)
            {
                list.Add("实体对象不能为Null");
                return(list);
            }

            //checker
            bool CheckProp(IEnumerable <ValidationAttribute> validators, object data, PropertyInfo p)
            {
                foreach (var validator in ConvertHelper.NotNullEnumerable(validators))
                {
                    if (!validator.IsValid(data))
                    {
                        var msg = ConvertHelper.GetString(validator.ErrorMessage).Trim();
                        if (!ValidateHelper.IsPlumpString(msg))
                        {
                            msg = $"字段{p.Name}未通过{validator.GetType().Name}标签的验证";
                        }
                        list.Add(msg);
                        return(false);
                    }
                }
                return(true);
            };

            foreach (var prop in model.GetType().GetProperties())
            {
                if (prop.GetCustomAttributes <NotMappedAttribute>().Any())
                {
                    continue;
                }

                var value = prop.GetValue(model);

                if (!CheckProp(prop.GetCustomAttributes_ <ValidationAttribute>(), value, prop))
                {
                    continue;
                }
            }

            list = list.Where(x => IsPlumpString(x)).Distinct().ToList();

            if (ValidateHelper.IsPlumpList(list))
            {
                Console.WriteLine("".Join_(list));
            }

            return(list);
        }
Ejemplo n.º 8
0
 public static Bitmap BytesToBitmap(byte[] buffer)
 {
     if (!ValidateHelper.IsPlumpList(buffer))
     {
         throw new Exception("bytes is null");
     }
     using (var ms = new MemoryStream())
     {
         ms.Write(buffer, 0, buffer.Length);
         var bmp = new Bitmap(ms);
         return(bmp);
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// join 不会返回空
        /// </summary>
        public static string Join(string spliter, IList <string> list)
        {
            string str = string.Empty;

            if (!ValidateHelper.IsPlumpList(list))
            {
                return(str);
            }
            list.ToList().ForEach(delegate(string s)
            {
                s    = ConvertHelper.GetString(s);
                str += ((str == string.Empty) ? string.Empty : spliter) + s;
            });
            return(str);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 使用递归找文件目录
        /// </summary>
        public static void FindFilesBad(DirectoryInfo dir, VoidFunc <FileInfo> func)
        {
            if (dir == null || !dir.Exists)
            {
                return;
            }
            var files = dir.GetFiles();

            if (ValidateHelper.IsPlumpList(files))
            {
                files.ToList().ForEach(x => { func.Invoke(x); });
            }

            var dirs = dir.GetDirectories();

            if (ValidateHelper.IsPlumpList(dirs))
            {
                dirs.ToList().ForEach(x => { FindFilesBad(x, func); });
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 多个集合里取交集(不返回null)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lists"></param>
        /// <returns></returns>
        public static List <T> GetInterSectionFromAny <T>(params List <T>[] lists)
        {
            if (!ValidateHelper.IsPlumpList(lists) || lists.Length < 2)
            {
                return(new List <T>());
            }
            var interSection = GetInterSection(lists[0], lists[1]);

            //原来这里i从3开始
            for (int i = 2; i < lists.Length; ++i)
            {
                //发现有非交集的就中断
                if (!ValidateHelper.IsPlumpList(interSection = GetInterSection(interSection, lists[i])))
                {
                    //原来这里如果没有找到焦急就break,最终返回一个空的list(interSection),现在直接返回
                    return(new List <T>());
                }
            }
            return(interSection);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// class property method都继承值memberinfo
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="member"></param>
        /// <returns></returns>
        public static List <T> GetAttributes <T>(MemberInfo member)
        {
            if (member == null)
            {
                return(null);
            }
            var attrs = member.GetCustomAttributes(typeof(T), false);

            if (!ValidateHelper.IsPlumpList(attrs))
            {
                return(null);
            }
            var list = new List <T>();

            foreach (T obj in attrs)
            {
                list.Add(obj);
            }
            return(list);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 使用栈找文件
        /// </summary>
        public static void FindFiles(string path, VoidFunc <FileInfo> func, VoidFunc <int> stack_count_func = null)
        {
            var root = new DirectoryInfo(path);

            if (!root.Exists)
            {
                throw new Exception("目录不存在");
            }

            var stack = new Stack <DirectoryInfo>();

            stack.Push(root);
            DirectoryInfo cur_node = null;

            while (stack.Count > 0)
            {
                stack_count_func?.Invoke(stack.Count);

                cur_node = stack.Pop();
                if (cur_node == null || !cur_node.Exists)
                {
                    break;
                }

                var files = cur_node.GetFiles();
                if (ValidateHelper.IsPlumpList(files))
                {
                    files.ToList().ForEach(x => { func.Invoke(x); });
                }

                var dirs = cur_node.GetDirectories();
                if (ValidateHelper.IsPlumpList(dirs))
                {
                    dirs.ToList().ForEach(x => { stack.Push(x); });
                }
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 获取map(测试可用)
        /// </summary>
        public static void MapEntity <T>(ref T entity, object model, string[] notmap = null)
        {
            if (model == null)
            {
                throw new Exception("对象为空");
            }

            //读取
            var modelproperties = ConvertHelper.NotNullList(model.GetType().GetProperties());

            modelproperties = modelproperties.Where(x => x.CanRead).ToList();

            //写入
            var entityproperties = ConvertHelper.NotNullList(entity.GetType().GetProperties());

            entityproperties = entityproperties.Where(x => x.CanWrite).ToList();
            if (ValidateHelper.IsPlumpList(notmap))
            {
                entityproperties = entityproperties.Where(x => !notmap.Contains(x.Name)).ToList();
            }

            foreach (var pi in entityproperties)
            {
                //属性名和属性类型一样
                var modelpi = modelproperties
                              .Where(x => x.Name == pi.Name)
                              .Where(x => x.GetType() == pi.GetType())
                              .FirstOrDefault();

                if (modelpi == null)
                {
                    continue;
                }

                pi.SetValue(entity, modelpi.GetValue(model), null);
            }
        }
Ejemplo n.º 15
0
        public static List <string> CheckEntity <T>(T model) where T : IDBTable
        {
            var list = new List <string>();

            if (model == null)
            {
                list.Add("实体对象不能为Null");
                return(list);
            }

            //checker
            Func <ValidationAttribute, object, bool> CheckProp = (validator, data) =>
            {
                if (validator != null && !validator.IsValid(data))
                {
                    list.Add(validator.ErrorMessage);
                    return(false);
                }
                return(true);
            };

            foreach (var prop in model.GetType().GetProperties())
            {
                if (prop.GetCustomAttributes <NotMappedAttribute>().Any())
                {
                    continue;
                }
                //自定义
                if (prop.GetCustomAttributes <CustomValidationAttribute>().FirstOrDefault() != null)
                {
                    throw new NotSupportedException("不支持CustomValidationAttribute");
                }

                var value = prop.GetValue(model);

                //是否可为空
                if (!CheckProp(prop.GetCustomAttributes <RequiredAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //字符串长度
                if (!CheckProp(prop.GetCustomAttributes <StringLengthAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //正则表达式
                if (!CheckProp(prop.GetCustomAttributes <RegularExpressionAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //范围
                if (!CheckProp(prop.GetCustomAttributes <RangeAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //最大长度
                if (!CheckProp(prop.GetCustomAttributes <MaxLengthAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //最小长度
                if (!CheckProp(prop.GetCustomAttributes <MinLengthAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //电话
                if (!CheckProp(prop.GetCustomAttributes <PhoneAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //邮件
                if (!CheckProp(prop.GetCustomAttributes <EmailAddressAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //URL
                if (!CheckProp(prop.GetCustomAttributes <UrlAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
                //信用卡
                if (!CheckProp(prop.GetCustomAttributes <CreditCardAttribute>().FirstOrDefault(), value))
                {
                    continue;
                }
            }

            list = list.Where(x => IsPlumpString(x)).Distinct().ToList();

            if (ValidateHelper.IsPlumpList(list))
            {
                Console.WriteLine("".Join_(list));
            }

            return(list);
        }