public void Should_silently_skip_unmatching_value_when_allowing_type_validation() { var mapper = new DynamicObjectMapper(new DynamicObjectMapperSettings { SilentlySkipUnassignableMembers = true }); var obj = mapper.Map<CustomType>(dynamicObject); obj.ShouldNotBeNull(); }
public void Should_throw_when_preventing_type_validation() { var mapper = new DynamicObjectMapper(new DynamicObjectMapperSettings { SilentlySkipUnassignableMembers = false }); var ex = Assert.Throws<Exception>(() => mapper.Map<CustomType>(dynamicObject)); ex.InnerException.ShouldBeOfType<ArgumentException>(); ex.InnerException.Message.ShouldBe("Object of type 'System.Double' cannot be converted to type 'System.Int32'."); }
public When_mapping_from_list_of_known_types() { sourceObjects = new[] { new CustomReferenceType { Int32Property = 1, StringProperty="One" }, new CustomReferenceType { Int32Property = 2, StringProperty="Two" }, }; var mapper = new DynamicObjectMapper(isKnownTypeProvider: new IsKnownTypeProvider()); dynamicObjects = mapper.MapCollection(sourceObjects); }
public When_created_based_on_object_with_abstract_properties_with_type_information() { obj = new ClassWithAbstractProperties() { Ref = new A(), Value1 = "the value's pay load", Value2 = 222, Value3 = new object(), Value4 = new byte[] { 1, 22, 0, 44 }, }; var mapper = new DynamicObjectMapper(); dynamicObject = mapper.MapObject(obj); }
/// <summary> /// Maps a collection of <see cref="DynamicObject" />s into a collection of objects. /// </summary> /// <param name="objectMapper">The <see cref="IDynamicObjectMapper"/> instance used to map the <see cref="DynamicObject"/>s.</param> /// <param name="objects">Collection of <see cref="DynamicObject" /> to be mapped.</param> /// <param name="type">Target type for mapping, set this parameter to null if type information included within individual <see cref="DynamicObject" />s should be used.</param> /// <returns>Collection of objects created based on the <see cref="DynamicObject" />s specified.</returns> public static IEnumerable Map(this IDynamicObjectMapper objectMapper, IEnumerable <DynamicObject?> objects, Type?type = null) { if (objectMapper is null) { throw new ArgumentNullException(nameof(objectMapper)); } if (objects is null) { throw new ArgumentNullException(nameof(objects)); } IEnumerable <object?> source = objects.Select(x => objectMapper.Map(x, type)); return(type is null || type == typeof(object) ? source.ToArray() : (IEnumerable)DynamicObjectMapper.CastCollectionToArrayOfType(type, source)); }
public When_converting_to_object_with_dictionary_property() { dynamicObject = new DynamicObject(typeof(ClassWithDictionaryProperty)) { { "Dictionary", new object[] { new KeyValuePair<string,string>("K1", "V1"), new KeyValuePair<string,string>("K2", "V2"), new KeyValuePair<string,string>("K3", "V3"), } }, }; obj = new DynamicObjectMapper().Map(dynamicObject); objectWithDictionaryProperty = obj as ClassWithDictionaryProperty; }
public When_mapping_to_custom_dynamic_object() { source = new A { B = new B { C = new C { Int32Value = Int32Value } } }; source.B.A = source; source.B.C.A = source; var mapper = new DynamicObjectMapper(dynamicObjectFactory: new DynamicObjectFactory()); dynamicObject = mapper.MapObject(source); }