简体   繁体   English

如何使用自动映射器对此进行映射?

[英]How to map this using automapper?

I will be upfront by saying I just started using AutoMapper. 首先,我要开始使用AutoMapper。 I'm having some problems understanding if what I'm trying to do is incorrect. 我在理解我要执行的操作是否不正确时遇到一些问题。

I have an abstract class. 我有一个抽象类。

 public abstract class Animal
    {
        public int Age { get; set; }
    }

I have a class that derives from this abstract class 我有一个从这个抽象类派生的类

 public class Cat : Animal
    {
        public string Name { get; set; }

        public string Type { get; set; }
    }

I have a separate class that shares values that need to be mapped to the Cat class derived from the Animal abstract class. 我有一个单独的类,该类共享需要映射到从Animal抽象类派生的Cat类的值。

 public class ProblemClass
    {
        public string Name { get; set; }
    }

I have a mapper setup like so, 我有一个这样的映射器设置,

Mapper.CreateMap<ProblemClass, Animal>();

I've instantiated a ProblemClass item. 我已经实例化了一个ProblemClass项目。

var problemClass = new ProblemClass();
Mapper.Map<Animal>(problemClass);

How would I go about mapping something like this? 我该如何映射这样的东西? My main point of confusion is is automapper obviously cannot instantiate an abstract class, so I'm really confused how to create a generic Mapper for Animal which works with all kinds of animal sub classes derived from it. 我主要的困惑是automapper显然不能实例化一个抽象类,因此我真的很困惑如何为Animal创建一个通用的Mapper,该Mapper可以处理从其派生的各种动物子类。

A possible solution is to create mappings between your source type and all derived destination types automatically: 一种可能的解决方案是在源类型和所有派生目标类型之间自动创建映射:

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        public abstract class Animal
        {
            public int Age { get; set; }
        }

        public class Cat : Animal
        {
            public string Name { get; set; }

            public string Type { get; set; }
        }

        public class ProblemClass
        {
            public string Name { get; set; }
        }

        private static IList<Type> GetAllDerivedTypes(Type t)
        {
            var listOfBs = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
                            from assemblyType in domainAssembly.GetTypes()
                            where t.IsAssignableFrom(assemblyType)
                            select assemblyType);
            return listOfBs.ToList();
        }

        private static void MapAllTypes(Type srcType, Type destType)
        {
            var allDestTypes = GetAllDerivedTypes(destType);
            foreach (var destTypeDerived in allDestTypes)
            {
                Mapper.CreateMap(srcType, destTypeDerived);
            }
        }

        static void Main(string[] args)
        {
            MapAllTypes(typeof(ProblemClass), typeof(Animal));

            var problemClass = new ProblemClass() { Name = "test name" };
            Animal someAnimal = new Cat();

            // after this (someAnimal as Cat).Name will be "test name"
            Mapper.Map(problemClass, someAnimal);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM