public static IEnumerable <GeneralSettings> ParseObjectToSettingDataBase(GeneralSettingsApp settings) { var result = new List <GeneralSettings>(); foreach (var prop in settings.GetType().GetProperties()) { var setting = new GeneralSettings { SettingName = prop.Name }; if (prop.PropertyType.Name == typeof(bool).Name) { setting.ValueBool = (bool)prop.GetValue(settings); } else if ((prop.PropertyType.Name == typeof(int).Name) || (prop.PropertyType.IsEnum == true)) { setting.ValueInt = (int)prop.GetValue(settings); } else if (prop.PropertyType.Name == typeof(string).Name) { setting.ValueString = (string)prop.GetValue(settings); } else { throw new Exception(String.Format("Setting type object is not recognized - Property Name: {0} / Property Type: {1}", prop.Name, prop.PropertyType.Name)); } result.Add(setting); } return(result); }
private static GeneralSettingsApp ParsetDataBaseToSettingsObject(IEnumerable <GeneralSettings> generalSettings) { var settings = new GeneralSettingsApp(); foreach (var prop in settings.GetType().GetProperties()) { var item = generalSettings.FirstOrDefault(s => s.SettingName == prop.Name); if (item == null) { continue; } object settingsValue; if (item.ValueBool != null) { settingsValue = item.ValueBool; } else if (item.ValueInt != null) { settingsValue = item.ValueInt; } else if (item.ValueString != null) { settingsValue = item.ValueString; } else { settingsValue = null; } // throw new Exception(String.Format("Setting type data base is not recognized {0}", prop.Name)); prop.SetValue(settings, settingsValue); } return(settings); }