私は、制御の反転を使用する MVC アプリケーションに取り組んでおり、その結果、必要に応じて依存関係リゾルバーによって具体的な実装が注入され、インターフェイスの型を広範囲に使用します。エンティティ インターフェイスは、エンティティのいくつかの基本的な管理機能を記述する基本インターフェイスから継承します。ViewModel も広く使用されています。
このアプリは Automapper を使用しており、ビュー モデルからさまざまなエンティティ インターフェイスへのマッピングを作成しました。マッピング構成が正しく検証されます。ただし、Automapper を呼び出してマッピングを実行すると、コードはTypeLoadException
.
Automapper はインターフェイスにマッピングできると思います ( Jimmy Bogardのthisを参照してください)。
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)