Exemplo n.º 1
0
        public static Expression GetFallbackCollectionValue(this IMemberMapperData mapperData)
        {
            var targetMember = mapperData.TargetMember;

            Expression emptyEnumerable;

            if (mapperData.TargetMemberHasInitAccessibleValue())
            {
                var existingValue = mapperData.GetTargetMemberAccess();

                if (mapperData.MapToNullCollections())
                {
                    return(existingValue);
                }

                emptyEnumerable = targetMember.Type.GetEmptyInstanceCreation(targetMember.ElementType);

                return(Expression.Coalesce(existingValue, emptyEnumerable));
            }

            if (mapperData.MapToNullCollections())
            {
                return(targetMember.Type.ToDefaultExpression());
            }

            emptyEnumerable = targetMember.Type.GetEmptyInstanceCreation(targetMember.ElementType);

            return(emptyEnumerable.GetConversionTo(targetMember.Type));
        }