由于向下转换错误,无法使用继承进行映射

时间:2018-10-15 12:22:41

标签: c# automapper

我正在尝试使用以下配置和结果将类型为NamespaceB.SearchResult的对象转换为NamespaceA.SearchResult(结构完全相同,只是名称空间不同):

源/目的地类型

只需确保您复制AutoMapper_Test.NamespaceA并将其重命名为AutoMapper_Test.NamespaceB

namespace AutoMapper_Test.NamespaceA
{
    //Types
    class SearchResult
    {
        public SearchItem[] SearchItems { get; set; }
    }

    class SearchItem
    {
        public BaseProfile Profile { get; set; }

        public DerivedProfile[] SubProfiles { get; set; }
    }

    //Property Types
    class BaseProfile
    {
        public string Id { get; set; }
    }

    class DerivedProfile : BaseProfile 
    {
        public string Name { get; set; }
    }
}

public static SearchResult CreateData()
    {
        var profile = new NamespaceB.BaseProfile { Id = "5" };
        var derivedProfile = new List<NamespaceB.DerivedProfile>();
        derivedProfile.Add(new NamespaceB.DerivedProfile { Name = "Test", Id = "4" });

        var profile1 = new NamespaceB.BaseProfile { Id = "6" };
        var derivedProfile1 = new List<NamespaceB.DerivedProfile>();
        derivedProfile1.Add(new NamespaceB.DerivedProfile { Name = "Test", Id = "7" });

        var item = new NamespaceB.SearchItem
        { 
            Profile = profile,
            SubProfiles = derivedProfile.ToArray()
        };

        var item1 = new NamespaceB.SearchItem
        { 
            Profile = profile1,
            SubProfiles = derivedProfile1.ToArray()
        };

        var searchItems = new List<NamespaceB.SearchItem>();
        searchItems.Add(item);
        searchItems.Add(item1);

        return new NamespaceB.SearchResult { SearchItems = searchItems.ToArray() };
    }

    public static void ConfigureMappings()
    {
        Mapper.Initialize(cfg => {
            cfg.CreateMissingTypeMaps = false;
            cfg.CreateMap<NamespaceA.SearchResult, NamespaceB.SearchResult>();
        });
    }

//Main
static void Main(string[] args)
{
    ConfigureMappings();

    var searchResultBeta = CreateData(); //namespaceB
    var searchResult = Mapper.Map<NamespaceA.SearchResult>(searchResultBeta);
    System.Console.WriteLine(searchResult.SearchItems.Length);

    Console.ReadLine();
}

映射配置

public static void ConfigureMappings()
{
    var mappings = new MapperConfigurationExpression();
    mappings.CreateMap<NamespaceA.SearchResult, NamespaceB.SearchResult>();
    Mapper.Initialize(mappings);
}

自动映射器版本:7.0.1

异常详细信息

Unhandled Exception: AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
SearchResult -> SearchResult
AutoMapper_Test.NamespaceB.SearchResult -> AutoMapper_Test.NamespaceA.SearchResult

Type Map configuration:
SearchResult -> SearchResult
AutoMapper_Test.NamespaceB.SearchResult -> AutoMapper_Test.NamespaceA.SearchResult

Property:
SearchItems ---> AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
SearchItem -> SearchItem
AutoMapper_Test.NamespaceB.SearchItem -> AutoMapper_Test.NamespaceA.SearchItem

Type Map configuration:
SearchItem -> SearchItem
AutoMapper_Test.NamespaceB.SearchItem -> AutoMapper_Test.NamespaceA.SearchItem

Property:
SubProfiles ---> System.InvalidCastException: Unable to cast object of type 'AutoMapper_Test.NamespaceA.BaseProfile' to type 'AutoMapper_Test.NamespaceA.DerivedProfile'.
   at lambda_method(Closure , SearchResult , SearchResult , ResolutionContext )
   --- End of inner exception stack trace ---
   at lambda_method(Closure , SearchResult , SearchResult , ResolutionContext )
   --- End of inner exception stack trace ---
   at lambda_method(Closure , SearchResult , SearchResult , ResolutionContext )
   at lambda_method(Closure , Object , Object , ResolutionContext )
   at AutoMapper.Mapper.AutoMapper.IMapper.Map[TDestination](Object source) in C:\projects\automapper\src\AutoMapper\Mapper.cs:line 207
   at AutoMapper_Test.Program.Main(String[] args) in D:\Projects\AutoMapper Test\Program.cs:line 55

0 个答案:

没有答案