public static T Map <T>(this Dictionary <string, AttributeValue> values) where T : Base, new() { var entity = new T(); var properties = typeof(T).GetProperties(); foreach (var property in properties) { var propertyType = property.PropertyType; if (values.ContainsKey(property.Name)) { if (AttributeValueConverter.ConvertToValue.ContainsKey(propertyType) && propertyType != typeof(object)) { property.SetValue(entity, AttributeValueConverter.ConvertToValue[propertyType](values[property.Name])); } else if (propertyType.IsArray || (propertyType.IsGenericType && propertyType.GetInterfaces().Contains(typeof(IEnumerable)))) { MapFromAttributeValueToArray(entity, values, propertyType, property); } else if (propertyType.IsClass) { var value = (Dictionary <string, AttributeValue>)AttributeValueConverter.ConvertToValue[typeof(object)](values[property.Name]); property.SetValue(entity, AttributeValueConverter.FromDictionary(propertyType, value)); } } } return(entity); }
/// <summary> /// Register an attribute value converter for a property value type. All types of properties must be registered /// to be able to be created using XML. /// </summary> /// <param name="type">Value type.</param> /// <param name="converter">Converter function.</param> public static void RegisterAttributeValueConverter(Type type, AttributeValueConverter converter) { if (!m_AttributeValueConverters.ContainsKey(type)) { m_AttributeValueConverters[type] = converter; } }
public static string ValueOrDefault([CanBeNull] this XAttribute attribute, string @default) { AttributeValueConverter <string> converter = (string input, out string output) => { output = input; return(true); }; return(ValueOrDefault(attribute, @default, converter)); }
public static T Map <T>(this Dictionary <string, AttributeValue> values) where T : Base, new() { var entity = new T(); var properties = typeof(T).GetProperties(); foreach (var property in properties) { var propertyType = property.PropertyType; if (values.ContainsKey(property.Name)) { if (AttributeValueConverter.ConvertToValue.ContainsKey(propertyType) && propertyType != typeof(object)) { property.SetValue(entity, AttributeValueConverter.ConvertToValue[propertyType](values[property.Name])); } else if (propertyType.IsArray || (propertyType.IsGenericType && propertyType.GetInterfaces().Contains(typeof(IEnumerable)))) { var declaredType = propertyType.GetDeclaringType(); if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(IDictionary <,>)) { var dictionary = AttributeValueConverter.ConvertToDictionary(propertyType, values[property.Name].M); property.SetValue(entity, dictionary); } else if (ListAttributeValueConverter.AllTypes.Contains(declaredType)) { var value = AttributeValueConverter.ConvertToArrayValue(declaredType, values[property.Name]); property.SetValue(entity, AttributeValueConverter.FromList(propertyType, value)); } else if (declaredType.IsClass && !values[property.Name].NULL) { var value = values[property.Name].L; property.SetValue(entity, AttributeValueConverter.FromList(propertyType, value)); } } else if (propertyType.IsClass) { var value = (Dictionary <string, AttributeValue>)AttributeValueConverter.ConvertToValue[typeof(object)](values[property.Name]); property.SetValue(entity, AttributeValueConverter.FromDictionary(propertyType, value)); } } } return(entity); }
public static decimal?ValueOrDefault([CanBeNull] this XAttribute attribute, decimal? @default) { AttributeValueConverter <decimal?> converter = (string input, out decimal? output) => { decimal outputAsDecimal; if (decimal.TryParse(input, out outputAsDecimal)) { output = outputAsDecimal; return(true); } output = null; return(false); }; return(ValueOrDefault(attribute, @default, converter)); }
private static void MapFromAttributeValueToArray <T>(T entity, Dictionary <string, AttributeValue> values, Type propertyType, PropertyInfo property) { var declaredType = propertyType.GetDeclaringType(); if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(IDictionary <,>)) { var dictionary = AttributeValueConverter.ConvertToDictionary(propertyType, values[property.Name].M); property.SetValue(entity, dictionary); } else if (ListAttributeValueConverter.AllTypes.Contains(declaredType)) { var value = AttributeValueConverter.ConvertToArrayValue(declaredType, values[property.Name]); value = value.Any() ? value : null; property.SetValue(entity, AttributeValueConverter.FromList(propertyType, value)); } else if (declaredType.IsClass && !values[property.Name].NULL) { var value = values[property.Name].L; property.SetValue(entity, AttributeValueConverter.FromList(propertyType, value)); } }
/// <summary> /// Serializes the attribute value. /// </summary> /// <param name="baseElement">The base element.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns></returns> private void SerializeAttributeValue(XElement baseElement, AttributeModel key) { string value = string.Empty; object typedValue = mAttributes[key]; if (typedValue != null) { switch (key.AttributeType.ToLowerInvariant()) { case "collection": // only collections are special cases { TrulyObservableCollection <UIControlInstanceModel> collection = typedValue as TrulyObservableCollection <UIControlInstanceModel>; if (collection.Count > 0) { AddToXMLChildElementsByID(baseElement, key.Name, collection); } } break; default: { AttributeValueConverter valueConverter = new AttributeValueConverter(); value = valueConverter.Convert(typedValue, typeof(string), key.AttributeType, System.Globalization.CultureInfo.InvariantCulture) as string; } break; } } else { value = string.Empty; } if (!string.IsNullOrEmpty(value)) { baseElement.SetAttributeValue(key.Name.ToLowerInvariant(), value); } }
public static int ValueOrDefault([CanBeNull] this XAttribute attribute, int @default) { AttributeValueConverter <int> converter = (string input, out int output) => int.TryParse(input, out output); return(ValueOrDefault(attribute, @default, converter)); }
public static T ValueOrDefault <T>([CanBeNull] this XAttribute attribute, T @default, [NotNull] AttributeValueConverter <T> converter) { if (converter == null) { throw new ArgumentNullException("converter"); } // If the attribute is missing, // we will use the default. if (null == attribute) { return(@default); } T value; // If we can't parse the attribute to the // target type, let's return the default. return((converter(attribute.Value, out value)) ? value : @default); }
public static decimal ValueOrDefault([CanBeNull] this XAttribute attribute, decimal @default) { AttributeValueConverter <decimal> converter = (string input, out decimal output) => decimal.TryParse(input, out output); return(ValueOrDefault(attribute, @default, converter)); }
public static DateTime ValueOrDefault([CanBeNull] this XAttribute attribute, DateTime @default) { AttributeValueConverter <DateTime> converter = (string input, out DateTime output) => DateTime.TryParse(input, out output); return(ValueOrDefault(attribute, @default, converter)); }
public static bool ValueOrDefault([CanBeNull] this XAttribute attribute, bool @default) { AttributeValueConverter <bool> converter = (string input, out bool output) => bool.TryParse(input, out output); return(ValueOrDefault(attribute, @default, converter)); }