Ejemplo n.º 1
0
        internal static T[,] Clone2DimArrayInternal <T>(T[,] objFrom, T[,] objTo, DeepCloneState state, bool isDeep)
        {
            if (objFrom == null || objTo == null)
            {
                return(null);
            }
            int num  = Math.Min(objFrom.GetLength(0), objTo.GetLength(0));
            int num2 = Math.Min(objFrom.GetLength(1), objTo.GetLength(1));

            state.AddKnownRef(objFrom, objTo);
            if ((!isDeep || DeepClonerSafeTypes.CanNotCopyType(typeof(T), null)) && objFrom.GetLength(0) == objTo.GetLength(0) && objFrom.GetLength(1) == objTo.GetLength(1))
            {
                Array.Copy(objFrom, objTo, objFrom.Length);
                return(objTo);
            }
            if (!isDeep)
            {
                for (int i = 0; i < num; i++)
                {
                    for (int j = 0; j < num2; j++)
                    {
                        int num3 = i;
                        int num4 = j;
                        T   val  = objFrom[i, j];
                        objTo[num3, num4] = val;
                    }
                }
                return(objTo);
            }
            if (typeof(T).IsValueType())
            {
                Func <T, DeepCloneState, T> clonerForValueType = DeepClonerGenerator.GetClonerForValueType <T>();
                for (int k = 0; k < num; k++)
                {
                    for (int l = 0; l < num2; l++)
                    {
                        int num5 = k;
                        int num6 = l;
                        T   val2 = clonerForValueType(objFrom[k, l], state);
                        objTo[num5, num6] = val2;
                    }
                }
            }
            else
            {
                for (int m = 0; m < num; m++)
                {
                    for (int n = 0; n < num2; n++)
                    {
                        int num7 = m;
                        int num8 = n;
                        T   obj  = (T)DeepClonerGenerator.CloneClassInternal(objFrom[m, n], state);
                        objTo[num7, num8] = obj;
                    }
                }
            }
            return(objTo);
        }
Ejemplo n.º 2
0
        internal static T[,] Clone2DimArrayInternal <T>(T[,] objFrom, T[,] objTo, DeepCloneState state, bool isDeep)
        {
            // not null from called method, but will check it anyway
            if (objFrom == null || objTo == null)
            {
                return(null);
            }
            var l1 = Math.Min(objFrom.GetLength(0), objTo.GetLength(0));
            var l2 = Math.Min(objFrom.GetLength(1), objTo.GetLength(1));

            state.AddKnownRef(objFrom, objTo);
            if ((!isDeep || DeepClonerSafeTypes.CanReturnSameObject(typeof(T))) &&
                objFrom.GetLength(0) == objTo.GetLength(0) &&
                objFrom.GetLength(1) == objTo.GetLength(1))
            {
                Array.Copy(objFrom, objTo, objFrom.Length);
                return(objTo);
            }

            if (!isDeep)
            {
                for (var i = 0; i < l1; i++)
                {
                    for (var k = 0; k < l2; k++)
                    {
                        objTo[i, k] = objFrom[i, k];
                    }
                }
                return(objTo);
            }

            if (typeof(T).IsValueType())
            {
                var cloner = DeepClonerGenerator.GetClonerForValueType <T>();
                for (var i = 0; i < l1; i++)
                {
                    for (var k = 0; k < l2; k++)
                    {
                        objTo[i, k] = cloner(objFrom[i, k], state);
                    }
                }
            }
            else
            {
                for (var i = 0; i < l1; i++)
                {
                    for (var k = 0; k < l2; k++)
                    {
                        objTo[i, k] = (T)DeepClonerGenerator.CloneClassInternal(objFrom[i, k], state);
                    }
                }
            }

            return(objTo);
        }
Ejemplo n.º 3
0
        // rare cases, very slow cloning. currently it's ok
        internal static Array CloneAbstractArrayInternal(Array objFrom, Array objTo, DeepCloneState state, bool isDeep)
        {
            // not null from called method, but will check it anyway
            if (objFrom == null || objTo == null)
            {
                return(null);
            }
            var rank = objFrom.Rank;

            if (objTo.Rank != rank)
            {
                throw new InvalidOperationException("Invalid rank of target array");
            }
            var lowerBoundsFrom = Enumerable.Range(0, rank).Select(objFrom.GetLowerBound).ToArray();
            var lowerBoundsTo   = Enumerable.Range(0, rank).Select(objTo.GetLowerBound).ToArray();
            var lengths         = Enumerable.Range(0, rank).Select(x => Math.Min(objFrom.GetLength(x), objTo.GetLength(x))).ToArray();
            var idxesFrom       = Enumerable.Range(0, rank).Select(objFrom.GetLowerBound).ToArray();
            var idxesTo         = Enumerable.Range(0, rank).Select(objTo.GetLowerBound).ToArray();

            state.AddKnownRef(objFrom, objTo);
            while (true)
            {
                if (isDeep)
                {
                    objTo.SetValue(DeepClonerGenerator.CloneClassInternal(objFrom.GetValue(idxesFrom), state), idxesTo);
                }
                else
                {
                    objTo.SetValue(objFrom.GetValue(idxesFrom), idxesTo);
                }
                var ofs = rank - 1;
                while (true)
                {
                    idxesFrom[ofs]++;
                    idxesTo[ofs]++;
                    if (idxesFrom[ofs] >= lowerBoundsFrom[ofs] + lengths[ofs])
                    {
                        idxesFrom[ofs] = lowerBoundsFrom[ofs];
                        idxesTo[ofs]   = lowerBoundsTo[ofs];
                        ofs--;
                        if (ofs < 0)
                        {
                            return(objTo);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        internal static T[] Clone1DimArrayClassInternal <T>(T[] objFrom, T[] objTo, DeepCloneState state)
        {
            if (objFrom == null || objTo == null)
            {
                return(null);
            }
            int num = Math.Min(objFrom.Length, objTo.Length);

            state.AddKnownRef(objFrom, objTo);
            for (int i = 0; i < num; i++)
            {
                objTo[i] = (T)DeepClonerGenerator.CloneClassInternal(objFrom[i], state);
            }
            return(objTo);
        }
Ejemplo n.º 5
0
        internal static Array CloneAbstractArrayInternal(Array objFrom, Array objTo, DeepCloneState state, bool isDeep)
        {
            if (objFrom == null || objTo == null)
            {
                return(null);
            }
            int rank = objFrom.Rank;

            if (objTo.Rank == rank)
            {
                int[] array  = Enumerable.Range(0, rank).Select(objFrom.GetLowerBound).ToArray();
                int[] array2 = Enumerable.Range(0, rank).Select(objTo.GetLowerBound).ToArray();
                int[] array3 = (from x in Enumerable.Range(0, rank)
                                select Math.Min(objFrom.GetLength(x), objTo.GetLength(x))).ToArray();
                int[] array4 = Enumerable.Range(0, rank).Select(objFrom.GetLowerBound).ToArray();
                int[] array5 = Enumerable.Range(0, rank).Select(objTo.GetLowerBound).ToArray();
                state.AddKnownRef(objFrom, objTo);
                while (true)
                {
                    if (isDeep)
                    {
                        objTo.SetValue(DeepClonerGenerator.CloneClassInternal(objFrom.GetValue(array4), state), array5);
                    }
                    else
                    {
                        objTo.SetValue(objFrom.GetValue(array4), array5);
                    }
                    int num = rank - 1;
                    while (true)
                    {
                        array4[num]++;
                        array5[num]++;
                        if (array4[num] < array[num] + array3[num])
                        {
                            break;
                        }
                        array4[num] = array[num];
                        array5[num] = array2[num];
                        num--;
                        if (num < 0)
                        {
                            return(objTo);
                        }
                    }
                }
            }
            throw new InvalidOperationException("Invalid rank of target array");
        }
Ejemplo n.º 6
0
        internal static T[] Clone1DimArrayClassInternal <T>(T[] objFrom, T[] objTo, DeepCloneState state)
        {
            // not null from called method, but will check it anyway
            if (objFrom == null || objTo == null)
            {
                return(null);
            }
            var l = Math.Min(objFrom.Length, objTo.Length);

            state.AddKnownRef(objFrom, objTo);
            for (var i = 0; i < l; i++)
            {
                objTo[i] = (T)DeepClonerGenerator.CloneClassInternal(objFrom[i], state);
            }

            return(objTo);
        }