Automapper to interface mapping: TypeLoadException exception

I am working on an MVC application that uses Inversion of Control and therefore makes extensive use of interface types, with specific implementations being introduced by the dependent converter as needed. Entity interfaces inherit from the base interface, which describes some basic management functions for entities. ViewModels are also widely used.

The application uses Automapper, and I created mappings from view models to various entity interfaces. The display configuration is validated correctly. However, when I call Automapper to do the mapping, the code crashes with a TypeLoadException .

I believe Automapper is able to map to interfaces (see this from Jimmy Bogard).

It seems possible that the Automapper proxy generator has omitted adding MyMethod () to the proxy, and this throws an exception when Reflection tries to create a type.

If this is not the case, how do I get this card to work? Am I missing something obvious?

Here's a simplified console application that demonstrates a script and that reproduces an error on startup:

 public interface IEntity { string Foo { get; set; } string Bar { get; set; } string MyMethod(); } public class MyEntity : IEntity { public string Foo { get; set; } public string Bar { get; set; } public string MyMethod() { throw new NotImplementedException(); } } public class MyViewModel { public string Foo { get; set; } public string Bar { get; set; } } class Program { static void Main(string[] args) { AutomapperConfig(); MyViewModel vm = new MyViewModel { Foo = "Hello", Bar = "World" }; IEntity e = Mapper.Map<MyViewModel, IEntity>(vm); Console.WriteLine(string.Format("{0} {1}", e.Foo, e.Bar)); } private static void AutomapperConfig() { Mapper.Initialize(cfg => { cfg.CreateMap<MyViewModel, IEntity>(); }); Mapper.AssertConfigurationIsValid(); } } 

The exception is:

 InnerException: System.TypeLoadException HResult=-2146233054 Message=Method 'MyMethod' in type 'Proxy<AutomapperException.IEntity_AutomapperException_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null>' from assembly 'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005' does not have an implementation. Source=mscorlib TypeName=Proxy<AutomapperException.IEntity_AutomapperException_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null> StackTrace: at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type) at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock() at System.Reflection.Emit.TypeBuilder.CreateType() at AutoMapper.Impl.ProxyGenerator.CreateProxyType(Type interfaceType) at AutoMapper.Impl.ProxyGenerator.GetProxyType(Type interfaceType) at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.CreateObject(ResolutionContext context) at AutoMapper.Mappers.TypeMapObjectMapperRegistry.NewObjectPropertyMapMappingStrategy.GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper) at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) 
+10
interface automapper
source share
1 answer

When using the interface as a destination, AutoMapper will create a proxy type for you, but this only supports properties.

To get around this problem, you can tell AutoMapper how the target should be constructed using ConstructUsing when you map, so in your example above, your creation map would look like this:

 cfg.CreateMap<MyViewModel, IEntity>().ConstructUsing((ResolutionContext rc) => new MyEntity()); 

For reference, I found this from this SO article: stack overflow

+7
source share

All Articles