/// <summary> /// 获取一个新的系统设置的副本 /// </summary> /// <param name="db">数据库连接项目</param> public SettingBase(IModelToDbContextOfBase db) { Type classType = this.GetType(); Dictionary<string, PropertyInfo> properties = new Dictionary<string, PropertyInfo>(); List<string> keys = classType.GetProperties() .Where(x => x.CanRead) .ToList() .ConvertAll(property => { string key = string.Format("[{0}]{1}", classType.FullName, property.Name); properties.Add(key, property); return key; }); List<SettingDetail> sds = db.SettingDetails.Where(x => keys.Contains(x.Key)).ToList(); keys.Where(key => !sds.Any(sd => sd.Key == key)).ToList() .ForEach(key => { string value = properties[key].GetValue(this).ToString(); SettingDetail sd = new SettingDetail(key, value); sds.Add(sd); }); this.details = sds; }
/// <summary> /// 设置 /// </summary> /// <param name="propertyName"></param> /// <param name="value"></param> protected void SetValue(string propertyName, object value) { Type classType = this.GetType(); string key = string.Format("[{0}]{1}", classType.FullName, propertyName); SettingDetail sd = this.details.FirstOrDefault(x => x.Key == key); if (sd == null) { sd = new SettingDetail(key, value.ToString()); this.details.Add(sd); return; } sd.Value = value.ToString(); }