private void AddSingleElementFunc <TProperty>(XName name, Expression <Func <T, TProperty> > propFunc, XmlMapper <TProperty> mapper) where TProperty : class, new() { Func <T, XElement> func = ObjectToXmlFuncGenerator <T> .GenerateSingleElemenetFunc(name, propFunc, mapper); _elementFuncs.Add(name, func); }
private void AddCollectionElementFunc <TProperty>(string name, Expression <Func <T, ICollection <TProperty> > > propFunc, XmlMapper <TProperty> mapper) where TProperty : class, new() { Func <T, XElement> func = ObjectToXmlFuncGenerator <T> .GenerateCollectionElementFunc(name, propFunc, mapper); _elementCollectionFuncs.Add(name, func); }
/// <summary> /// Adds a mapping for a collection of elements to a collection of properties of a complex type. /// </summary> /// <typeparam name="TProperty">The type of the objects in the property collection.</typeparam> /// <param name="name">The name of the XML element.</param> /// <param name="propFunc">An expression representing the object property.</param> /// <param name="mapper"> /// An <see cref="XmlMapper{T}" /> to map the child element to the complex property type. /// </param> public void Add <TProperty>(string name, Expression <Func <T, ICollection <TProperty> > > propFunc, XmlMapper <TProperty> mapper) where TProperty : class, new() { string childName = null; int slashIndex = name.IndexOf('/'); if (slashIndex > -1) { childName = name.Substring(slashIndex + 1); } _elementCollectionActions.Add(name, _xmlToObjectActionGenerator.Generate(propFunc, mapper, childName)); AddCollectionElementFunc(name, propFunc, mapper); }
public Action <XElement, TItem> Generate <TProperty>(Expression <Func <TItem, ICollection <TProperty> > > propFunc, XmlMapper <TProperty> mapper, string childName) where TProperty : class, new() { var property = propFunc.Body as MemberExpression; if (property == null) { throw new ArgumentException("Expression does not represent a Property."); } var generator = new CustomCollectionElementConverterActionGenerator <TItem, TProperty>((PropertyInfo)property.Member, x => mapper.ToObject(x), childName); return(generator.Generate()); }
/// <summary> /// Adds a mapping for an element to a property of a complex type. /// </summary> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="name">The name of the XML element.</param> /// <param name="propFunc">An expression representing the object property.</param> /// <param name="mapper"> /// An <see cref="XmlMapper{T}" /> to map the child element to the complex property type. /// </param> public void Add <TProperty>(XName name, Expression <Func <T, TProperty> > propFunc, XmlMapper <TProperty> mapper) where TProperty : class, new() { _elementActions.Add(name, _xmlToObjectActionGenerator.Generate(propFunc, mapper)); AddSingleElementFunc(name, propFunc, mapper); }
public static Func <T, XElement> GenerateCollectionElementFunc <TProperty>(string name, Expression <Func <T, ICollection <TProperty> > > propFunc, XmlMapper <TProperty> mapper) where TProperty : class, new() { var itemParam = Expression.Parameter(typeof(T)); var invoke = Expression.Invoke(propFunc, itemParam); var mapperConstant = Expression.Constant(mapper); XName containerElementName; XName childElementName; GetContainerElementName(name, out containerElementName, out childElementName); var containerNameConstant = Expression.Constant(containerElementName); var childNameConstant = Expression.Constant(childElementName); var propertyEnumerableType = typeof(IEnumerable <>).MakeGenericType(typeof(TProperty)); var toXml = mapper.GetType() .GetMethod("ToXml", new[] { propertyEnumerableType, typeof(XName), typeof(XName) }); var callToXml = Expression.Call(mapperConstant, toXml, invoke, containerNameConstant, childNameConstant); var func = Expression.Lambda <Func <T, XElement> >(callToXml, itemParam).Compile(); return(func); }
public static Func <T, XElement> GenerateSingleElemenetFunc <TProperty>(XName name, Expression <Func <T, TProperty> > propFunc, XmlMapper <TProperty> mapper) where TProperty : class, new() { var itemParam = Expression.Parameter(typeof(T)); var invoke = Expression.Invoke(propFunc, itemParam); var mapperConstant = Expression.Constant(mapper); var nameConstant = Expression.Constant(name); var toXml = mapper.GetType().GetMethod("ToXml", new[] { typeof(TProperty), typeof(XName) }); var callToXml = Expression.Call(mapperConstant, toXml, invoke, nameConstant); var func = Expression.Lambda <Func <T, XElement> >(callToXml, itemParam).Compile(); return(func); }