| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 | using System;using System.Collections;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace MECF.Framework.Common.Utilities{    public sealed class CloneUtil    {        public static bool CheckIsChanged<T>(T OldValue, T NewValue)        {            Type typeSource = OldValue.GetType();            PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);            Type typeNewSource = NewValue.GetType();            PropertyInfo[] newPropertyInfo = typeNewSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);            foreach (PropertyInfo property in propertyInfo)            {                //Check whether property can be written to                if (!property.CanWrite) continue;                if (property.Name == "IsNotifying") continue;                //check whether property type is value type, enum or string type                if (property.PropertyType.IsPrimitive || property.PropertyType.IsEnum)                {                    var newProperty = newPropertyInfo.Where(x => x.Name == property.Name).FirstOrDefault();                    if (property.GetValue(OldValue, null) != newProperty.GetValue(NewValue, null))                    {                        return false;                    }                }                else if (property.PropertyType == typeof(string))                {                    var oldPropertyValue = property.GetValue(OldValue, null) == null ? "" : property.GetValue(OldValue, null);                    var newProperty = newPropertyInfo.Where(x => x.Name == property.Name).FirstOrDefault();                    var newPropertyValue = newProperty.GetValue(NewValue, null) == null ? "" : newProperty.GetValue(NewValue, null);                    if (oldPropertyValue != newPropertyValue)                    {                        return false;                    }                }            }            return true;        }        public static object CloneObject(object objSource)        {            //Get the type of source object and create a new instance of that type            Type typeSource = objSource.GetType();            object objTarget = Activator.CreateInstance(typeSource);            //Get all the properties of source object type            PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);            //Assign all source property to taget object 's properties            foreach (PropertyInfo property in propertyInfo)            {                //Check whether property can be written to                if (!property.CanWrite) continue;                if (property.Name == "StepNo") continue;                //check whether property type is value type, enum or string type                if (property.PropertyType.IsPrimitive || property.PropertyType.IsEnum || property.PropertyType == typeof(string))                {                    property.SetValue(objTarget, property.GetValue(objSource, null), null);                }                else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))                {                    //Include List and Dictionary and......                    if (property.PropertyType.IsGenericType)                    {                        var cloneObj = Activator.CreateInstance(property.PropertyType);                        var addMethod = property.PropertyType.GetMethod("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);                        Debug.Assert(null != addMethod);                        var currentValues = property.GetValue(objSource, null) as IEnumerable;                        if (currentValues == null)                        {                            property.SetValue(objTarget, null, null);                        }                        else                        {                            if (cloneObj is IDictionary)                            {                                cloneObj = cloneObj as IDictionary;                                foreach (var currentValue in currentValues)                                {                                    var propInfoKey = currentValue.GetType().GetProperty("Key");                                    var propInfoValue = currentValue.GetType().GetProperty("Value");                                    if (propInfoKey != null && propInfoValue != null)                                    {                                        var keyValue = propInfoKey.GetValue(currentValue, null);                                        var valueValue = propInfoValue.GetValue(currentValue, null);                                        object finalKeyValue, finalValueValue;                                        //Get finalKeyValue                                        var currentKeyType = keyValue.GetType();                                        if (currentKeyType.IsPrimitive || currentKeyType.IsEnum || currentKeyType == typeof(string))                                        {                                            finalKeyValue = keyValue;                                        }                                        else                                        {                                            //Reference type                                            var copyObj = CloneObject(keyValue);                                            finalKeyValue = copyObj;                                        }                                        //Get finalValueValue                                        var currentValueType = valueValue.GetType();                                        if (currentValueType.IsPrimitive || currentValueType.IsEnum || currentValueType == typeof(string))                                        {                                            finalValueValue = valueValue;                                        }                                        else                                        {                                            //Reference type                                            var copyObj = CloneObject(valueValue);                                            finalValueValue = copyObj;                                        }                                        addMethod.Invoke(cloneObj, new[] { finalKeyValue, finalValueValue });                                    }                                }                                property.SetValue(objTarget, cloneObj, null);                            }                            //Common IList type                            else                            {                                foreach (var currentValue in currentValues)                                {                                    var currentType = currentValue.GetType();                                    if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))                                    {                                        addMethod.Invoke(cloneObj, new[] { currentValue });                                    }                                    else                                    {                                        //Reference type                                        var copyObj = CloneObject(currentValue);                                        addMethod.Invoke(cloneObj, new[] { copyObj });                                    }                                }                                property.SetValue(objTarget, cloneObj, null);                            }                        }                    }                    //Array type                    else                    {                        var currentValues = property.GetValue(objSource, null) as Array;                        if (null == currentValues)                        {                            property.SetValue(objTarget, null, null);                        }                        else                        {                            var cloneObj = Activator.CreateInstance(property.PropertyType, currentValues.Length) as Array;                            var arrayList = new ArrayList();                            for (var i = 0; i < currentValues.Length; i++)                            {                                var currentType = currentValues.GetValue(i).GetType();                                if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))                                {                                    arrayList.Add(currentValues.GetValue(i));                                }                                else                                {                                    //Reference type                                    var copyObj = CloneObject(currentValues.GetValue(i));                                    arrayList.Add(copyObj);                                }                            }                            arrayList.CopyTo(cloneObj, 0);                            property.SetValue(objTarget, cloneObj, null);                        }                    }                }                //else property type is object/complex types, so need to recursively call this method until the end of the tree is reached                else                {                    object objPropertyValue = property.GetValue(objSource, null);                    if (objPropertyValue == null)                    {                        property.SetValue(objTarget, null, null);                    }                    else if (objPropertyValue.GetType().IsPrimitive || objPropertyValue.GetType().IsEnum || objPropertyValue.GetType() == typeof(string))                    {                        property.SetValue(objTarget, objPropertyValue, null);                    }                    else                    {                        property.SetValue(objTarget, CloneObject(objPropertyValue), null);                    }                }            }            return objTarget;        }        public static object CloneObjectSetFlag(object objSource, string flag, bool setValue)        {            //Get the type of source object and create a new instance of that type            Type typeSource = objSource.GetType();            object objTarget = Activator.CreateInstance(typeSource);            //Get all the properties of source object type            PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);            //Assign all source property to taget object 's properties            foreach (PropertyInfo property in propertyInfo)            {                //Check whether property can be written to                if (!property.CanWrite) continue;                if (property.Name == "StepNo") continue;                //check whether property type is value type, enum or string type                if (property.PropertyType.IsPrimitive || property.PropertyType.IsEnum || property.PropertyType == typeof(string))                {                    if (property.Name.Contains(flag))                    { }                    else                    {                        property.SetValue(objTarget, property.GetValue(objSource, null), null);                    }                }                else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))                {                    //Include List and Dictionary and......                    if (property.PropertyType.IsGenericType)                    {                        var cloneObj = Activator.CreateInstance(property.PropertyType);                        var addMethod = property.PropertyType.GetMethod("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);                        Debug.Assert(null != addMethod);                        var currentValues = property.GetValue(objSource, null) as IEnumerable;                        if (currentValues == null)                        {                            property.SetValue(objTarget, null, null);                        }                        else                        {                            if (cloneObj is IDictionary)                            {                                cloneObj = cloneObj as IDictionary;                                foreach (var currentValue in currentValues)                                {                                    var propInfoKey = currentValue.GetType().GetProperty("Key");                                    var propInfoValue = currentValue.GetType().GetProperty("Value");                                    if (propInfoKey != null && propInfoValue != null)                                    {                                        var keyValue = propInfoKey.GetValue(currentValue, null);                                        var valueValue = propInfoValue.GetValue(currentValue, null);                                        object finalKeyValue, finalValueValue;                                        //Get finalKeyValue                                        var currentKeyType = keyValue.GetType();                                        if (currentKeyType.IsPrimitive || currentKeyType.IsEnum || currentKeyType == typeof(string))                                        {                                            finalKeyValue = keyValue;                                        }                                        else                                        {                                            //Reference type                                            var copyObj = CloneObjectSetFlag(keyValue, flag, setValue);                                            finalKeyValue = copyObj;                                        }                                        //Get finalValueValue                                        var currentValueType = valueValue.GetType();                                        if (currentValueType.IsPrimitive || currentValueType.IsEnum || currentValueType == typeof(string))                                        {                                            finalValueValue = valueValue;                                        }                                        else                                        {                                            //Reference type                                            var copyObj = CloneObjectSetFlag(valueValue, flag, setValue);                                            finalValueValue = copyObj;                                        }                                        addMethod.Invoke(cloneObj, new[] { finalKeyValue, finalValueValue });                                    }                                }                                property.SetValue(objTarget, cloneObj, null);                            }                            //Common IList type                            else                            {                                foreach (var currentValue in currentValues)                                {                                    var currentType = currentValue.GetType();                                    if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))                                    {                                        addMethod.Invoke(cloneObj, new[] { currentValue });                                    }                                    else                                    {                                        //Reference type                                        var copyObj = CloneObjectSetFlag(currentValue, flag, setValue);                                        addMethod.Invoke(cloneObj, new[] { copyObj });                                    }                                }                                property.SetValue(objTarget, cloneObj, null);                            }                        }                    }                    //Array type                    else                    {                        var currentValues = property.GetValue(objSource, null) as Array;                        if (null == currentValues)                        {                            property.SetValue(objTarget, null, null);                        }                        else                        {                            var cloneObj = Activator.CreateInstance(property.PropertyType, currentValues.Length) as Array;                            var arrayList = new ArrayList();                            for (var i = 0; i < currentValues.Length; i++)                            {                                var currentType = currentValues.GetValue(i).GetType();                                if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))                                {                                    arrayList.Add(currentValues.GetValue(i));                                }                                else                                {                                    //Reference type                                    var copyObj = CloneObjectSetFlag(currentValues.GetValue(i), flag, setValue);                                    arrayList.Add(copyObj);                                }                            }                            arrayList.CopyTo(cloneObj, 0);                            property.SetValue(objTarget, cloneObj, null);                        }                    }                }                //else property type is object/complex types, so need to recursively call this method until the end of the tree is reached                else                {                    object objPropertyValue = property.GetValue(objSource, null);                    if (objPropertyValue == null)                    {                        property.SetValue(objTarget, null, null);                    }                    else if (objPropertyValue.GetType().IsPrimitive || objPropertyValue.GetType().IsEnum || objPropertyValue.GetType() == typeof(string))                    {                        property.SetValue(objTarget, objPropertyValue, null);                    }                    else                    {                        property.SetValue(objTarget, CloneObjectSetFlag(objPropertyValue, flag, setValue), null);                    }                }            }            return objTarget;        }        public static object CloneAllObject(object objSource)        {            //Get the type of source object and create a new instance of that type            Type typeSource = objSource.GetType();            object objTarget = Activator.CreateInstance(typeSource);            //Get all the properties of source object type            PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);            //Assign all source property to taget object 's properties            foreach (PropertyInfo property in propertyInfo)            {                //Check whether property can be written to                if (!property.CanWrite) continue;                //check whether property type is value type, enum or string type                if (property.PropertyType.IsPrimitive || property.PropertyType.IsEnum || property.PropertyType == typeof(string))                {                    property.SetValue(objTarget, property.GetValue(objSource, null), null);                }                else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))                {                    //Include List and Dictionary and......                    if (property.PropertyType.IsGenericType)                    {                        var cloneObj = Activator.CreateInstance(property.PropertyType);                        var addMethod = property.PropertyType.GetMethod("Add", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);                        Debug.Assert(null != addMethod);                        var currentValues = property.GetValue(objSource, null) as IEnumerable;                        if (currentValues == null)                        {                            property.SetValue(objTarget, null, null);                        }                        else                        {                            if (cloneObj is IDictionary)                            {                                cloneObj = cloneObj as IDictionary;                                foreach (var currentValue in currentValues)                                {                                    var propInfoKey = currentValue.GetType().GetProperty("Key");                                    var propInfoValue = currentValue.GetType().GetProperty("Value");                                    if (propInfoKey != null && propInfoValue != null)                                    {                                        var keyValue = propInfoKey.GetValue(currentValue, null);                                        var valueValue = propInfoValue.GetValue(currentValue, null);                                        object finalKeyValue, finalValueValue;                                        //Get finalKeyValue                                        var currentKeyType = keyValue.GetType();                                        if (currentKeyType.IsPrimitive || currentKeyType.IsEnum || currentKeyType == typeof(string))                                        {                                            finalKeyValue = keyValue;                                        }                                        else                                        {                                            //Reference type                                            var copyObj = CloneObject(keyValue);                                            finalKeyValue = copyObj;                                        }                                        //Get finalValueValue                                        var currentValueType = valueValue.GetType();                                        if (currentValueType.IsPrimitive || currentValueType.IsEnum || currentValueType == typeof(string))                                        {                                            finalValueValue = valueValue;                                        }                                        else                                        {                                            //Reference type                                            var copyObj = CloneObject(valueValue);                                            finalValueValue = copyObj;                                        }                                        addMethod.Invoke(cloneObj, new[] { finalKeyValue, finalValueValue });                                    }                                }                                property.SetValue(objTarget, cloneObj, null);                            }                            //Common IList type                            else                            {                                foreach (var currentValue in currentValues)                                {                                    var currentType = currentValue.GetType();                                    if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))                                    {                                        addMethod.Invoke(cloneObj, new[] { currentValue });                                    }                                    else                                    {                                        //Reference type                                        var copyObj = CloneObject(currentValue);                                        addMethod.Invoke(cloneObj, new[] { copyObj });                                    }                                }                                property.SetValue(objTarget, cloneObj, null);                            }                        }                    }                    //Array type                    else                    {                        var currentValues = property.GetValue(objSource, null) as Array;                        if (null == currentValues)                        {                            property.SetValue(objTarget, null, null);                        }                        else                        {                            var cloneObj = Activator.CreateInstance(property.PropertyType, currentValues.Length) as Array;                            var arrayList = new ArrayList();                            for (var i = 0; i < currentValues.Length; i++)                            {                                var currentType = currentValues.GetValue(i).GetType();                                if (currentType.IsPrimitive || currentType.IsEnum || currentType == typeof(string))                                {                                    arrayList.Add(currentValues.GetValue(i));                                }                                else                                {                                    //Reference type                                    var copyObj = CloneObject(currentValues.GetValue(i));                                    arrayList.Add(copyObj);                                }                            }                            arrayList.CopyTo(cloneObj, 0);                            property.SetValue(objTarget, cloneObj, null);                        }                    }                }                //else property type is object/complex types, so need to recursively call this method until the end of the tree is reached                else                {                    object objPropertyValue = property.GetValue(objSource, null);                    if (objPropertyValue == null)                    {                        property.SetValue(objTarget, null, null);                    }                    else if (objPropertyValue.GetType().IsPrimitive || objPropertyValue.GetType().IsEnum || objPropertyValue.GetType() == typeof(string))                    {                        property.SetValue(objTarget, objPropertyValue, null);                    }                    else                    {                        property.SetValue(objTarget, CloneObject(objPropertyValue), null);                    }                }            }            return objTarget;        }    }}
 |