これらは私のドメインとビュークラスです:
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; }
}
これは私のマッピング拡張機能です:
public static TModel ToModel<TModel>(this IEntity entity)
where TModel : IBaseViewModel
{
return (TModel)Mapper.Map(entity, entity.GetType(), typeof(TModel));
}
使用方法は次のとおりです。
City city = GetCity();
CityModel model = city.ToModel<CityModel>();
しかし、一般的なリストでは機能しません:
List<City> cities = GetCities();
List<CityModel> model =cities.ToModel<CityModel>() // doesn't work
ジェネリック リストの拡張メソッドを作成することは可能ですか?