{"action":"created","issue":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/828","labels_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/828/labels{/name}","comments_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/828/comments","events_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/828/events","html_url":"https://github.com/AutoMapper/AutoMapper/issues/828","id":100773615,"number":828,"title":"Overriding destination type with .As<> does not work as in 3.3.1.0","user":{"login":"bluerobotch","id":3074874,"avatar_url":"https://avatars.githubusercontent.com/u/3074874?v=3","gravatar_id":"","url":"https://api.github.com/users/bluerobotch","html_url":"https://github.com/bluerobotch","followers_url":"https://api.github.com/users/bluerobotch/followers","following_url":"https://api.github.com/users/bluerobotch/following{/other_user}","gists_url":"https://api.github.com/users/bluerobotch/gists{/gist_id}","starred_url":"https://api.github.com/users/bluerobotch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluerobotch/subscriptions","organizations_url":"https://api.github.com/users/bluerobotch/orgs","repos_url":"https://api.github.com/users/bluerobotch/repos","events_url":"https://api.github.com/users/bluerobotch/events{/privacy}","received_events_url":"https://api.github.com/users/bluerobotch/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-08-13T13:49:57Z","updated_at":"2015-08-13T19:57:02Z","closed_at":null,"body":"We have a scenario where we map two base types and want to override the target type with .As<>(). The following example works with 3.3.1.0 but not with 4.0.4.0. In 4.0.4.0 AutoMapper tries to instantiate the ChildModelBase instead of ChildModel which leads to an exception because the type is abstract. The typeMapPlanCache in the ConfigurationStore seems to be correct, but the MappingEngine (IMappingEngineRunner.CreateObject()) does not take the destination type on the type map into account.\r\n\r\nIn real world we load an entity using NHibernate, which generates proxies for all entities.\r\n\r\n```cs\r\nusing AutoMapper;\r\nusing FluentAssertions;\r\nusing Moq;\r\nusing Xunit;\r\n\r\npublic class OverrideDestinationMappingsTest\r\n{\r\n    [Fact]\r\n    public void Map_WhenOverrideDestinationTypeAndSourceIsDerived_MustCreateOverriddenDestinationType()\r\n    {\r\n        InitializeMapper();\r\n\r\n        Entity entity = LoadEntity();\r\n\r\n        var model = Mapper.Map<Model>(entity);\r\n\r\n        model.Child.Should().BeOfType<ChildModel>();\r\n    }\r\n\r\n    private static Entity LoadEntity()\r\n    {\r\n        return Mock.Of<Entity>(x => x.Child == Mock.Of<ChildEntiy>());\r\n    }\r\n\r\n    private static void InitializeMapper()\r\n    {\r\n        Mapper.Initialize(c =>\r\n        {\r\n            c.CreateMap<Entity, Model>();\r\n\r\n            c.CreateMap<ChildEntiy, ChildModelBase>()\r\n                .Include<ChildEntiy, ChildModel>()\r\n                .ForMember(x => x.SomeValue, x => x.Ignore())\r\n                .As<ChildModel>();\r\n\r\n            c.CreateMap<ChildEntiy, ChildModel>();\r\n        });\r\n    }\r\n\r\n    public class Entity\r\n    {\r\n        public ChildEntiy Child { get; set; }\r\n    }\r\n\r\n    public class ChildEntiy\r\n    {\r\n        public string SomeValue { get; set; }\r\n    }\r\n\r\n    public class Model\r\n    {\r\n        public ChildModelBase Child { get; set; }\r\n    }\r\n\r\n    public abstract class ChildModelBase\r\n    {\r\n        public string SomeValue { get; set; }\r\n    }\r\n\r\n    public class ChildModel : ChildModelBase\r\n    {\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/comments/130821699","html_url":"https://github.com/AutoMapper/AutoMapper/issues/828#issuecomment-130821699","issue_url":"https://api.github.com/repos/AutoMapper/AutoMapper/issues/828","id":130821699,"user":{"login":"TylerCarlson1","id":6801536,"avatar_url":"https://avatars.githubusercontent.com/u/6801536?v=3","gravatar_id":"","url":"https://api.github.com/users/TylerCarlson1","html_url":"https://github.com/TylerCarlson1","followers_url":"https://api.github.com/users/TylerCarlson1/followers","following_url":"https://api.github.com/users/TylerCarlson1/following{/other_user}","gists_url":"https://api.github.com/users/TylerCarlson1/gists{/gist_id}","starred_url":"https://api.github.com/users/TylerCarlson1/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TylerCarlson1/subscriptions","organizations_url":"https://api.github.com/users/TylerCarlson1/orgs","repos_url":"https://api.github.com/users/TylerCarlson1/repos","events_url":"https://api.github.com/users/TylerCarlson1/events{/privacy}","received_events_url":"https://api.github.com/users/TylerCarlson1/received_events","type":"User","site_admin":false},"created_at":"2015-08-13T19:57:02Z","updated_at":"2015-08-13T19:57:02Z","body":"Does `entitiesToMap.Select(e => Mapper.Map<IClientDTO>(e)).ToList();` work as an alternative?"}}