| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 | 
							- using System;
 
- using System.Collections;
 
- using System.Collections.Generic;
 
- using System.Collections.ObjectModel;
 
- using System.Collections.Specialized;
 
- using System.ComponentModel;
 
- using System.Linq;
 
- using System.Linq.Expressions;
 
- using System.Reflection;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- /// <summary>
 
- /// <para>一个更高性能的用于数据绑定的对象,继承自BindingList对象</para>
 
- /// <para>添加了AddRange、InsertRange,支持批量添加后再整体刷新视图,提高性能</para>
 
- /// <para>添加了几个常用的类似Linq中List的函数</para>
 
- /// <para>该对象可直接用于json序列化和反序列化,其结果和List一样</para>
 
- /// </summary>
 
- /// <typeparam name="T">泛型对象</typeparam>
 
- /// <remarks>BindingData不具有线程安全特性,该对象应该只初始化一次,通过Clear、Add等更新</remarks>
 
- public class BindingData<T> : BindingList<T> where T : class, new()
 
- {
 
-     public event Action<T> OnItemDelete;
 
-     public BindingData()
 
-         : base()
 
-     {
 
-     }
 
-     /// <summary>
 
-     /// 是否支持排序
 
-     /// </summary>
 
-     protected override bool SupportsSortingCore
 
-     {
 
-         get { return true; }
 
-     }
 
-     /// <summary>
 
-     /// 是否支持搜索
 
-     /// </summary>
 
-     protected override bool SupportsSearchingCore
 
-     {
 
-         get { return true; }
 
-     }
 
-     protected override void RemoveItem(int itemIndex)
 
-     {
 
-         OnItemDelete?.Invoke(this[itemIndex]);
 
-         base.RemoveItem(itemIndex);
 
-     }
 
-     public BindingData(IEnumerable<T> data)
 
-     {
 
-         //构造函数中不会触发OnListChanged
 
-         this.AddRange(data);
 
-     }
 
-     public void AddRange(IEnumerable<T> items)
 
-     {
 
-         if (items == null || items.Count() == 0) return;
 
-         RaiseListChangedEvents = false;//让View不刷新
 
-         foreach (var item in items)
 
-         {
 
-             Add(item);
 
-         }
 
-         OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));//批量更新View
 
-         RaiseListChangedEvents = true;//让View继续更新
 
-     }
 
-     public void InsertRange(IEnumerable<T> items)
 
-     {
 
-         if (items == null || items.Count() == 0) return;
 
-         RaiseListChangedEvents = false;//让View不刷新
 
-         int count = items.Count();
 
-         for (int i = count - 1; i >= 0; i--)
 
-         {
 
-             InsertItem(0, items.ElementAt(i));
 
-         }
 
-         OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));//批量更新View
 
-         RaiseListChangedEvents = true;//让View继续更新
 
-     }
 
-     public T[] GetRows(int[] rows)
 
-     {
 
-         if (this.Count < 0) return null;
 
-         List<T> list = new List<T>();
 
-         foreach (var index in rows)
 
-         {
 
-             if (index < 0 || index >= this.Count) continue;
 
-             list.Add(this[index]);
 
-         }
 
-         return list.ToArray();
 
-     }
 
-     public int RemoveAll(Predicate<T> match)
 
-     {
 
-         var list = this.FindAll(match);
 
-         RaiseListChangedEvents = false;//让View不刷新
 
-         int i = 0;
 
-         foreach (var item in list)
 
-         {
 
-             if (this.Remove(item)) i++;
 
-         }
 
-         OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));//批量更新View
 
-         RaiseListChangedEvents = true;//让View继续更新
 
-         return i;
 
-     }
 
-     public void RemoveAll(T[] data)
 
-     {
 
-         foreach (var item in data)
 
-         {
 
-             this.Remove(item);
 
-         }
 
-     }
 
-     public T Find(Predicate<T> match)
 
-     {
 
-         foreach (var item in this)
 
-         {
 
-             if (match(item))
 
-                 return item;
 
-         }
 
-         return default;
 
-     }
 
-     public List<T> FindAll(Predicate<T> match)
 
-     {
 
-         List<T> list = new List<T>();
 
-         foreach (var item in this)
 
-         {
 
-             if (match(item))
 
-                 list.Add(item);
 
-         }
 
-         return list;
 
-     }
 
-     public int FindIndex(Predicate<T> match)
 
-     {
 
-         int idx = 0;
 
-         foreach (var item in this)
 
-         {
 
-             if (match(item))
 
-                 return idx;
 
-             idx++;
 
-         }
 
-         return -1;
 
-     }
 
-     public bool Exists(Predicate<T> match)
 
-     {
 
-         foreach (var item in this)
 
-         {
 
-             if (match(item))
 
-                 return true;
 
-         }
 
-         return false;
 
-     }
 
-     public void SortData(Expression<Func<T, dynamic>> express, bool asc = true)
 
-     {
 
-         RaiseListChangedEvents = false;//让View不刷新
 
-         List<T> tmp;
 
-         if (asc)
 
-             tmp = this.OrderBy(express.Compile()).ToList();
 
-         else
 
-             tmp = this.OrderByDescending(express.Compile()).ToList();
 
-         this.Clear();
 
-         this.AddRange(tmp);
 
-         RaiseListChangedEvents = true;//让View继续更新
 
-     }
 
-     public List<T> DeepClone()
 
-     {
 
-         ParameterExpression parameterExpression = Expression.Parameter(typeof(T), "p");
 
-         List<MemberBinding> memberBindingList = new List<MemberBinding>();
 
-         foreach (var item in typeof(T).GetProperties())
 
-         {
 
-             if (!item.CanWrite)
 
-                 continue;
 
-             MemberExpression property = Expression.Property(parameterExpression, typeof(T).GetProperty(item.Name));
 
-             MemberBinding memberBinding = Expression.Bind(item, property);
 
-             memberBindingList.Add(memberBinding);
 
-         }
 
-         MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(T)), memberBindingList.ToArray());
 
-         Expression<Func<T, T>> lambda = Expression.Lambda<Func<T, T>>(memberInitExpression, new ParameterExpression[] { parameterExpression });
 
-         List<T> list = new List<T>();
 
-         foreach (var item in this)
 
-         {
 
-             list.Add(lambda.Compile()(item));
 
-         }
 
-         return list;
 
-     }
 
- }
 
 
  |