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 文件读写; namespace Hydro.CommonBase { [Serializable] public class dict : Dictionary { public bool emptyCreate = false; List _list = null; public dict() : base() { } public dict(int capacity) : base(capacity) { } public dict(IEqualityComparer comparer) : base(comparer) { } public dict(IDictionary dictionary) : base(dictionary) { } public dict(int capacity, IEqualityComparer comparer) : base(capacity, comparer) { } public dict(IDictionary dictionary, IEqualityComparer comparer) : base(dictionary, comparer) { } protected dict(SerializationInfo info, StreamingContext context) : base(info, context) { Value = info.GetBoolean("Test_Value"); } public override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); info.AddValue("Test_Value", Value); } //public Dictionary Dictionary() //{ // Dictionary dc = new Dictionary(this); // return dc; //} public bool Value { get; set; } public bool SetKey(string key, dynamic Value) { if (this.Contains(key)) { this[key] = Value; return false; } else { this.Add(key, Value); return true; } } public string[] 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(string[] keys) { foreach (var key in keys) if (!this.Contains(key)) return false; return true; } public bool Contains(string key) { return this.ContainsKey(key); } public bool Contains(KeyValuePair key) { return this.ContainsKey(key.Key); } public dynamic Get(string key, dynamic @default = null) { if (this.Contains(key)) return this[key];//obj.FirstOrDefault((k) => k.Key == key); else return @default; } public dynamic Get(KeyValuePair key, dynamic @default = null) { if (this.Contains(key)) return this[key];//obj.FirstOrDefault((k) => k.Key == key); else return @default; } public dynamic pop(string key, dynamic @default = null) { if (this.Contains(key)) { var temp = this[key]; this.Remove(key); return temp;//obj.FirstOrDefault((k) => k.Key == key); } else return @default; } public dynamic pop(KeyValuePair key, dynamic @default = null) { if (this.Contains(key)) { var temp = this[key]; this.Remove(key); return temp;//obj.FirstOrDefault((k) => k.Key == key); } else return @default; } public dynamic this[KeyValuePair key] { get { return this[key.Key]; } set { this[key.Key] = value; } } public dynamic this[string key] { get { if (this.ContainsKey(key)) return base[key]; else { //this.Add(key,); return new dict { emptyCreate = true }; } } set { if (this.ContainsKey(key)) base[key] = value; else this.Add(key, value); } } public dynamic this[Type key_Type] { get { return base[key_Type.Name]; } set { if (this.ContainsKey(key_Type.Name)) base[key_Type.Name] = value; else { this.Add(key_Type.Name, value); if (this.dic_Type == null) dic_Type = new dict(); if (!this.dic_Type.ContainsKey(key_Type.Name)) this.dic_Type.Add(key_Type.Name, key_Type); } } } public dict dic_Type; public dynamic this[int index] { get { if (this._list == null) _list = new List(); if (_list.Count <= index) { int count = _list.Count; for (int i = count; i <= index; i++) _list.Add(new dict { emptyCreate = true }); } return this._list[index]; } set { if (this._list == null) _list = new List(); if (_list.Count <= index) { int count = _list.Count; for (int i = count; i <= index; i++) _list.Add(new dict { emptyCreate = true }); } this._list[index] = value; } } public override string ToString() { return JsonConvert.SerializeObject(this); } public void LoadFromString(string s) { dict 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 keys() { return this.Keys.ToList(); } 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 Task send(dynamic returnMsg) //{ // Trace.WriteLine("收集信息send"); // 发送计时 = DateTime.Now; // if (msgList != "") msgList += "\n"; // msgList += returnMsg; // //return Task.Run(() => { bot.nonebot.Send(returnMsg, this.Get("group_id").ToString()); }); // return Task.Run(() => { }); //} //public Task finish(string returnMsg) //{ // if (bot.msgHandler.AutoSetCurrentGroup && !string.IsNullOrEmpty(this.Get("group_id").ToString())) // bot.nonebot.SetCurrentActiveGroup(this.Get("group_id").ToString()); // return Task.Run(() => { bot.nonebot.Send(returnMsg, this.Get("group_id").ToString()); }); //} //public Task finish() //{ // Trace.WriteLine("发送信息"); // //return Task.Run(() => { }); // if (bot.msgHandler.AutoSetCurrentGroup && !string.IsNullOrEmpty(this.Get("group_id").ToString())) // bot.nonebot.SetCurrentActiveGroup(this.Get("group_id").ToString()); // return Task.Run(() => { bot.nonebot.Send(msgList, this.Get("group_id").ToString()); msgList = ""; }); //} public string msg { get { return this.Get("message"); } } public string msg_text { get { return this.Get("message"); } } public string current_arg_text { get { return this.Get("current_arg_text"); } } 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 string Item1; public dynamic Item2; public TempForEnum(string key, dynamic value) { Item1 = key; Item2 = value; } } 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; } } }