Automapper映射到接口:"TypeLoadException"异常

13

我正在开发一个使用控制反转的MVC应用程序,因此广泛使用接口类型,具体实现将根据需要由依赖解析器注入。实体接口继承自描述实体基本管理功能的基本接口,ViewModel也广泛使用。

该应用程序使用Automapper,并且我已经创建了从视图模型到各种实体接口的映射。映射配置正确验证。但是,当我要求Automapper执行映射时,代码失败并引发TypeLoadException异常。

我相信Automapper能够映射到接口(请参见Jimmy Bogard的这篇文章)。

看起来可能是Automapper代理生成器省略了将MyMethod()添加到代理中,这导致Reflection在创建类型时引发异常。

如果不是这种情况,我该如何使此映射工作?我错过了什么明显的东西吗?

这是一个简化的控制台应用程序,演示了场景,并在运行时重新生成错误:

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();
    }
}

抛出的异常是:

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)

可能是[automapper,映射到接口]的重复问题(https://dev59.com/-XjZa4cB1Zd3GeqPaSET)。 - FlyingFoX
1个回答

11
当使用接口作为目标类型时,AutoMapper将为您创建一个代理类型,但这仅支持属性。为了解决此问题,您可以在映射上使用ConstructUsing来告诉AutoMapper如何构造目标对象,因此在上面的示例中,您的创建映射应如下所示...
cfg.CreateMap<MyViewModel, IEntity>().ConstructUsing((ResolutionContext rc) => new MyEntity());

供参考,我从这篇 Stack Overflow 文章中找到了这个答案:https://dev59.com/anPYa4cB1Zd3GeqPk5vA#17244307


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接