AutoMapper Object Collections not mapping

2019-07-16 08:54发布

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:

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

After that I call the map function like so:

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

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.

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

标签: c# AutoMapper
2条回答
叛逆
2楼-- · 2019-07-16 09:47

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; }
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-16 09:48

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[].

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;
    }
}
查看更多
登录 后发表回答