http://docs.automapper.org/en/stable/Getting-started.html

IMappingExpression<TSource, TDestination> CreateMap<TSource, TDestination>(MemberList memberList);

Mapper.Initialize(cfg => cfg.CreateMap<Order, OrderDto>());
//or
var config = new MapperConfiguration(cfg => cfg.CreateMap<Order, OrderDto>());    IMappingExpression<TSource, TDestination> ForMember<TMember>(Expression<Func<TDestination, TMember>> destinationMember, Action<IMemberConfigurationExpression<TSource, TDestination, TMember>> memberOptions);
var mapper = config.CreateMapper();
// or
var mapper = new Mapper(config);
OrderDto dto = mapper.Map<OrderDto>(order);
// or
OrderDto dto = Mapper.Map<OrderDto>(order);
 

C# : Converting Base Class to Child Class

https://stackoverflow.com/a/25653977/3782855

'm surprised AutoMapper hasn't come up as an answer.

As is clear from all the previous answers, you cannot do the typecast. However, using AutoMapper, in a few lines of code you can have a new SkyfilterClient instantiated based on an existing NetworkClient.

In essence, you would put the following where you are currently doing your typecasting:

using AutoMapper;
...
// somewhere, your network client was declared
var existingNetworkClient = new NetworkClient();
...
// now we want to type-cast, but we can't, so we instantiate using AutoMapper
AutoMapper.Mapper.CreateMap<NetworkClient, SkyfilterClient>();
var skyfilterObject = AutoMapper.Mapper.Map<SkyfilterClient>(existingNetworkClient);

Here's a full-blown example:

 public class Vehicle
{
public int NumWheels { get; set; }
public bool HasMotor { get; set; }
} public class Car: Vehicle
{
public string Color { get; set; }
public string SteeringColumnStyle { get; set; }
} public class CarMaker
{
// I am given vehicles that I want to turn into cars...
public List<Car> Convert(List<Vehicle> vehicles)
{
var cars = new List<Car>();
AutoMapper.Mapper.CreateMap<Vehicle, Car>(); // Declare that we want some automagic to happen
foreach (var vehicle in vehicles)
{
var car = AutoMapper.Mapper.Map<Car>(vehicle);
// At this point, the car-specific properties (Color and SteeringColumnStyle) are null, because there are no properties in the Vehicle object to map from.
// However, car's NumWheels and HasMotor properties which exist due to inheritance, are populated by AutoMapper.
cars.Add(car);
}
return cars;
}
}
 

最新文章

  1. 编译OpenCV文档
  2. mysql my.cnf 配置详解
  3. PyCharm 5 破解注册方法
  4. 移动Web应用开发入门指南——视觉篇
  5. Set集合——HashSet、TreeSet、LinkedHashSet(2015年07月06日)
  6. SQL Server查询所有的表名/空间占用量/行数
  7. poj 3229 The Best Travel Design ( 图论+状态压缩 )
  8. 常用元素的属性/方法 attr / val / html /text
  9. 使用 Fluent API 配置/映射属性和类型2
  10. 用Total Commander替换windos默认资源管理器
  11. C语言栈的实现
  12. 不能取组织ID
  13. 家庭洗车APP --- Androidclient开展 之 网络框架包介绍(一)
  14. numpy教程:统计函数Statistics
  15. mssql sqlserver with cte表达式(递归)找出最顶值的方法分享
  16. MySQL中的EXPLAIN
  17. 节流(Throttling)和去抖(Debouncing)详解
  18. _pvp_killed_loot
  19. pip 离线安装
  20. (原+译)使用numpy.savez保存字典后读取的问题

热门文章

  1. mac mamp host 配置
  2. spring cloud深入学习(三)-----服务消费
  3. jodatime 计算时间差_统计程序运行耗时
  4. java实体类的属性名首字母不能大写,不然el表达式无法取值
  5. matplotlib无法显示中文
  6. Linxu SSH登陆出现Access Denied错误的解决方法
  7. CHARINDEX函数
  8. day37 01-上次课内容回顾
  9. Struts_改写客户列表练习
  10. Django项目:CRM(客户关系管理系统)--16--08PerfectCRM实现King_admin显示注册表的字段表头