/// <summary> /// 打乱序列列表的元素顺序 /// </summary> /// <typeparam name="T">数组类型</typeparam> /// <param name="slist">数据源序列</param> /// <param name="need_num">需要的结果数量 如小于等于零取值序列个数</param> /// <returns>结果序列</returns> public static T[] Sample <T>(IList <T> slist, int need_num = 0) { if (CheckData.IsSizeEmpty(slist)) { return(new T[] { }); } List <T> rlist = new List <T>(); int slen_size = slist.Count; if (need_num <= 0) { need_num = slen_size; } if (need_num > slen_size) { rlist.AddRange(Sample(slist, need_num - slen_size)); need_num = slen_size; } while (need_num > 0) { int index = GetInt(0, need_num); need_num -= 1; rlist.Add(slist[index]); T C = slist[index]; slist[index] = slist[need_num]; slist[need_num] = C; } return(rlist.ToArray()); }
/// <summary> /// 随机选取其中一个选项 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="source">数据源</param> /// <returns>结果选项, 数据源为空返回:数据类型默认值</returns> public static T Item <T>(IList <T> source) { if (CheckData.IsSizeEmpty(source)) { return(default(T)); } return(source[R.Next(0, source.Count)]); }
/// <summary> /// 检查模型可以使用 多例 /// </summary> /// <typeparam name="M">数据模型类型</typeparam> /// <param name="array">数据来源</param> /// <param name="errorMethod">不能使用的判断条件</param> /// <returns>True: 可以使用, 反之亦然</returns> public static bool IsCanUse <M>(M[] array, Func <M, bool> errorMethod) where M : AbsBasicDataModel { if (!CheckData.IsSizeEmpty(array)) { foreach (M item in array) { if (IsCanUse(item, errorMethod)) { return(true); } } } return(false); }
/// <summary> /// 拼接随机字符串 /// </summary> /// <param name="source">指定字符进行拼接</param> /// <param name="max_charlength">指定字符个数</param> /// <returns>拼接结果</returns> public static string GetString(char[] source, int sum_length) { if (CheckData.IsSizeEmpty(source)) { return(string.Empty); } StringBuilder strbu = new StringBuilder(); for (int i = 0; i < sum_length; i++) { strbu.Append(Item(source)); } return(strbu.ToString()); }
/// <summary> /// 过滤禁用的字符 /// </summary> /// <param name="source">需要处理的字符串</param> /// <param name="disable_chars">禁用字符列表</param> /// <returns>结果</returns> public static string FilterDisableChars(string source, char[] disable_chars) { if (CheckData.IsStringNull(source)) { return(string.Empty); } if (CheckData.IsSizeEmpty(disable_chars)) { return(source); } foreach (char c in disable_chars) { source = source.Replace(c.ToString(), ""); } return(source); }
/// <summary> /// 所有子文件夹递归循环获取 /// </summary> /// <param name="olddir"></param> /// <returns></returns> public static DirectoryInfo[] AllSonDirectorys(DirectoryInfo olddir) { if (CheckData.IsObjectNull(olddir)) { return(new DirectoryInfo[] { }); } DirectoryInfo[] selfson = olddir.GetDirectories(); if (CheckData.IsSizeEmpty(selfson)) { return(new DirectoryInfo[] { }); } List <DirectoryInfo> sons = new List <DirectoryInfo>(selfson); foreach (DirectoryInfo item in selfson) { sons.AddRange(AllSonDirectorys(item)); } return(sons.ToArray()); }
/// <summary> /// 获取指定范围选项, 长度计算方式: 加法计算: index + length /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="source">数据源</param> /// <param name="index">页面索引</param> /// <param name="length">列表长度</param> /// <returns>选项结果</returns> public static T[] ToRangeIndex <T>(IList <T> source, int index, int length) { if (CheckData.IsSizeEmpty(source) || index >= source.Count) { return(new T[] { }); } if (index < 0) { index = 0; } if (length < 0) { length = 0; } int sumlen = index + length; if (sumlen >= source.Count) { length -= sumlen - source.Count; } List <T> list = new List <T>(source); return(list.GetRange(index, length).ToArray()); }
/// <summary> /// 查找指定的 Attribute '特性' 内容对象 /// </summary> /// <typeparam name="A">指定的 Attribute '特性'</typeparam> /// <param name="memberInfo">元数据</param> /// <param name="isFindInherit">是否查找继承链, 默认不查找</param> /// <returns>返回指定'特性'类型的第一个结果</returns> public static A AttributeFindOnly <A>(MemberInfo memberInfo, bool isFindInherit = false) where A : System.Attribute { A[] attrs = AttributeFindALL <A>(memberInfo, isFindInherit); return(CheckData.IsSizeEmpty(attrs) ? null : attrs[0]); }