Example #1
0
        public override void Bind(ITypeAccessor targetAccessor, IMember targetMember, ValueBinderContext valueBinderContext)
        {
            IEnumerable sourceEnumerable = FindSourceEnumerable(valueBinderContext.Source, targetMember, valueBinderContext.TargetConfiguration, valueBinderContext.ValueProvider);
            Type        enumerableType   = FindEnumerableType(targetMember, sourceEnumerable, valueBinderContext.TargetConfiguration);

            // if we don't have a type or the type is primative - we can't build an array list of it (prevents error when attempting to map to List<string> and we try and map to string and iterate through it's members)
            if (enumerableType == null || !IsMappableType(enumerableType))
            {
                return;
            }

            object value;

            if (sourceEnumerable != null)
            {
                // we have a source enumerable we need to convert to target enumerable
                value = valueBinderContext.ObjectMapper.MapAll(enumerableType, sourceEnumerable, declaringMember: targetMember);
            }
            else
            {
                // otherwise we create an array with a single entry where we've mapped from the original object and set the value to that
                value = valueBinderContext.ObjectMapper.Map(enumerableType, valueBinderContext.Source, valueProvider: valueBinderContext.ValueProvider, declaringMember: targetMember);
                ArrayList list = new ArrayList {
                    value
                };
                value = list.ToArray(enumerableType);
            }

            targetAccessor.SetValue(valueBinderContext.Result, targetMember.Name, value);
        }
        public override void Bind(ITypeAccessor targetAccessor, IMember targetMember, ValueBinderContext valueBinderContext)
        {
            object value;

            if (TryFindResolvedType(targetMember.Type, valueBinderContext.TargetConfiguration, out Type resolvedType))
            {
                value = valueBinderContext.ObjectMapper.Map(resolvedType, valueBinderContext.Source, valueProvider: valueBinderContext.ValueProvider, declaringMember: targetMember);
            }
            else
            {
                string name = GetSourceNameOrDefault(targetMember, valueBinderContext.TargetConfiguration);

                if (valueBinderContext.ValueProvider.TryGetValue(valueBinderContext.Source, name, out value) && value != null)
                {
                    ValueConverter valueConverter = Configuration.ValueConverters.First(x => x.CanConvert(targetMember.Type));
                    value = valueConverter.Convert(value, targetMember.Type);
                }
                else if (IsMappableType(targetMember.Type))
                {
                    value = valueBinderContext.ObjectMapper.Map(targetMember.Type, valueBinderContext.Source, valueProvider: valueBinderContext.ValueProvider, declaringMember: targetMember);
                }
                else
                {
                    return;
                }
            }

            targetAccessor.SetValue(valueBinderContext.Result, targetMember, value);
        }