/// <summary> /// Gets a mapping from a property on <see cref="ContentData"/> to a property on an instance of T. /// </summary> /// <typeparam name="T">The type that is being mapped to.</typeparam> /// <param name="sourcePropertyInfo">The property on the <see cref="ContentData"/>.</param> /// <param name="propertyInfo">The property on T.</param> /// <returns>An <see cref="Action"/> that maps a single property on <see cref="ContentData"/> to a single property on an instance of T.</returns> private static Action <ContentData, T> GetPropertyMapping <T>(PropertyInfo sourcePropertyInfo, PropertyInfo propertyInfo) where T : new() { var getProperty = ExpressionUtil.GetPropertyGetter <ContentData>(sourcePropertyInfo); var setProperty = ExpressionUtil.GetPropertySetter <T>(propertyInfo); return((contentData, t) => { var value = getProperty(contentData); setProperty(t, value); }); }
/// <summary> /// Gets a mapping from a metadata field on <see cref="ContentData"/> to a property on an instance of T. /// </summary> /// <typeparam name="T">The type that is being mapped to.</typeparam> /// <param name="propertyInfo">The property on T.</param> /// <param name="fieldName">The name of the metadata field to map.</param> /// <returns>An <see cref="Action"/> that maps a single metadata field on <see cref="ContentData"/> to a single property on an instance of T.</returns> private static Action <ContentData, T> GetPrimitiveMapping <T>(PropertyInfo propertyInfo, string fieldName) where T : new() { var mapToPropertyType = StringMapper.GetMapping(propertyInfo.PropertyType); var setProperty = ExpressionUtil.GetPropertySetter <T>(propertyInfo); return((contentData, t) => { var metadata = GetMetadata(contentData, fieldName); var value = mapToPropertyType(metadata.Text); setProperty(t, value); }); }
/// <summary> /// Gets a mapping from a metadata field with multiple values on <see cref="ContentData"/> to a property on an instance of T. /// </summary> /// <typeparam name="T">The type that is being mapped to.</typeparam> /// <param name="propertyInfo">The property on T.</param> /// <param name="fieldName">The name of the metadata field with multiple values to map.</param> /// <returns>An <see cref="Action"/> that maps a single metadata field on <see cref="ContentData"/> to a single property on an instance of T.</returns> private static Action <ContentData, T> GetEnumerableMapping <T>(PropertyInfo propertyInfo, string fieldName) where T : new() { var mapToPropertyType = StringMapper.GetEnumerableMapping(propertyInfo.PropertyType); var setProperty = ExpressionUtil.GetPropertySetter <T>(propertyInfo); return((contentData, t) => { var metadata = GetMetadata(contentData, fieldName); var rawValues = metadata.Text.Split(metadata.Separator[0]); var values = mapToPropertyType(rawValues); setProperty(t, values); }); }
/// <summary> /// Gets a mapping from multiple matching elements Smart Form XML elements to a property on an instance of T. /// </summary> /// <typeparam name="T">The type being mapped to.</typeparam> /// <param name="propertyInfo">The property on T being mapped to.</param> /// <param name="xpath">The xpath for the field values being mapped.</param> /// <returns>An <see cref="Action"/> that maps an multiple matching fields from the Smart Form XML to a property on an instance of T.</returns> private static Action <XNode, T> GetEnumerableMapping <T>(PropertyInfo propertyInfo, string xpath) where T : new() { var mapToPropertyType = StringMapper.GetEnumerableMapping(propertyInfo.PropertyType); var setProperty = ExpressionUtil.GetPropertySetter <T>(propertyInfo); return((xml, t) => { var elements = xml.XPathSelectElements(xpath).ToList(); if (!elements.Any()) { return; } var rawValues = elements.Select(GetXmlInnerText).ToList(); var values = mapToPropertyType(rawValues); setProperty(t, values); }); }
/// <summary> /// Gets a mapping from a single Smart Form XML element to a property on an instance of T. /// </summary> /// <typeparam name="T">The type being mapped to.</typeparam> /// <param name="propertyInfo">The property on T being mapped to.</param> /// <param name="xpath">The xpath for the field values being mapped.</param> /// <returns>An <see cref="Action"/> that maps a field value from the Smart Form XML to a property on an instance of T.</returns> private static Action <XNode, T> GetSingleMapping <T>(PropertyInfo propertyInfo, string xpath) where T : new() { var mapToPropertyType = StringMapper.GetMapping(propertyInfo.PropertyType); var setProperty = ExpressionUtil.GetPropertySetter <T>(propertyInfo); return((xml, t) => { var element = xml.XPathSelectElement(xpath); if (element == null) { return; } var text = GetXmlInnerText(element); var value = mapToPropertyType(text); setProperty(t, value); }); }
private static Action <XNode, T> GetPropertyMapping <T>(PropertyInfo propertyInfo, string xpath) where T : new() { var mapToPropertyType = StringMapper.GetMapping(propertyInfo.PropertyType); var setProperty = ExpressionUtil.GetPropertySetter <T>(propertyInfo); return((xml, t) => { var attrResult = (IEnumerable)xml.XPathEvaluate(xpath); var attr = attrResult.Cast <XAttribute>().FirstOrDefault(); if (attr == null) { return; } var value = mapToPropertyType(attr.Value); setProperty(t, value); }); }
/// <summary> /// Gets a mapping for a multiple sections (including properties on the object itself) of Smart Form XML onto a given property. /// </summary> /// <typeparam name="T">The type being mapped to.</typeparam> /// <param name="propertyInfo">The property on T.</param> /// <param name="xpath">The xpath representing the XML section to map.</param> /// <returns>An <see cref="Action"/> that maps a single Smart Form XML branch onto an instance of type T.</returns> public static Action <XNode, T> GetEnumerableMapping <T>(PropertyInfo propertyInfo, string xpath) where T : new() { var propertyType = propertyInfo.PropertyType.GetGenericArguments().First(); var setProperty = ExpressionUtil.GetPropertySetter <T>(propertyInfo); var getObjectCreatorMethod = typeof(SmartFormObjectMapper) .GetMethod("GetEnumerableObjectCreator", BindingFlags.NonPublic | BindingFlags.Static) .MakeGenericMethod(propertyType); var objectCreator = ExpressionUtil.GetMappingFromMethod <IEnumerable <XNode> >(getObjectCreatorMethod); return((xml, t) => { var elements = xml.XPathSelectElements(xpath).ToList(); if (!elements.Any()) { return; } var obj = objectCreator(elements); setProperty(t, obj); }); }
/// <summary> /// Gets a mapping for a single section (including properties on the object itself) of Smart Form XML onto a given property. /// </summary> /// <typeparam name="T">The type being mapped to.</typeparam> /// <param name="propertyInfo">The property on T.</param> /// <param name="xpath">The xpath representing the XML section to map.</param> /// <returns>An <see cref="Action"/> that maps a single Smart Form XML branch onto an instance of type T.</returns> public static Action <XNode, T> GetSingleMapping <T>(PropertyInfo propertyInfo, string xpath) where T : new() { var propertyType = propertyInfo.PropertyType; var setter = ExpressionUtil.GetPropertySetter <T>(propertyInfo); var getObjectCreatorMethod = typeof(SmartFormObjectMapper) .GetMethod("GetObjectCreator", BindingFlags.NonPublic | BindingFlags.Static) .MakeGenericMethod(propertyType); var objectCreator = ExpressionUtil.GetMappingFromMethod <XNode>(getObjectCreatorMethod); return((xml, t) => { var element = xml.XPathSelectElement(xpath); if (element == null) { return; } var obj = objectCreator(element); setter(t, obj); }); }