Example #1
0
        /// <summary>
        /// Deep cloning of Student type instance
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            Student result = new Student();

            // Collects all values of all properties
            var queue = new Queue<object>();
            foreach (var prop in this.GetType().GetProperties())
            {
                if (prop.PropertyType == typeof(int))
                {
                    queue.Enqueue(prop.GetValue(this));
                }
                else
                {
                    queue.Enqueue((prop.GetValue(this) as string).Clone());
                }
            }

            // Save all data to the new object
            foreach (var prop in result.GetType().GetProperties())
            {
                prop.SetValue(result, queue.Dequeue());
            }

            return result;
        }