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