/// <summary> /// 从配置文件中读取合约组信息 /// </summary> /// <returns></returns> public static ObservableCollection <UserCodeSet> GetUserCodeSetList() { if (_UserCodeSetList == null) { if (File.Exists(ConfigFilePath)) { try { //先从配置文件中读取合约组信息 _UserCodeSetList = (ObservableCollection <UserCodeSet>)(CommonUtil.RecoverObjectFromFile(typeof(ObservableCollection <UserCodeSet>), ConfigFilePath)); } catch (System.Exception ex) { Util.Log(ex.ToString()); } } if (_UserCodeSetList == null) { _UserCodeSetList = new ObservableCollection <UserCodeSet>(); UserCodeSet defaultSet = new UserCodeSet(); defaultSet.Name = "普通行情"; defaultSet.Id = Guid.NewGuid().ToString(); defaultSet.IsDefault = true; defaultSet.FileName = LASTCODEINFOPATH; defaultSet.IsArbitrage = false; _UserCodeSetList.Add(defaultSet); UserCodeSet GroupSet = new UserCodeSet(); GroupSet.Name = "组合行情"; GroupSet.Id = Guid.NewGuid().ToString(); GroupSet.IsDefault = true; GroupSet.FileName = LASTGROUPCODEINFOPATH; GroupSet.IsArbitrage = true; _UserCodeSetList.Add(GroupSet); } //再读取各合约组的合约情况 foreach (var item in _UserCodeSetList) { if (File.Exists(LASTCODEINFODIR + item.FileName)) { ObservableCollection <Contract> userCodes = new ObservableCollection <Contract>(); try { FileStream fs2 = new FileStream(LASTCODEINFODIR + item.FileName, FileMode.OpenOrCreate); BinaryFormatter formatter = new BinaryFormatter(); ObservableCollection <Contract> temp = (ObservableCollection <Contract>)formatter.Deserialize(fs2); foreach (Contract codeInfo in temp) { if (codeInfo != null && !CodeSetManager.IsOutOfDate(codeInfo.Code)) { userCodes.Add(codeInfo); } } fs2.Close(); } catch (System.Exception ex) { Util.Log_Error("exception: " + ex.Message); Util.Log_Error("exception: " + ex.StackTrace); } //要对userCodes进行处理,保证无重复的内容。 Dictionary <Contract, Boolean> codeDict = new Dictionary <Contract, bool>(); for (int i = 0; i < userCodes.Count; i++) { if (userCodes[i] != null) { if (codeDict.ContainsKey(userCodes[i])) { userCodes.RemoveAt(i); i -= 1; } else { codeDict.Add(userCodes[i], true); } } } while (userCodes.Count > USER_CODE_SET_MAX_CODES && item.IsZhuli == false) { userCodes.RemoveAt(userCodes.Count - 1); } if (item.IsZhuli) { Util.Log("添加UserCodes到主力合约: userCodes的个数为" + userCodes.Count.ToString()); foreach (Contract codeInfo in userCodes) { if (codeInfo != null) { //Util.Log("--CodeInfo为:" + codeInfo.ToString()); } else { //Util.Log("--CodeInfo为null"); } } } _DicUserCodeSetCodeInfo.Add(item.Id, userCodes); } else { _DicUserCodeSetCodeInfo.Add(item.Id, new ObservableCollection <Contract>()); } } } //AddZhuliCodeTemp(userCodeSetList); return(_UserCodeSetList); }