Cloning objects without Serialization

前端 未结 3 455
傲寒
傲寒 2020-12-02 02:21

I\'ve found numerous solutions here at SO and elsewere that deal with deep clone of object via serialization/deserialization (into memory and back).

It requires that

3条回答
  •  被撕碎了的回忆
    2020-12-02 02:36

    Here's how to use it:

    var oldList = new List();
    var newList = oldList.Clone();
    

    using these Methods:

    public static T Clone(this T o) where T : new()
    {
        return (T)CloneObject(o);
    }
    static object CloneObject(object obj)
    {
        if (ReferenceEquals(obj, null)) return null;
    
        var type = obj.GetType();
        if (type.IsValueType || type == typeof(string))
            return obj;
        else if (type.IsArray)
        {
            var array = obj as Array;
            var arrayType = Type.GetType(type.FullName.Replace("[]", string.Empty));
            var arrayInstance = Array.CreateInstance(arrayType, array.Length);
    
            for (int i = 0; i < array.Length; i++)
                arrayInstance.SetValue(CloneObject(array.GetValue(i)), i);
            return Convert.ChangeType(arrayInstance, type);
        }
        else if (type.IsClass)
        {
            var instance = Activator.CreateInstance(type);
            var fields = type.GetFields(BindingFlags.Public |
                        BindingFlags.NonPublic | BindingFlags.Instance);
    
            foreach (var field in fields)
            {
                var fieldValue = field.GetValue(obj);
                if (ReferenceEquals(fieldValue, null)) continue;
                field.SetValue(instance, CloneObject(fieldValue));
            }
            return instance;
        }
        else
            return null;
    }
    

提交回复
热议问题