AutoMapper对象集合未映射 [英] AutoMapper Object Collections not mapping

查看:70
本文介绍了AutoMapper对象集合未映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试映射一个与我要映射到的类具有相同布局的类.除了我尝试映射对象集合时,一切都进行得很好.例如,当我尝试映射在源类中定义的此属性时:

I am trying to map a class which has an identical layout to the class I am trying to map to. All goes well except when I try to map Object collections. For example when I try to map this property defined in the source class:

[System.Xml.Serialization.XmlElementAttribute("trust", typeof(Trust))]
[System.Xml.Serialization.XmlElementAttribute("valuation", typeof(Valuation))]
[System.Xml.Serialization.XmlElementAttribute("waiver_of_premium_ind", typeof(YesNo))]
[System.Xml.Serialization.XmlElementAttribute("written_under_trust_ind", typeof(YesNo), IsNullable = true)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public object[] Items
{
    get { return this.itemsField; }
    set { this.itemsField = value; }
}

我发现它不映射但与源对象保留在相同的名称空间中,即使它是目标对象中的集合.

I find that it does not map but remains in the same namespace as the source object even though it is a collection in the destination object.

我想知道您对此事有什么想法吗?

I wonder if you have any ideas on this matter?

通过示例获取更多信息-源类:

More information by way of an example - source class:

namespace Namespace1
{
public class Person
{
    public int PersonID { get; set; }
    public List<Arm> Arms { get; set; }

    [System.Xml.Serialization.XmlElementAttribute("_arms", typeof(Arm))]
    [System.Xml.Serialization.XmlElementAttribute("_hand", typeof(Hand))]
    public object[] Items { get; set; }
}

public class Arm
{
    public Hand Hand { get; set; }
}

public class Hand
{
    public int HandID { get; set; }
    public string HandSide { get; set; }
    public List<Fingers> Fingers { get; set; }
}

public class Fingers
{
    public int FingerNumber { get; set; }
}
}

目的地类别:

namespace Namespace2
{
public class Person
{
    public int PersonID { get; set; }
    public List<Arm> Arms { get; set; }

    [System.Xml.Serialization.XmlElementAttribute("_arms", typeof(Arm))]
    [System.Xml.Serialization.XmlElementAttribute("_hand", typeof(Hand))]
    public object[] Items { get; set; }
}

public class Arm
{
    public Hand Hand { get; set; }
}

public class Hand
{
    public int HandID { get; set; }
    public string HandSide { get; set; }
    public List<Fingers> Fingers { get; set; }
}

public class Fingers
{
    public int FingerNumber { get; set; }
}
}

用于映射这两种类型以及两个命名空间中所有嵌套类型的代码:

Code to map the two types and all the nested types within the two namespaces:

public static void CreateMappings(string nsFrom, string nsTo, Type typeFrom)
{
    Assembly assembly = Assembly.GetAssembly(typeFrom);
    var TypesInNamespace = assembly.GetTypes().Where(type => type.Namespace == nsFrom);
    foreach (var sourceType in TypesInNamespace)
    {
        Type destinationType = Type.GetType(sourceType.FullName.Replace(nsFrom, nsTo));
        Mapper.CreateMap(sourceType, destinationType);
    }
}

然后我从Namespace1创建我的person对象,并使用上面的函数创建映射,如下所示:

I then create my person object from Namespace1 and create the mappings using the function above like so:

CreateMappings("Namespace1", "Namespace2", typeof(Namespace1.Person));

之后,我像这样调用map函数:

After that I call the map function like so:

var result = Mapper.Map<Namespace2.Person>(person);

这将对Person类的所有属性进行映射,只是对Items对象数组的精确度除外.它跨对象传输对象,但它们仍属于Namespace1而不是Namespace2命名空间.

This maps all the properties of the Person class just fine EXCEPT for the Items object array. It transfers the objects accross but they still belong to the Namespace1 instead of the Namespace2 namespace.

可以在此处

如果您喜欢此处

感谢您提供的任何帮助.M

Thanks for any help you can give. M

推荐答案

我知道它确实很旧,但是我确实找到了答案(至少对于最新版本的AutoMapper(在此答案时为8.0).为Person写一个自定义的 ITypeConverter 来完成对象的映射[].

I know this is really old but I did find the answer (at least for the latest version of AutoMapper (8.0 at time of this answer). You need to write a custom ITypeConverter for Person that does the mapping of the object[].

在我的情况下,我有一个像这样的对象(删除了xml属性):

In my case i had an object like this (xml attributes removed):

namespace bob {
    public class sally 
    {
        public bob.MyEnum[] SomeVarName {get;set;}
        public object[] SomeValueVar {get;set;}
    }
}

namespace martin {
    public class sally 
    {
        public martin.MyEnum[] SomeVarName {get;set;}
        public object[] SomeValueVar {get;set;}
    }
}

自定义类型转换器如下所示:

The custom type converter looked like this:

public class LocationTypeResolver : ITypeConverter<bob.sally,martin.sally>
{
    public martin.sally Convert(bob.sally source, martin.sally destination, ResolutionContext context)
    {
        var retVal = new martin.sally
                     {
                         SomeValueVar = new object[source.SomeVarName.Length],
                         SomeVarName  = new martin.MyEnum[source.SomeVarName.Length]
                     };

        for (int i = 0; i < source.Items.Length; i++)
        {
            retVal.SomeVarName[i] = (martin.MyEnum)Enum.Parse(typeof(martin.MyEnum), source.SomeVarName[i].ToString());

            switch (source.ItemsElementName[i])
            {
                //map any custom types
                default:
                    retVal.SomeValueVar[i] = source.SomeValueVar[i]
            }
        }

        return retVal;
    }
}

这篇关于AutoMapper对象集合未映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆