/// <summary> /// Initializes a new instance of the <see cref="ParameterMapping"/> class. /// </summary> /// <param name="attribute">The attribute.</param> /// <param name="property">The property.</param> public ParameterMapping(ParameterAttributeBase attribute, PropertyInfo property) { this.Attribute = attribute; this.Property = property; var parameterName = attribute.ParameterName; if (string.IsNullOrWhiteSpace(parameterName)) { parameterName = property.Name; } if (!string.IsNullOrWhiteSpace(attribute.Prefix)) { parameterName = attribute.Prefix + parameterName; } this.FullParameterName = parameterName; }
/// <summary> /// Deserializes the property. /// </summary> /// <param name="settings">The settings.</param> /// <param name="property">The property.</param> /// <param name="propertyValue">The property value.</param> /// <exception cref="System.InvalidCastException"></exception> private void DeserializeProperty(T settings, PropertyInfo property, ParameterAttributeBase attribute, string propertyValue) { try { var valueType = propertyValue.GetType(); var propertyType = property.PropertyType; if (propertyType.GetGenericArguments().Any() && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { // Nullable type propertyType = propertyType.GetGenericArguments()[0]; if (string.IsNullOrEmpty(propertyValue)) { property.SetValue(settings, null, null); return; } } if (propertyType == valueType) { // The property and settingsValue have the same type - no conversion needed - just update! property.SetValue(settings, propertyValue, null); return; } if (!string.IsNullOrEmpty(attribute.Serializer)) { var deserializedValue = CallSerializerMethod(attribute.Serializer, property.PropertyType, propertyValue, nameof(ISettingsSerializer <T> .Deserialize)); property.SetValue(settings, deserializedValue, null); return; } if (propertyType.BaseType == typeof(Enum)) { // The property is an enum. Determine if the enum value is persisted as string or numeric. if (Regex.IsMatch(propertyValue, "^\\d+$")) { // The enum value is a number property.SetValue(settings, Enum.ToObject(propertyType, Convert.ToInt32(propertyValue, CultureInfo.InvariantCulture)), null); } else { try { property.SetValue(settings, Enum.Parse(propertyType, propertyValue, true), null); } catch (ArgumentException exception) { // Just log the exception. Use the default. Exceptions.LogException(exception); } } return; } TimeSpan timeSpanValue; if (propertyType.IsAssignableFrom(typeof(TimeSpan)) && TimeSpan.TryParse(propertyValue, CultureInfo.InvariantCulture, out timeSpanValue)) { property.SetValue(settings, timeSpanValue); return; } DateTime dateTimeValue; if (propertyType.IsAssignableFrom(typeof(DateTime)) && DateTime.TryParse(propertyValue, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeValue)) { property.SetValue(settings, dateTimeValue); return; } if (propertyType.GetInterface(typeof(IConvertible).FullName) != null) { propertyValue = ChangeFormatForBooleansIfNeeded(propertyType, propertyValue); property.SetValue(settings, Convert.ChangeType(propertyValue, propertyType, CultureInfo.InvariantCulture), null); return; } var converter = TypeDescriptor.GetConverter(propertyType); if (converter.IsValid(propertyValue)) { converter.ConvertFromInvariantString(propertyValue); } } catch (Exception exception) { // TODO: Localize exception throw new InvalidCastException(string.Format(CultureInfo.CurrentUICulture, "Could not cast {0} to property {1} of type {2}", propertyValue, property.Name, property.PropertyType), exception); } }
/// <summary> /// Deserializes the property. /// </summary> /// <param name="settings">The settings.</param> /// <param name="property">The property.</param> /// <param name="propertyValue">The property value.</param> /// <exception cref="System.InvalidCastException"></exception> private void DeserializeProperty(T settings, PropertyInfo property, ParameterAttributeBase attribute, string propertyValue) { SerializationController.DeserializeProperty(settings, property, propertyValue, attribute.Serializer); }