public static TAttribute Attribute <TAttribute>(this XElement @this, XName name) { var attribute = @this.Attribute(name); if (attribute == null) { throw new Exception($"Unable to find attribute '{name}'"); } return(ConvertEx.ChangeType <TAttribute>(attribute.Value)); }
protected TProperty GetSetting <TProperty>(TProperty defaultValue = default(TProperty), [CallerMemberName] string propertyName = "") { //var accessor = new PropertyAccessor<TProperty>(settingProperty); if (!_propertyCache.ContainsKey(propertyName)) { if (_appSettings.AllKeys.Contains(propertyName)) { var textValue = _appSettings[propertyName].Value; _propertyCache[propertyName] = ConvertEx.ChangeType <TProperty>(textValue); } else { SetSetting(defaultValue, propertyName); } } return((TProperty)_propertyCache[propertyName]); }
private void AssignProperty(string propertyName, string propertyValue) { var selectedProperties = _properties.Where(x => x.Name.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase)); if (selectedProperties.Count() <= 0) { Console.WriteLine($"'{propertyName}' is not a valid property name."); } else if (selectedProperties.Count() > 1) { Console.WriteLine($"Invalid settings file. There are too many properties named {propertyName}."); } else { Console.WriteLine($"Assigning {propertyName}={propertyValue}"); var property = selectedProperties.Single(); property.SetValue(_settings, ConvertEx.ChangeType(propertyValue, property.PropertyType)); } }
/// <summary> /// Look for XAML-style properties. /// </summary> public static TProperty Property <TProperty>(this XElement @this, string name) { if (@this.Attribute(name) != null) { return(@this.Attribute <TProperty>(name)); } else { var propertyElementName = string.Format("{0}.{1}", @this.Name, name); var propertyElement = @this.Element(propertyElementName); if (propertyElement != null) { return(ConvertEx.ChangeType <TProperty>(propertyElement.Value)); } else { throw new Exception($"Unable to find property '{name}'."); } } }
protected void SetSetting <TProperty>(TProperty value, [CallerMemberName] string propertyName = "") { //var accessor = new PropertyAccessor<TProperty>(settingProperty); var keyExists = _appSettings.AllKeys.Contains(propertyName); var isDirty = false; if (value == null) { if (keyExists) { _appSettings.Remove(propertyName); _propertyCache.Remove(propertyName); isDirty = true; } } else { if (keyExists) { var valueText = ConvertEx.ChangeType <string>(value); if (_appSettings[propertyName].Value != valueText) { _appSettings[propertyName].Value = valueText; _propertyCache[propertyName] = value; isDirty = true; } } else { _appSettings.Add(propertyName, value.ToString()); _propertyCache.Add(propertyName, value); isDirty = true; } } if (isDirty) { _config.Save(); ConfigurationManager.RefreshSection("appSettings"); } }