public abstract class Entity : IEntity
{
[Key]
public virtual int Id { get; set; }
}
public class City:Entity
{
public string Code { get; set; }
}
public class BaseViewModel:IBaseViewModel
{
public int Id { get; set; }
}
public class CityModel:BaseViewModel
{
public string Code { get; set; }
}
我的域和 View 类...
和
用于映射扩展
public static TModel ToModel<TModel,TEntity>(this TEntity entity)
where TModel:IBaseViewModel where TEntity:IEntity
{
return Mapper.Map<TEntity, TModel>(entity);
}
我正在使用如下
City city = GetCity(Id);
CityModel model = f.ToModel<CityModel, City>();
但是它很长
我可以像下面这样写吗?
City city = GetCity(Id);
CityModel model = f.ToModel();
这可能吗?
最佳答案
为什么不直接使用:
public static TDestination ToModel<TDestination>(this object source)
{
return Mapper.Map<TDestination>(source);
}
关于c# - 自动映射器的通用扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9822723/