private void ReadIList(IList list, Type eleType, XElement[] eleEleArr, ConfigAttributeTypes configAttributeTypes) { ConfigDataType configDataType = this.GetDataType(eleType); string value, eleName = null; object obj; switch (configDataType) { case ConfigDataType.IDictionary: foreach (var eleEle in eleEleArr) { obj = Activator.CreateInstance(eleType); this.ReadIDictionary((IDictionary)obj, eleEle.Elements(eleName).ToArray(), eleType, configAttributeTypes); list.Add(obj); } break; case ConfigDataType.IList: var itemType2 = this.GetIListElementType(eleType); eleName = this.GetIListElementName(itemType2); XElement[] eleEleArr2; foreach (var eleEle in eleEleArr) { eleEleArr2 = eleEle.Elements(eleName).ToArray(); obj = this.CreateIList(eleType, eleEleArr2.Length); this.ReadIList((IList)obj, itemType2, eleEleArr2, configAttributeTypes); list.Add(obj); } break; case ConfigDataType.Object: PropertyInfo[] objPropertyInfoArr = this.GetTypePropertyInfos(eleType); int index = 0; foreach (var eleEle in eleEleArr) { obj = Activator.CreateInstance(eleType); this.ReadConfigToXml(eleEle, objPropertyInfoArr, ref obj, configAttributeTypes); if (list is Array) { list[index++] = obj; } else { list.Add(obj); } } break; case ConfigDataType.Basic: foreach (var eleEle in eleEleArr) { value = XmlEx.GetXElementAttributeValue(eleEle, _VALUE, true); list.Add(this.StringToObject(value, eleType)); } break; default: throw new NotImplementedException(configDataType.ToString()); } }
private void ReadItem(XElement ele, IConfigValueConverter converter, PropertyInfo propertyInfo, object ownerObj) { if (!propertyInfo.CanWrite) { throw new ArgumentException($"属性{propertyInfo.DeclaringType.FullName}.{propertyInfo.Name}不支持set操作"); } object value; string valueStr = XmlEx.GetXElementAttributeValue(ele, _VALUE, true); if (valueStr == null) { value = null; } else { if (converter != null) { value = converter.ConvertFrom(propertyInfo, valueStr); } else { value = this.StringToObject(valueStr, propertyInfo.PropertyType); } } propertyInfo.SetValue(ownerObj, value, null); }
private void ReadIDictionary(IDictionary dic, XElement[] eleEleArr, Type eleType, ConfigAttributeTypes configAttributeTypes) { XElement keyEle, valueEle; string keyStr, valueStr; object key, value = null; Type[] argsTypeArr = eleType.GetGenericArguments(); ConfigDataType configDataType = this.GetDataType(argsTypeArr[1]); string name2 = null, eleName2 = null, eleName3 = null; Type eleType2 = null; PropertyInfo[] objPropertyInfoArr = null; XElement[] eleEleArr2 = null; foreach (var childEle in eleEleArr) { keyEle = childEle.Element(_KEY); keyStr = XmlEx.GetXElementAttributeValue(keyEle, _VALUE, false); key = this.StringToObject(keyStr, argsTypeArr[0]); valueEle = childEle.Element(_VALUE); switch (configDataType) { case ConfigDataType.Basic: valueStr = XmlEx.GetXElementAttributeValue(valueEle, _VALUE, true); if (valueStr == null) { value = null; } else { value = this.StringToObject(valueStr, argsTypeArr[1]); } break; case ConfigDataType.IDictionary: if (name2 == null) { name2 = this.GetIListElementName(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIDictionaryElementName(null); } value = Activator.CreateInstance(argsTypeArr[1]); this.ReadIDictionary((IDictionary)value, valueEle.XPathSelectElements($"{name2}/{eleName2}").ToArray(), argsTypeArr[1], configAttributeTypes); break; case ConfigDataType.IList: if (eleType2 == null) { eleType2 = this.GetIListElementType(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIListElementName(argsTypeArr[1]); } if (eleName3 == null) { eleName3 = this.GetIListElementName(eleType2); } eleEleArr2 = valueEle.XPathSelectElements($"{eleName2}/{eleName3}").ToArray(); value = this.CreateIList(argsTypeArr[1], eleEleArr2.Length); this.ReadIList((IList)value, eleType2, eleEleArr2, configAttributeTypes); break; case ConfigDataType.Object: if (objPropertyInfoArr == null) { objPropertyInfoArr = this.GetTypePropertyInfos(argsTypeArr[1]); } if (eleName2 == null) { eleName2 = this.GetIListElementName(argsTypeArr[1]); } value = Activator.CreateInstance(argsTypeArr[1]); this.ReadConfigToXml(valueEle.Element(eleName2), objPropertyInfoArr, ref value, configAttributeTypes); break; default: throw new NotSupportedException(configDataType.ToString()); } dic[key] = value; } }
private void LoadColumnsSetting() { try { var columnSettingFilePath = this._getColSettingFilePath(); if (string.IsNullOrWhiteSpace(columnSettingFilePath) || !File.Exists(columnSettingFilePath)) { return; } var xdoc = XDocument.Load(columnSettingFilePath); var rootEle = xdoc.Root; int count = int.Parse(XmlEx.GetXElementAttributeValue(rootEle, "Count")); if (this._dgv.Columns.Count != count) { //项数不同,同数据源名称,但是内容有变,不加载,删除无效的设置文件 File.Delete(columnSettingFilePath); return; } //加载数据 List <dynamic> items = new List <dynamic>(); try { foreach (var itemEle in rootEle.Elements("Item")) { dynamic item = new ExpandoObject(); item.Name = XmlEx.GetXElementAttributeValue(itemEle, "Name"); item.Width = int.Parse(XmlEx.GetXElementAttributeValue(itemEle, "Width")); item.DisplayIndex = int.Parse(XmlEx.GetXElementAttributeValue(itemEle, "DisplayIndex")); item.Visible = bool.Parse(XmlEx.GetXElementAttributeValue(itemEle, "Visible")); items.Add(item); if (!this._dgv.Columns.Contains(item.Name)) { //不包含该列,同数据源名称,但是内容有变,不加载 return; } } } catch (Exception exi) { //数据有错误,不加载,删除无效的设置文件 Loger.Error(exi); File.Delete(columnSettingFilePath); return; } foreach (dynamic item in items) { DataGridViewColumn col = this._dgv.Columns[item.Name]; col.Width = item.Width; col.DisplayIndex = item.DisplayIndex; col.Visible = item.Visible; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet; } } catch (Exception ex) { Loger.Error(ex); } finally { this.OnRaiseColumnVisibleChangedNotify(null, true); } }