Tôi đang làm việc trên ứng dụng MVC sử dụng Inversion of Control và do đó sử dụng rộng rãi các loại giao diện, triển khai cụ thể được chèn bởi trình phân giải phụ thuộc theo yêu cầu. Các giao diện thực thể kế thừa từ một giao diện cơ sở mô tả một số tính năng quản lý cơ bản cho các thực thể. ViewModels cũng được sử dụng rộng rãi.Ánh xạ tự động tới một giao diện: Ngoại lệ "Phương thức không được triển khai"
Ứng dụng sử dụng Automapper và tôi đã tạo ánh xạ từ các kiểu xem cho các giao diện thực thể khác nhau. Cấu hình ánh xạ xác nhận chính xác. Tuy nhiên, khi tôi gọi Automapper để thực hiện ánh xạ, mã không thành công với số TypeLoadException
.
Tôi tin rằng Automapper có khả năng ánh xạ tới giao diện (xem this từ Jimmy Bogard).
Có vẻ như trình tạo proxy Automapper đã bỏ qua để thêm MyMethod() vào proxy và điều này gây ra một ngoại lệ khi Reflection cố gắng tạo kiểu.
Nếu không phải như vậy, làm cách nào để bản đồ này hoạt động? Tôi đã bỏ lỡ một cái gì đó hiển nhiên?
Dưới đây là một ứng dụng giao diện điều khiển đơn giản thể hiện kịch bản, và khả năng tái tạo các lỗi khi chạy:
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();
}
}
Trường hợp ngoại lệ ném là:
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)