/// <summary> /// Clone a person object by manually typing the copy statements. /// </summary> /// <param name="p">Object to clone</param> /// <returns>Cloned object</returns> private static ILPerson CloneNormal(ILPerson p) { ILPerson newPerson = new ILPerson(); newPerson.ID = p.ID; newPerson.Name = p.Name; newPerson.FirstName = p.FirstName; return(newPerson); }
/// <summary> /// Create a list of persons with random data and a given number of items. /// </summary> /// <param name="count">Number of persons to generate</param> /// <returns>List of generated persons</returns> private static List <ILPerson> CreatePersonsList(int count) { Random r = new Random(Environment.TickCount); List <ILPerson> persons = new List <ILPerson>(count); for (int i = 0; i < count; i++) { ILPerson p = new ILPerson(); p.ID = r.Next(); p.Name = string.Concat("Slaets_", r.Next()); p.FirstName = string.Concat("Filip_", r.Next()); persons.Add(p); } return(persons); }
/// <summary> /// Clone one person object with reflection /// </summary> /// <param name="p">Person to clone</param> /// <returns>Cloned person</returns> private static ILPerson CloneObjectWithReflection(ILPerson p) { // Get all the fields of the type, also the privates. FieldInfo[] fis = p.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic); // Create a new person object ILPerson newPerson = new ILPerson(); // Loop through all the fields and copy the information from the parameter class // to the newPerson class. foreach (FieldInfo fi in fis) { fi.SetValue(newPerson, fi.GetValue(p)); } // Return the cloned object. return(newPerson); }