private void MapMember(MemberInfo member, MappingMethods mappingMethods, object value)
{
Type typeOfMember;
#region PropertyInfo
if (member.MemberType == MemberTypes.Property)
{
typeOfMember = (member as PropertyInfo).PropertyType;
if (mappingMethods.HasMethodForType(typeOfMember))
{
MemberMapping methodToCall = mappingMethods.GetMappingForType(typeOfMember);
methodToCall(member as PropertyInfo, value);
}
else if (mappingMethods.BreakdownUnknownTypes == false)
{
throw new ArgumentException("ObjectMapper found a type to map that it cannot handle," +
" fix this by adding a Mapping to your MappingMethods class");
}
else if (typeOfMember.IsPrimitive == false)
{
if (value == null)
{
value = System.Activator.CreateInstance((member as PropertyInfo).PropertyType);
}
this.MapObject(value, mappingMethods, typeOfMember);
}
}
#endregion
#region FieldInfo
else if (member.MemberType == MemberTypes.Field)
{
typeOfMember = (member as FieldInfo).FieldType;
if (mappingMethods.HasMethodForType(typeOfMember))
{
MemberMapping methodToCall = mappingMethods.GetMappingForType(typeOfMember);
methodToCall(member as FieldInfo, value);
}
else if (mappingMethods.BreakdownUnknownTypes == false)
{
throw new ArgumentException("ObjectMapper found a type to map that it cannot handle," +
" fix this by adding a Mapping to your MappingMethods class");
}
else if (typeOfMember.IsPrimitive == false)
{
if (value == null)
{
value = System.Activator.CreateInstance((member as FieldInfo).FieldType);
}
this.MapObject(value, mappingMethods, typeOfMember);
}
}
#endregion
else //Not a Property or Field, ERROR! (this should never happen)
{
throw new Exception("MemberType not accepted, MapObject only accepts Fields and Properties.");
}
}