当我读到“ASP_NET-MVC-5-with-Bootstrap-and-Knockout_js”这本书时,无法编译自爆代码
AutoMapper.Mapper.CreateMap(_sourceType, _destinationType);
如果我根据新版本指南修改 Mapper.Initialize(cfg => cfg.CreateMap< _sourceType,_destinationType>()),则会出现此错误:严重性代码说明项目文件线路抑制状态 错误CS0246找不到类型或命名空间名称'_sourceType'(您是否缺少using指令或程序集引用?) 谁能帮我提一些建议?
namespace BootstrapIntroduction.Filters {
[AttributeUsage(AttributeTargets.Method)]
public class GenerateResultListFilterAttribute : FilterAttribute, IResultFilter {
private readonly Type _sourceType;
private readonly Type _destinationType;
public GenerateResultListFilterAttribute(Type sourceType, Type destinationType) {
_sourceType = sourceType;
_destinationType = destinationType;
}
public void OnResultExecuting(ResultExecutingContext filterContext) {
var model = filterContext.Controller.ViewData.Model;
var resultListGenericType = typeof(ResultList<>).MakeGenericType(new Type[] { _destinationType });
var srcGenericType = typeof(List<>).MakeGenericType(new Type[] { _sourceType });
var destGenericType = typeof(List<>).MakeGenericType(new Type[] { _destinationType });
AutoMapper.Mapper.CreateMap(_sourceType, _destinationType);
var viewModel = AutoMapper.Mapper.Map(model, srcGenericType, destGenericType);
var queryOptions = filterContext.Controller.ViewData.ContainsKey("QueryOptions") ?
filterContext.Controller.ViewData["QueryOptions"] : new QueryOptions();
var resultList = Activator.CreateInstance(resultListGenericType, viewModel, queryOptions);
filterContext.Controller.ViewData.Model = resultList;
}
public void OnResultExecuted(ResultExecutedContext filterContext) {
}
}
我发现有人也有同样的问题,但没有回答:http://www.endyourif.com/integrating-automapper-with-an-mvc-result-filter/