2016-06-13 6 views
0

Das ist mein Modell-Hierarchie ist:AutoMapper Die Schnittstelle hat eine widersprüchliche Eigenschaft ID Parametername: Interface

public interface INodeModel<T> : INodeModel 
where T : struct 
{ 
    new T? ID { get; set; } 
} 

public interface INodeModel 
{ 
    object ID { get; set; } 
    string Name { get; set; } 
} 

public class NodeModel<T> : INodeModel<T> 
where T : struct 
{ 
    public T? ID { get; set; } 
    public string Name { get; set; } 

    object INodeModel.ID 
    { 
     get 
     { 
      return ID; 
     } 

     set 
     { 
      ID = value as T?; 
     } 
    } 
} 

public class NodeDto<T> where T : struct 
{ 
    public T? ID { get; set; } 
    public string Name { get; set; } 
} 

und das sind meine Mappings und Test:

class Program 
{ 
    private static MapperConfiguration _mapperConfiguration; 

    static void Main(string[] args) 
    { 
     _mapperConfiguration = new MapperConfiguration(cfg => 
     { 

      cfg.CreateMap(typeof(NodeDto<>), typeof(NodeModel<>)); 
      cfg.CreateMap(typeof(NodeDto<>), typeof(INodeModel<>)); 
      cfg.CreateMap(typeof(INodeModel<>), typeof(NodeModel<>)); 

     }); 

     var dto = new NodeDto<int> { ID = 1, Name = "Hi" }; 

     var obj = _mapperConfiguration.CreateMapper().Map<INodeModel<int>>(dto); 


     Console.Write(obj.ID); 
     Console.ReadLine(); 
    } 
} 

und hier ist die Ausnahme:

AutoMapper.AutoMapperMappingException:

Mapping-Typen:

NodeDto 1 -> INodeModel 1 NodeDto`1 [[System.Int32] ->

INodeModel`1 [[System.Int32]

Nachricht:

Die Schnittstelle hat eine widersprüchliche Eigenschaft ID Parametername: interfaceType

Stapel:

bei AutoMapper.Internal.ProxyGenerator. CreateProxyType (Typ interfaceType)

bei AutoMapper.Internal.ProxyGenerator. GetProxyType (Typ interfaceType)

bei AutoMapper.MappingEngine. CreateObject (ResolutionContext Kontext)

Antwort

0

AutoMapper ist beim Erstellen einer Proxy-Implementierung verwirrt, dass Sie zwei Mitglieder mit demselben Namen in Ihren Schnittstellen haben. Sie verwenden Shadowing, was noch schwieriger ist. Anstatt AutoMapper Sinn daraus machen annehmen kann, die viel Glück in ein neues Teammitglied zu erklären, ich stattdessen die Interface-Klasse Implementierung explizit machen würde:

cfg.CreateMap(typeof(NodeDto<>), typeof(NodeModel<>)); 
cfg.CreateMap(typeof(NodeDto<>), typeof(INodeModel<>)) 
    .ConvertUsing(typeof(NodeModelConverter<>)); 
cfg.CreateMap(typeof(INodeModel<>), typeof(NodeModel<>)); 

public class NodeModelConverter<T> : 
    ITypeConverter<NodeModel<T>, INodeModel<T>> where T : struct 
{ 
    public INodeModel<T> Convert(NodeModel<T> source, ResolutionContext context) 
     => new NodeModelImpl {ID = source.ID, Name = source.Name}; 

    private class NodeModelImpl : INodeModel<T> 
    { 
     public T? ID { get; set; } 

     public string Name { get; set; } 

     object INodeModel.ID 
     { 
      get { return ID; } 

      set { ID = (T?) value; } 
     } 
    } 
} 

Keine Magie und ganz ausdrücklich und klar!

Verwandte Themen