本文整理汇总了C#中MemberMapper.ClearMapCache方法的典型用法代码示例。如果您正苦于以下问题:C# MemberMapper.ClearMapCache方法的具体用法?C# MemberMapper.ClearMapCache怎么用?C# MemberMapper.ClearMapCache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemberMapper
的用法示例。
在下文中一共展示了MemberMapper.ClearMapCache方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: CreateMapIsThreadSafe
public void CreateMapIsThreadSafe()
{
var mapper = new MemberMapper();
var thread1 = new Thread(() =>
{
while (true)
{
var map = mapper.CreateMap<Source, Destination>();
var result = map.MappingFunction(new Source { Foo = 1 }, new Destination());
Assert.AreEqual(1, result.Foo);
mapper.ClearMapCache();
}
});
var thread2 = new Thread(() =>
{
while (true)
{
var map = mapper.CreateMap<Source, Destination>();
var result = map.MappingFunction(new Source { Foo = 1 }, new Destination());
Assert.AreEqual(1, result.Foo);
mapper.ClearMapCache();
}
});
thread1.Start();
thread2.Start();
Thread.Sleep(2000);
thread1.Abort();
thread2.Abort();
}
示例3: 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);
}