Last active
December 22, 2015 01:59
Convert Persons to Humans and Humans To Persons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static dynamic PeopleConverter(dynamic sourceobject) | |
{ | |
if (sourceobject.GetType().Name=="Person") | |
{ | |
return ConvertFromPersonToHuman(sourceobject); | |
} | |
if (sourceobject.GetType().Name =="Human") | |
{ | |
return ConvertFromHumanToPerson(sourceobject); | |
} | |
throw new Exception("Bang! Pow! You submitted the wrong type!"); | |
} | |
private static Human ConvertFromPersonToHuman(Person person) | |
{ | |
Human result = new Human(); | |
string[] parts = person.Name.Split(' '); | |
result.BirthDay = person.DOB.ToShortDateString(); | |
result.FirstName = parts[0]; | |
result.LastName = parts[1]; | |
result.HeightInInches =(short)((Double) person.HeightInCM * 0.393701); | |
return result; | |
} | |
private static Person ConvertFromHumanToPerson(Human human) | |
{ | |
Person result = new Person(); | |
result.DOB = DateTime.Parse(human.BirthDay); | |
result.HeightInCM =(int)((Double)human.HeightInInches * 2.54); | |
result.Name = string.Format("{0} {1}", human.FirstName, human.LastName); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment