/// <summary> /// Clones an object for the following fields: /// - public fields only /// /// Does not include the following: /// - static fields /// - collection fields (array, list, ...etc) /// - properties /// - private or protected fields /// </summary> /// <param name="input"></param> /// <returns></returns> static public Object Clone(object input) { if (input == null) { return(null); } var inputType = input.GetType(); //Console.WriteLine($"DeepClone.Clone() = input={input}, Type={inputType}"); // if primitve value type, return if (inputType.IsValueType && inputType.IsPrimitive) { return(CloneValueType(input, inputType)); } var deepcloner = new DeepClone(); return(deepcloner.CloneReferenceType_Iterative(input, inputType)); // *** do not use *** //the recursive implementation throws stackoverflow exception when the list item counts are > 54000 or so //return deepcloner.CloneReferenceType(input, inputType); }
static void CloneListTest(LinkedListItem input) { var result = (LinkedListItem)DeepClone.Clone(input); Console.WriteLine($"result = {result}"); Console.WriteLine($"is clone NOT referenceEqual() to input? = {!object.ReferenceEquals(input, result)}"); Console.WriteLine(); }