本文整理汇总了C#中MemberMapper.Map方法的典型用法代码示例。如果您正苦于以下问题:C# MemberMapper.Map方法的具体用法?C# MemberMapper.Map怎么用?C# MemberMapper.Map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemberMapper
的用法示例。
在下文中一共展示了MemberMapper.Map方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ClearMapCacheIsRespected
public void ClearMapCacheIsRespected()
{
var mapper = new MemberMapper();
mapper.CreateMap<SourceType, DestinationType>(customMapping: src => new
{
ID = src.ID * 10,
Name = src.Name + src.Name
});
var source = new SourceType
{
ID = 10,
Name = "x"
};
var result = mapper.Map<DestinationType>(source);
Assert.AreEqual(100, result.ID);
Assert.AreEqual("xx", result.Name);
mapper.ClearMapCache();
result = mapper.Map<DestinationType>(source);
Assert.AreEqual(10, result.ID);
Assert.AreEqual("x", result.Name);
}
示例2: MappingConditionIsRespectedForNestedCollectionMembers
//[TestMethod]
public void MappingConditionIsRespectedForNestedCollectionMembers()
{
var mapper = new MemberMapper();
int i = 10;
mapper.CreateMapProposal<SourceTypeCollection, DestinationTypeCollection>()
.ForMember(dest => dest.Nested).OnlyIf(src => i == 0)
.FinalizeMap();
var source = new SourceTypeCollection
{
Nested = new List<NestedSourceType>
{
new NestedSourceType
{
Foo = "Bla"
}
}
};
var result = mapper.Map<SourceTypeCollection, DestinationTypeCollection>(source);
Assert.IsNull(result.Nested);
//i = 0;
result = mapper.Map<SourceTypeCollection, DestinationTypeCollection>(source);
Assert.AreEqual("Bla", result.Nested.Single().Foo);
}
示例3: WithListStringToWithListString
public void WithListStringToWithListString()
{
var mapper = new MemberMapper();
mapper.Options.Debug.DebugInformationEnabled = true;
var source = new WithListofString { Foo = new List<string> { "test" } };
var result = mapper.Map(source, new WithListofString());
Assert.IsTrue(object.ReferenceEquals(source.Foo, result.Foo));
var x = result;
result.Foo = x.Foo;
var newResult = mapper.Map(source, result);
Assert.IsTrue(object.ReferenceEquals(result, newResult));
Assert.IsTrue(object.ReferenceEquals(result.Foo, newResult.Foo));
Assert.AreEqual(1, newResult.Foo.Count);
var otherSource = new WithListofString { Foo = new List<string> { "test1" } };
mapper.Map(otherSource, result);
Assert.IsTrue(object.ReferenceEquals(result.Foo, newResult.Foo));
Assert.AreEqual(2, result.Foo.Count);
}
示例4: MappingConditionIsRespected
public void MappingConditionIsRespected()
{
var mapper = new MemberMapper();
mapper.CreateMapProposal<SourceType, DestinationType>()
.ForMember(dest => dest.Name).OnlyIf(src => src.ID == 0)
.FinalizeMap();
var source = new SourceType
{
ID = 10,
Name = "X",
Nested = new NestedSourceType
{
Foo = "Bla"
}
};
var result = mapper.Map<SourceType, DestinationType>(source);
Assert.IsNull(result.Name);
source.ID = 0;
result = mapper.Map<SourceType, DestinationType>(source);
Assert.AreEqual("X", result.Name);
}
示例5: RecursiveRelationshipsAreMappedCorrectly
public void RecursiveRelationshipsAreMappedCorrectly()
{
var map = new MemberMapper();
var source = new SourceType
{
ID = 10,
Children = new List<SourceType>
{
new SourceType
{
ID = 11,
},
new SourceType
{
ID = 12,
Children = new List<SourceType>
{
new SourceType
{
ID = 13
}
}
}
}
};
var result = map.Map<SourceType, DestinationType>(source);
}
示例6: BaseClassMappingAndInheritedClassMappingIsRespected
public void BaseClassMappingAndInheritedClassMappingIsRespected()
{
var mapper = new MemberMapper();
mapper.Options.Strictness.ThrowWithoutCorrespondingSourceMember = true;
mapper.CreateMap<SourceBase, DestinationBase>(customMapping: src => new DestinationBase
{
Test = src.ID
});
mapper.CreateMap<SecondSourceInherited, SecondDestinationInherited>(customMapping: src => new SecondDestinationInherited
{
Bar = src.Foo
});
var result = mapper.Map<SecondSourceInherited, SecondDestinationInherited>(new SecondSourceInherited
{
ID = 10,
Foo = "test"
});
Assert.AreEqual(10, result.Test);
Assert.AreEqual("test", result.Bar);
}
示例7: ComplexTypeMappingRespectsExistingMapping
public void ComplexTypeMappingRespectsExistingMapping()
{
var mapper = new MemberMapper();
var proposed = mapper.CreateMap(typeof(ComplexSourceType), typeof(ComplexDestinationType),
(s, p, option) =>
{
if (s.Name == "Name")
{
option.IgnoreMember();
}
});
proposed.FinalizeMap();
var source = new ComplexSourceType
{
ID = 5,
Complex = new NestedSourceType
{
ID = 10,
Name = "test"
}
};
var destination = mapper.Map<ComplexSourceType, ComplexDestinationType>(source);
Assert.AreEqual(destination.ID, 5);
Assert.IsNotNull(destination.Complex);
Assert.AreNotEqual(destination.Complex.Name, source.Complex.Name);
}
示例8: NonNullableGetsMappedToNullableNonPrimitiveVT
public void NonNullableGetsMappedToNullableNonPrimitiveVT()
{
var mapper = new MemberMapper();
var result = mapper.Map<NonNullableSourceNonPrimitiveVT, NullableDestinationNonPrimitiveVT>(new NonNullableSourceNonPrimitiveVT { ID = 10 });
Assert.AreEqual(10, result.ID.Value);
}
示例9: ParameterIsUsed
public void ParameterIsUsed()
{
var mapper = new MemberMapper();
mapper.CreateMap<SourceType, DestinationType, int>((src, i) => new DestinationType
{
ID = i
});
var result = mapper.Map(new SourceType(), new DestinationType(), 10);
Assert.AreEqual(10, result.ID);
result = mapper.Map(new SourceType(), new DestinationType(), 15);
Assert.AreEqual(15, result.ID);
}
示例10: InvokingMapWithoutParameterAsMapWithParameterThrows
public void InvokingMapWithoutParameterAsMapWithParameterThrows()
{
var mapper = new MemberMapper();
mapper.CreateMap<SourceType, DestinationType>();
var result = mapper.Map(new SourceType(), new DestinationType(), 1);
}
示例11: IntIsMappedToEnum
public void IntIsMappedToEnum()
{
var mapper = new MemberMapper();
var result = mapper.Map<IntClass, EnumClass>(new IntClass { Foo = 2 });
Assert.AreEqual(Foo.B, result.Foo);
}
示例12: EnumIsMappedToInt
public void EnumIsMappedToInt()
{
var mapper = new MemberMapper();
var result = mapper.Map<EnumClass, IntClass>(new EnumClass { Foo = Foo.B });
Assert.AreEqual(2, result.Foo);
}
示例13: DirectMapping
public static void DirectMapping()
{
var customer = new Customer
{
ID = 1,
FirstName = "First",
LastName = "Last",
Orders = new List<Order>
{
new Order
{
ID = 1,
Amount = 10,
},
new Order
{
ID = 2,
Amount = 20
}
}
};
var mapper = new MemberMapper();
// Explicit source and destination type, just pass a source type instance
var dto = mapper.Map<Customer, CustomerDto>(customer);
// Just specify what ThisMember should map to, pass in any type that you think can be mapped
dto = mapper.Map<CustomerDto>(customer);
// Update the existing Customer, just set a new FirstName for him
dto = new CustomerDto
{
FirstName = "NewName"
};
// For that we have to set an option that null-values from the source are ignored, so that LastName does not get set to null
mapper.Options.Conventions.IgnoreMembersWithNullValueOnSource = true;
// Setting an option that affects map generation has no effect on maps that have already been generated. Normally there'd be little
// need to set this option on the fly, you would just have a seperate mapper for doing these mappings. But for this sample, it works fine.
mapper.ClearMapCache(); // We could also have called mapper.CreateMap<CustomerDto, Customer>(), which always recreates the map.
// Only FirstName will have changed now on customer, because the null (or null-equivalent in case of nullable value types) values were ignored.
mapper.Map(dto, customer);
}
示例14: MapForMapWithParameterThrowsForMapWithoutParameter
public void MapForMapWithParameterThrowsForMapWithoutParameter()
{
var mapper = new MemberMapper();
mapper.CreateMap<SourceType, DestinationType>();
mapper.Map<SourceType, DestinationType, int>(new SourceType(), 0);
}
示例15: DictionaryToIDictionaryIsReferenceEqual
public void DictionaryToIDictionaryIsReferenceEqual()
{
var mapper = new MemberMapper();
var source = new DictionaryClass { Foo = new Dictionary<int, string>() };
var result = mapper.Map(source, new IDictionaryClass());
Assert.IsTrue(object.ReferenceEquals(source.Foo, result.Foo));
}