/// <summary> /// Gets a mapping for properties from metadata fields on <see cref="ContentData"/> to an instance of T. /// </summary> /// <typeparam name="T">The type being mapped to.</typeparam> /// <returns>An <see cref="Action"/> that maps metadata fields onto an instance of type T.</returns> public static Action <ContentData, T> GetMapping <T>() where T : new() { var properties = typeof(T).GetProperties(); var propertyMappings = new List <Action <ContentData, T> >(); foreach (var propertyInfo in properties) { var attribute = propertyInfo.GetCustomAttribute <MetadataAttribute>(); if (attribute == null) { continue; } if (string.IsNullOrWhiteSpace(attribute.FieldName)) { continue; } if (StringMapper.IsMappable(propertyInfo.PropertyType)) { propertyMappings.Add(GetPrimitiveMapping <T>(propertyInfo, attribute.FieldName)); } else if (StringMapper.IsMappableEnumerable(propertyInfo.PropertyType)) { propertyMappings.Add(GetEnumerableMapping <T>(propertyInfo, attribute.FieldName)); } } return((contentData, t) => propertyMappings.ForEach(mapping => mapping(contentData, t))); }
/// <summary> /// Gets a mapping from all mapped Smart Form XML field values to the properties on an instance of T. /// </summary> /// <typeparam name="T">The type being mapped to.</typeparam> /// <returns>An <see cref="Action"/> that maps the field values from the Smart Form XML to the instance of T.</returns> public static Action <XNode, T> GetMapping <T>() where T : new() { var properties = typeof(T).GetProperties(); var propertyMappings = new List <Action <XNode, T> >(); foreach (var propertyInfo in properties) { var attribute = propertyInfo.GetCustomAttribute <SmartFormFieldValueAttribute>(); if (attribute == null) { continue; } if (string.IsNullOrWhiteSpace(attribute.Xpath)) { continue; } if (StringMapper.IsMappable(propertyInfo.PropertyType)) { propertyMappings.Add(GetSingleMapping <T>(propertyInfo, attribute.Xpath)); } else if (StringMapper.IsMappableEnumerable(propertyInfo.PropertyType)) { propertyMappings.Add(GetEnumerableMapping <T>(propertyInfo, attribute.Xpath)); } } return((xml, t) => propertyMappings.ForEach(mapping => mapping(xml, t))); }