简体   繁体   English

AutoMapper对象集合未映射

[英]AutoMapper Object Collections not mapping

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? 我想知道您是否对此有任何想法?

EDIT: 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; }
}
}

Destination Class: 目的地类别:

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);
    }
}

I then create my person object from Namespace1 and create the mappings using the function above like so: 然后,我从Namespace1创建我的person对象,并使用上述函数创建映射,如下所示:

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

After that I call the map function like so: 之后,我像这样调用map函数:

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

This maps all the properties of the Person class just fine EXCEPT for the Items object array. 这将对Person类的所有属性进行映射,除了Items对象数组之外。 It transfers the objects accross but they still belong to the Namespace1 instead of the Namespace2 namespace. 它跨对象传输对象,但它们仍属于Namespace1而不是Namespace2命名空间。

Image of the problem from the watch window can be found here 从监视窗口问题的图片,可以发现这里

You can download the console app if you like here 您可以在此处下载控制台应用程序

Thanks for any help you can give. 谢谢你提供的所有帮助。 M 中号

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[]. 我知道这确实很老,但是我确实找到了答案(至少对于最新版本的AutoMapper(在此答案时为8.0)。您需要为Person编写一个自定义ITypeConverter ,以进行对象[]的映射。

In my case i had an object like this (xml attributes removed): 就我而言,我有一个像这样的对象(删除了xml属性):

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;
    }
}

I have tried the following and it seem working; 我已经尝试了以下方法,但似乎可行;

public class Tester
{
    public void Test()
    {
        AutoMapper.Mapper.CreateMap<FirstObject, SecondObject>();


        FirstObject t = new FirstObject();
        t.Items = new object[] { new Item() { Id = 1 }, new Item() { Id = 2 } };

        SecondObject result = AutoMapper.Mapper.Map<SecondObject>(t);
    }
}



public class FirstObject
{
    public object[] Items { get; set; }

}

public class SecondObject
{
    public object[] Items { get; set; }
}

public class Item
{
    public int Id { get; set; }
}

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

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