public static bool MappingIsList(XmlMapping mapping) { var type = mapping.PropertyCache.Property.PropertyType; return(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>)); }
public LadderCondition(XmlMapping mapping, int mappingIndex, XmlSerializerGenerator generator) { this.mapping = mapping; this.mappingIndex = mappingIndex; this.generator = generator; }
/// <summary> /// Generate an expression like /// /// if (obj.Items == null) /// obj.Items = new List(); /// </summary> /// <param name="target">The object containing an item property.</param> /// <param name="mapping">The mapping for the list property.</param> /// <returns>An expression for initializing an list of items.</returns> public static Tuple <MemberExpression, ConditionalExpression> InitializeListIfNull(Expression target, XmlMapping mapping) { var memberAccess = Expression.MakeMemberAccess(target, mapping.PropertyCache.Property); //obj.Items var @null = Expression.Constant(null, mapping.PropertyCache.Property.PropertyType); var isNull = Expression.Equal(memberAccess, @null); //obj.Items == null var newList = Expression.New(mapping.PropertyCache.Property.PropertyType); //new List() var assignNew = Expression.Assign(memberAccess, newList); var ifNullThenNew = Expression.IfThen(isNull, assignNew); //if(obj.Items == null) obj.Items = new List() return(Tuple.Create(memberAccess, ifNullThenNew)); }