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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
            public void bind_handles_null_configuration()
            {
                Configuration configuration = new Configuration();
                TestBinder    binder        = new TestBinder(configuration);
                ITypeAccessor typeAccessor  = TypeAccessorFactory.Create(typeof(Test));
                IMember       member        = typeAccessor.GetMembers().First();

                binder.Bind(typeAccessor, member, CreateContext(null));
            }
Esempio n. 4
0
        public void does_not_bind_when_concrete_enumerable_type_cannot_be_found()
        {
            IEnumerableValueBinder valueBinder    = new IEnumerableValueBinder(new Configuration());
            ITypeAccessor          typeAccessor   = TypeAccessorFactory.Create(typeof(Test));
            IMember            member             = typeAccessor.GetMembers().First();
            object             source             = new { };
            Test               target             = new Test();
            ValueBinderContext valueBinderContext = CreateContext(source, target);

            valueBinder.Bind(typeAccessor, member, valueBinderContext);

            typeAccessor.GetValue(target, member.Name).MustBeNull();
        }
Esempio n. 5
0
        public void bind_does_not_overwrite_with_null_when_no_source_value_found()
        {
            ITypeAccessor typeAccessor = TypeAccessorFactory.Create(typeof(Test));
            object        source       = new { };
            Test          target       = new Test
            {
                Foo = "foo"
            };
            IMember            member             = typeAccessor.GetMembers().First();
            ValueBinderContext valueBinderContext = CreateContext(source, target);

            new DefaultValueBinder(new Configuration()).Bind(typeAccessor, member, valueBinderContext);

            // check that the binder doesn't write null when it doesn't find a source value
            typeAccessor.GetValue(target, member).MustEqual(target.Foo);
        }
Esempio n. 6
0
        public void binds_to_array()
        {
            Configuration          configuration = new Configuration();
            IEnumerableValueBinder valueBinder   = new IEnumerableValueBinder(new Configuration());
            ITypeAccessor          typeAccessor  = TypeAccessorFactory.Create(typeof(TestWithArray));
            IMember member = typeAccessor.GetMembers().First();
            object  source = new
            {
                Foo = "a-foo-value"
            };
            TestWithArray      target             = new TestWithArray();
            ValueBinderContext valueBinderContext = new ValueBinderContext(source, target, A.Fake <TargetConfiguration>(), new DefaultValueProvider(), new ObjectMapper(configuration));

            valueBinder.Bind(typeAccessor, member, valueBinderContext);

            target.Tests[0].Foo.MustEqual("a-foo-value");
        }
Esempio n. 7
0
        public override bool TryGetValue(object source, string name, out object value)
        {
            ITypeAccessor         sourceAccessor = TypeAccessorFactory.Create(source.GetType());
            IEnumerable <IMember> sourceMembers  = sourceAccessor.GetMembers();
            IMember sourceMember = sourceMembers.FirstOrDefault(x => x.CanRead && string.Equals(x.Name, name));

            value = null;

            if (sourceMember == null)
            {
                return(false);
            }

            value = sourceAccessor.GetValue(source, name);

            return(true);
        }
Esempio n. 8
0
 public Int16Accessor(ITypeAccessor <UInt16> accessorU16)
 {
     _accessorU16 = accessorU16;
 }
Esempio n. 9
0
 public Int32Accessor(ITypeAccessor <UInt32> accessorU32)
 {
     _accessorU32 = accessorU32;
 }
Esempio n. 10
0
 public Int64Accessor(ITypeAccessor <UInt64> accessorU64)
 {
     _accessorU64 = accessorU64;
 }
Esempio n. 11
0
 public FloatAccessor(ITypeAccessor <UInt32> accessorUInt32)
 {
     _accessorUInt32 = accessorUInt32;
 }
Esempio n. 12
0
 public BooleanAccessor(ITypeAccessor <Byte> accessorByte)
 {
     _accessorByte = accessorByte;
 }
Esempio n. 13
0
 /// <summary>
 /// Returns the value for the member.
 /// </summary>
 /// <param name="typeAccessor"></param>
 /// <param name="obj"></param>
 /// <param name="member"></param>
 /// <returns></returns>
 public static object GetValue(this ITypeAccessor typeAccessor, object obj, IMember member)
 {
     return(typeAccessor.GetValue(obj, member.Name));
 }
Esempio n. 14
0
 public DoubleAccessor(ITypeAccessor <UInt64> accessorUInt64)
 {
     _accessorUInt64 = accessorUInt64;
 }
Esempio n. 15
0
 public VarIntAccessor(ITypeAccessor <Byte> accessorByte)
 {
     _accessorByte = accessorByte;
 }
Esempio n. 16
0
 public static void SetValue(this ITypeAccessor typeAccessor, object obj, IMember member, object value)
 {
     typeAccessor.SetValue(obj, member.Name, value);
 }
Esempio n. 17
0
 public StringAccessor(ITypeAccessor <VarInt> accessorVarInt, ITypeAccessor <Byte> accessorByte)
 {
     _accessorByte   = accessorByte;
     _accessorVarInt = accessorVarInt;
 }
Esempio n. 18
0
 public SByteAccessor(ITypeAccessor <Byte> accessorByte)
 {
     _accessorByte = accessorByte;
 }
Esempio n. 19
0
 public UInt64Accessor(ITypeAccessor <Byte> accessorByte)
 {
     _accessorByte = accessorByte;
 }
Esempio n. 20
0
 public override void Bind(ITypeAccessor targetAccessor, IMember targetMember, ValueBinderContext valueBinderContext)
 {
     GetSourceNameOrDefault(targetMember, valueBinderContext.TargetConfiguration);
 }