這是我的模型層次結構:Automapper接口有衝突的屬性ID參數名:interfaceType
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; }
}
,這些都是我的映射和測試:
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();
}
}
這裏是例外:
AutoMapper.AutoMapperMappingException:
映射類型:
NodeDto 1 -> INodeModel
1 NodeDto`1 [[System.Int32] - >
INodeModel`1 [[System.Int32]
消息:
該接口具有一個衝突屬性ID參數名稱:接口類型
堆棧:
at AutoMapper.Internal.ProxyGenerator。 CreateProxyType(Type interfaceType)
at AutoMapper.Internal.ProxyGenerator。 GetProxyType(Type interfaceType)
at AutoMapper.MappingEngine。 CreateObject(ResolutionContext上下文)