using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Threading; using Newtonsoft.Json; using System.Diagnostics; using System.Data; using NPOI.SS.Formula.Functions; //using 文件读写; namespace Hydro.CommonBase { //public class IndexedDictionary : Dictionary //{ // private List keys = new List(); // public TValue this[int index] // { // get { return this[keys[index]]; } // set { this[keys[index]] = value; } // } // public new void Add(TKey key, TValue value) // { // base.Add(key, value); // keys.Add(key); // } // public new bool Remove(TKey key) // { // if (base.Remove(key)) // { // keys.Remove(key); // return true; // } // return false; // } //} [Serializable] public class dict : Dictionary { private List keys { get { return this.Keys.ToList(); } } public bool emptyCreate = true; //List _list = null; public dict() : base() { } public dict(int capacity) : base(capacity) { } public dict(IEnumerable> collection) : base((IDictionary)collection) { } public bool Value { get; set; } public bool SetKey(TKey key, dynamic Value) { if (this.Contains(key)) { this[key] = Value; return false; } else { this.Add(key, Value); return true; } } public TKey[] GetKeys() { return this.Keys.ToArray(); } public List> items() { var temp_list = new List>(); foreach (var Pair in this) { temp_list.Add(new TempForEnum(Pair.Key, Pair.Value)); } return temp_list; } public bool Contains(TKey[] keys) { foreach (var key in keys) if (!this.Contains(key)) return false; return true; } public bool Contains(TKey key) { return this.ContainsKey(key); } public bool Contains(KeyValuePair key) { return this.ContainsKey(key.Key); } public TValue Get(TKey key, TValue @default = default(TValue)) { if (this.Contains(key)) return this[key];//obj.FirstOrDefault((k) => k.Key == key); else return @default; } public TValue Get(KeyValuePair key, TValue @default = default(TValue)) { if (this.Contains(key)) return this[key.Key];//obj.FirstOrDefault((k) => k.Key == key); else return @default; } public TValue pop(TKey key, TValue @default = default(TValue)) { if (this.Contains(key)) { var temp = this[key]; this.Remove(key); return temp;//obj.FirstOrDefault((k) => k.Key == key); } else return @default; } public TValue pop(KeyValuePair key, TValue @default = default(TValue)) { if (this.Contains(key)) { var temp = this[key.Key]; this.Remove(key); return temp;//obj.FirstOrDefault((k) => k.Key == key); } else return @default; } public TValue this[KeyValuePair key] { get { return this[key.Key]; } set { this[key.Key] = value; } } public TValue this[TKey key] { get { if (!this.ContainsKey(key)) { lock (this) { if (!this.ContainsKey(key)) { if (emptyCreate) this.Add(key, default(TValue)); else return default(TValue); } } } return base[key]; } set { if (!this.ContainsKey(key)) { lock (this) { if (!this.ContainsKey(key)) { if (emptyCreate) this.Add(key, value); else return; } } } base[key] = value; } } //public TValue this[int index] //{ // get // { // //如果是int类型的key,直接返回base[index],帮我补全以下return后的内容 // TKey key = default(TKey); // dynamic k = index; // key = k; // if (typeof(TKey)==typeof(int) ) // return base[key]; // else // return this[keys[index]]; // } // set // { // this[keys[index]] = value; // } //} public TValue this[int index] { get { //如果是int类型的key,直接返回base[index],帮我补全以下return后的内容 TKey key = default(TKey); if (typeof(TKey) == typeof(int)) key = (TKey)(object)index; else key = keys[index]; if (!base.ContainsKey(key)) base.Add(key, default(TValue)); return base[key]; } set { TKey key = default(TKey); if (typeof(TKey) == typeof(int)) { key = (TKey)(object)index; base[key] = value; } else { if (index d= JsonConvert.DeserializeObject>(s); this.Clear(); foreach(var kp in d) { this.Add(kp.Key,kp.Value); } } public void ExportToExcel() { DataTable dt = new DataTable(); dt.Columns.Add("Key"); dt.Columns.Add("Value",typeof(double)); foreach (var kp in this) { dt.Rows.Add(kp.Key, kp.Value); } ExcelExport.DtToExcel(dt); } public bool Remove(KeyValuePair key) { return this.Remove(key.Key); } public List values() { return this.Values.ToList(); } #region 将Session的元素简单放进dict中,简单处理方便使用 private DateTime 发送计时; private string msgList = ""; private System.Timers.Timer t = new System.Timers.Timer(); public dict ctx { get { return this; } } #endregion #region IntentCommand public double confidence; public string methodName; public dict args; public string current_arg; //current_arg: str = '' public dict(double confidence, string methodName, string current_arg = "", dict args = null) { this.confidence = confidence; this.methodName = methodName; this.args = args; this.current_arg = current_arg; } #endregion } public class TempForEnum { public TKey Item1; public TValue Item2; public TempForEnum(TKey key, TValue value) { Item1 = key; Item2 = value; } } [Serializable] public class dict : dict { public dict() : base() { } public dict(int capacity) : base(capacity) { } public dict(IEnumerable> collection) : base((IDictionary)collection) { } } public static class DictionaryEx { public static variable Get(this Dictionary dict,string key) { if (dict != null && dict.ContainsKey(key)) return dict[key]; else return null; } public static void AddKey(this Dictionary dict, string key,variable v) { if (dict == null) return; if ( !dict.ContainsKey(key)) { dict.Add(key, v); } else dict[key] = v; } } }