using System; using System.Collections.Generic; using System.Text; namespace Thousandto.Core.Base { /// /// 容器的工具类 /// public class ContainerUtils { /// /// 封装字典的Foreach处理 /// /// /// /// /// public static void DoForeachDictionary(Dictionary data, MyAction onCallback) { if (data == null || data.Count == 0) return; var e = data.GetEnumerator(); try { while (e.MoveNext()) { onCallback(e.Current.Key, e.Current.Value); } } finally { e.Dispose(); } } /// /// 封装Dict添加数据泛型方法 返回false为修改原有数据 /// /// /// /// /// /// /// public static bool DictionaryAdd(Dictionary tagDict, K key, V value) { if (tagDict != null) { V data; if (!tagDict.TryGetValue(key, out data)) { tagDict.Add(key, value); return true; } else { tagDict[key] = value; } } return false; } /// /// 封装List置换数据方法 /// /// /// /// /// /// public static void ListReplace(List tagList, T oldData, T newData, bool doAdd = false) { if (tagList != null) { int index = tagList.IndexOf(oldData); if (index != -1) { tagList[index] = newData; } else { if (doAdd) { tagList.Add(newData); } } } } public static void ListReplace(List tagList, T newData, bool doAdd = false) { if (tagList != null) { int index = tagList.IndexOf(newData); if (index != -1) { tagList[index] = newData; } else { if (doAdd) { tagList.Add(newData); } } } } /// /// 封装List置换数据唯一方法 /// /// /// /// /// /// public static void ListReplaceUnique(List tagList, T oldData, T newData, bool doAdd = false) { if (tagList != null) { int index = tagList.IndexOf(oldData); if (index != -1) { tagList.RemoveAt(index); } ListAddUnique(tagList, newData); } } /// /// 封装List唯一数据方法 /// /// /// /// public static void ListAddUnique(List tagList, T newData) { if (tagList != null) { if (!tagList.Contains(newData)) { tagList.Add(newData); } } } /// /// 封装添加目标容器固定位方法 /// /// /// /// /// /// public static void ListAddToTagIndex(List tagList, int index, T newData, T defaultData) { if (tagList != null) { if (index > tagList.Count - 1) { for (int i = 0, imax = (index - (tagList.Count)); i < imax; i++) { tagList.Add(defaultData); } tagList.Add(newData); } else { tagList[index] = newData; } } } /// /// 封装array foreach方法 /// /// /// /// public static void DoForeach(T[] tagArray, MyAction foreachFunc = null) { if (tagArray != null) { for (int i = 0, imax = tagArray.Length; i < imax; i++) { T curElement = tagArray[i]; if (foreachFunc != null && curElement != null) { foreachFunc(curElement); } } } } /// /// 封装array foreach方法 /// /// /// /// public static void DoForeachIteratively(T[] tagArray, MyAction foreachFunc = null) { if (tagArray != null) { for (int i = 0, imax = tagArray.Length; i < imax; i++) { T curElement = tagArray[i]; if (foreachFunc != null && curElement != null) { foreachFunc(curElement, i); } } } } /// /// 获取字典的值,通过索引 /// /// /// /// /// /// public static KeyValuePair GetDictItem(Dictionary dict, int idx) { var e = dict.GetEnumerator(); try { while (e.MoveNext()) { if (idx == 0) { return e.Current; } else if (idx < 0) { break; }else { idx--; } } } finally { e.Dispose(); } return default(KeyValuePair); } /// /// 查找字典通过值 /// /// /// /// /// /// public static KeyValuePair FindDictItemByValue(Dictionary dict, T2 value) { var e = dict.GetEnumerator(); try { while (e.MoveNext()) { if (e.Current.Value.Equals(value)) { return e.Current; } } } finally { e.Dispose(); } return default(KeyValuePair); } public static T GetItemByIndex(LinkedList list, int idx) { var e = list.GetEnumerator(); try { while (e.MoveNext()) { if (idx == 0) { return e.Current; } else if (idx < 0) { break; } else { idx--; } } } finally { e.Dispose(); } return default(T); } } }