1

When I read the book 'ASP_NET-MVC-5-with-Bootstrap-and-Knockout_js', the blew code can't be compiled

AutoMapper.Mapper.CreateMap(_sourceType, _destinationType);

If I modify it Mapper.Initialize(cfg => cfg.CreateMap<_sourceType, _destinationType>()),according the new version guide,then got this error:Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name '_sourceType' could not be found (are you missing a using directive or an assembly reference?) Can anyone help me give some advice?

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) {
        }  
    }

I found someone also had the same question but no answer:http://www.endyourif.com/integrating-automapper-with-an-mvc-result-filter/

NXT
  • 1,981
  • 1
  • 24
  • 30
keji zhang
  • 11
  • 5
  • Find which version of automapper is used in the book and use that exact version. – CodingYoshi Aug 07 '17 at 04:18
  • If it can't be compiled please include the compilation error – Daniel Aug 07 '17 at 04:56
  • 3
    Possible duplicate of [AutoMapper.Mapper.CreateMap()' is obsolete](https://stackoverflow.com/questions/36398318/automapper-mapper-createmaptsource-tdestination-is-obsolete) – Samvel Petrosov Aug 07 '17 at 04:57
  • I know automapper Initialize been changed in the new version, but this question may be little different, because have generic question at the same time, I search url(https://github.com/AutoMapper/AutoMapper/wiki/Open-Generics), but still don't know how to do it. – keji zhang Aug 07 '17 at 05:57

0 Answers0