示例#1
0
        /// <summary>
        ///     Iterate through all array elements executing a given action for each array item
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="action">The action.</param>
        public static void ForEach(this Array array, Action <Array, int[]> action)
        {
            if (array.LongLength == 0)
            {
                return;
            }
            var walker = new ArrayTraverse(array);

            do
            {
                action(array, walker.Position);
            } while (walker.Step());
        }
示例#2
0
            private object ExecuteClone(object originalObject, Dictionary <object, object> visited, bool checkObjectGraph)
            {
                if (originalObject == null)
                {
                    return(null);
                }

                Type typeToReflect = originalObject.GetType();

                if (typeToReflect.IsPrimitive())
                {
                    return(originalObject);
                }

                if (checkObjectGraph && visited.ContainsKey(originalObject))
                {
                    return(visited[originalObject]);
                }

                if (originalObject is Delegate)
                {
                    return(null);
                }

                object cloneObject;

                if (typeToReflect.IsArray)
                {
                    Type arrayType     = typeToReflect.GetElementType();
                    var  originalArray = (Array)originalObject;
                    var  clonedArray   = Array.CreateInstance(arrayType, originalArray.Length);
                    cloneObject = clonedArray;

                    if (checkObjectGraph)
                    {
                        visited.Add(originalObject, cloneObject);
                    }

                    if (arrayType.IsPrimitive())
                    {
                        // ignore array of primitive, shallow copy will suffice
                    }
                    else if (!typeToReflect.IsPrimitive())
                    {
                        if (clonedArray.LongLength != 0)
                        {
                            var walker = new ArrayTraverse(clonedArray);

                            do
                            {
                                clonedArray.SetValue(
                                    ExecuteClone(originalArray.GetValue(walker.Position), visited, !arrayType.IsValueType),
                                    walker.Position);
                            }while (walker.Step());
                        }
                    }
                    else
                    {
                        Array.Copy(originalArray, clonedArray, clonedArray.Length);
                    }
                }
                else
                {
                    cloneObject = originalObject.MemberwiseClone();

                    if (checkObjectGraph)
                    {
                        visited.Add(originalObject, cloneObject);
                    }
                }

                CopyFields(originalObject, visited, cloneObject, typeToReflect);
                RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect);
                return(cloneObject);
            }