Пример #1
0
        internal static T[,] CopyArrayRank2 <T>(T[,] originalArray, CopyContext context)
        {
            if (context.TryGetCopy(originalArray, out var existingCopy))
            {
                return((T[, ])existingCopy);
            }

            var lenI   = originalArray.GetLength(0);
            var lenJ   = originalArray.GetLength(1);
            var result = new T[lenI, lenJ];

            context.RecordCopy(originalArray, result);
            for (var i = 0; i < lenI; i++)
            {
                for (var j = 0; j < lenJ; j++)
                {
                    var original = originalArray[i, j];
                    if (original != null)
                    {
                        if (context.TryGetCopy(original, out var existingElement))
                        {
                            result[i, j] = (T)existingElement;
                        }
                        else
                        {
                            var copy = CopierGenerator <T> .Copy(original, context);

                            context.RecordCopy(original, copy);
                            result[i, j] = copy;
                        }
                    }
                }
            }
            return(result);
        }
Пример #2
0
        internal static T[] CopyArrayRank1 <T>(T[] originalArray, CopyContext context)
        {
            if (context.TryGetCopy(originalArray, out var existingCopy))
            {
                return((T[])existingCopy);
            }

            var length = originalArray.Length;
            var result = new T[length];

            context.RecordCopy(originalArray, result);
            for (var i = 0; i < length; i++)
            {
                var original = originalArray[i];
                if (original != null)
                {
                    if (context.TryGetCopy(original, out var existingElement))
                    {
                        result[i] = (T)existingElement;
                    }
                    else
                    {
                        var copy = CopierGenerator <T> .Copy(original, context);

                        context.RecordCopy(original, copy);
                        result[i] = copy;
                    }
                }
            }
            return(result);
        }
Пример #3
0
        /// <summary>
        /// Creates and returns a deep copy of the provided object.
        /// </summary>
        /// <typeparam name="T">The object type.</typeparam>
        /// <param name="original">The object to copy.</param>
        /// <returns>A deep copy of the provided object.</returns>
        public static T Copy <T>(T original)
        {
            var context = Context.Value;

            try
            {
                return(CopierGenerator <T> .Copy(original, context));
            }
            finally
            {
                context.Reset();
            }
        }
Пример #4
0
        internal static T[] CopyArrayRank1 <T>(T[] originalArray, CopyContext context)
        {
            if (context.TryGetCopy(originalArray, out var existingCopy))
            {
                return((T[])existingCopy);
            }

            var length = originalArray.Length;
            var result = new T[length];

            context.RecordCopy(originalArray, result);
            for (var i = 0; i < length; i++)
            {
                result[i] = CopierGenerator <T> .Copy(originalArray[i], context);
            }
            return(result);
        }
Пример #5
0
 /// <summary>
 /// Creates and returns a deep copy of the provided object.
 /// </summary>
 /// <typeparam name="T">The object type.</typeparam>
 /// <param name="original">The object to copy.</param>
 /// <param name="context">
 /// The copy context, providing referential integrity between multiple calls to this method.
 /// </param>
 /// <returns>A deep copy of the provided object.</returns>
 public static T Copy <T>(T original, CopyContext context)
 {
     return(CopierGenerator <T> .Copy(original, context));
 }