public object Read(string subPath, string property, object defaultValue) { Validate.IsNotNull(property, nameof(property)); Validate.IsNotEmpty(property, nameof(property)); var collection = subPath != null?Path.Combine(_root, subPath) : _root; _store.CreateCollection(collection); if (defaultValue is bool b) { return(_store.GetBoolean(collection, property, b)); } if (defaultValue is int i) { return(_store.GetInt32(collection, property, i)); } if (defaultValue is uint u) { return(_store.GetUInt32(collection, property, u)); } if (defaultValue is long l) { return(_store.GetInt64(collection, property, l)); } if (defaultValue is ulong ul) { return(_store.GetUInt64(collection, property, ul)); } return(_store.GetString(collection, property, defaultValue?.ToString() ?? "")); }
/// <summary> /// Read from a settings store /// </summary> /// <typeparam name="T"></typeparam> /// <param name="subpath">The subcollection path (appended to the path passed to the constructor)</param> /// <param name="property">The property name to read</param> /// <param name="defaultValue">The default value to use in case the property doesn't exist. /// The type of the default value will be used to figure out the proper way to read the property, so if pass null, /// the property will be read as a string (which may or may not be what you want)</param> /// <returns></returns> public object Read(string subpath, string property, object defaultValue) { Guard.ArgumentNotNull(property, nameof(property)); Guard.ArgumentNotEmptyString(property, nameof(property)); var collection = subpath != null?Path.Combine(root, subpath) : root; store.CreateCollection(collection); if (defaultValue is bool) { return(store.GetBoolean(collection, property, (bool)defaultValue)); } else if (defaultValue is int) { return(store.GetInt32(collection, property, (int)defaultValue)); } else if (defaultValue is uint) { return(store.GetUInt32(collection, property, (uint)defaultValue)); } else if (defaultValue is long) { return(store.GetInt64(collection, property, (long)defaultValue)); } else if (defaultValue is ulong) { return(store.GetUInt64(collection, property, (ulong)defaultValue)); } return(store.GetString(collection, property, defaultValue?.ToString() ?? "")); }
public static T ReadSetting <T>(string propertyName) { CreatePropertyIfDoesNotExist <T>(propertyName); object value; switch (Type.GetTypeCode(typeof(T))) { case TypeCode.String: value = _userSettingsStore.GetString(CollectionName, propertyName); return((T)Convert.ChangeType(value, typeof(T))); case TypeCode.Boolean: value = _userSettingsStore.GetBoolean(CollectionName, propertyName); return((T)Convert.ChangeType(value, typeof(T))); case TypeCode.Int32: value = _userSettingsStore.GetInt32(CollectionName, propertyName); return((T)Convert.ChangeType(value, typeof(T))); case TypeCode.Int64: value = _userSettingsStore.GetInt64(CollectionName, propertyName); return((T)Convert.ChangeType(value, typeof(T))); case TypeCode.UInt32: value = _userSettingsStore.GetUInt32(CollectionName, propertyName); return((T)Convert.ChangeType(value, typeof(T))); case TypeCode.UInt64: value = _userSettingsStore.GetUInt64(CollectionName, propertyName); return((T)Convert.ChangeType(value, typeof(T))); default: value = _userSettingsStore.GetString(CollectionName, propertyName); return((T)Convert.ChangeType(value, typeof(T))); } }