示例#1
0
        /// <summary>
        /// Creates a deep copy of an object
        /// </summary>
        /// <param name="original">The object to create a copy of</param>
        /// <param name="context">The copy context.</param>
        /// <returns>The copy.</returns>
        public static object DeepCopy(object original, ICopyContext context)
        {
            var source = original as AzureQueueBatchContainerV2;

            if (source == null)
            {
                throw new ArgumentNullException(nameof(original));
            }

            var copy = new AzureQueueBatchContainerV2();

            context.RecordCopy(original, copy);
            var           token  = source.sequenceToken == null ? null : new EventSequenceTokenV2(source.sequenceToken.SequenceNumber, source.sequenceToken.EventIndex);
            List <object> events = null;

            if (source.events != null)
            {
                events = new List <object>(source.events.Count);
                foreach (var item in source.events)
                {
                    events.Add(SerializationManager.DeepCopyInner(item, context));
                }
            }

            var ctx = source.requestContext?.ToDictionary(kv => kv.Key, kv => SerializationManager.DeepCopyInner(kv.Value, context));

            copy.SetValues(source.StreamGuid, source.StreamNamespace, events, ctx, token);
            return(copy);
        }
示例#2
0
        public static object DeepCopier(object original, ICopyContext context)
        {
            var clone = original.DeepClone();

            context.RecordCopy(original, clone);

            return(clone);
        }
示例#3
0
        public object DeepCopy(object source, ICopyContext context)
        {
            _logger.LogInformation("Deep copying: {source}", source.GetType().Name);
            var clone = ObjectCloner.Clone(source);

            context.RecordCopy(source, clone);
            return(clone);
        }
            public static object DeepCopier(object original, ICopyContext context)
            {
                TestTypeA input  = (TestTypeA)original;
                TestTypeA result = new TestTypeA();

                context.RecordCopy(original, result);
                result.Collection = (ICollection <TestTypeA>)SerializationManager.DeepCopyInner(input.Collection, context);
                return(result);
            }
示例#5
0
        static private object Copy(object input, ICopyContext context)
        {
            var inputCopy = context.CheckObjectWhileCopying(input);

            if (inputCopy == null)
            {
                context.RecordCopy(input, inputCopy);
            }
            var copy = SerializationManager.DeepCopyInner(input, context);

            return(copy);
        }
示例#6
0
        /// <summary>
        /// Create a deep copy of the token.
        /// </summary>
        /// <param name="original">The token to copy</param>
        /// <param name="context">The serialization context.</param>
        /// <returns>A copy</returns>
        public static object DeepCopy(object original, ICopyContext context)
        {
            var source = original as EventHubSequenceTokenV2;

            if (source == null)
            {
                return(null);
            }

            var copy = new EventHubSequenceTokenV2(source.EventHubOffset, source.SequenceNumber, source.EventIndex);

            context.RecordCopy(original, copy);
            return(copy);
        }
示例#7
0
        public static object DeepCopier(object original, ICopyContext context)
        {
            var input  = (IDictionary <string, object>)original;
            var result = new ActivityContext();

            foreach (var kvp in input)
            {
                result.Data.Add(kvp.Key, kvp.Value);
            }

            context.RecordCopy(original, result);

            return(result);
        }
示例#8
0
        public static object DeepCopier(object original, ICopyContext context)
        {
            var input  = (User)original;
            var result = new User();

            // Record 'result' as a copy of 'input'. Doing this immediately after construction allows for
            // data structures which have cyclic references or duplicate references.
            // For example, imagine that 'input.BestFriend' is set to 'input'. In that case, failing to record
            // the copy before trying to copy the 'BestFriend' field would result in infinite recursion.
            context.RecordCopy(original, result);

            // Deep-copy each of the fields.
            result.BestFriend     = (User)SerializationManager.DeepCopyInner(input.BestFriend, context);
            result.NickName       = input.NickName;       // strings in .NET are immutable, so they can be shallow-copied.
            result.FavoriteNumber = input.FavoriteNumber; // ints are primitive value types, so they can be shallow-copied.
            result.BirthDate      = (DateTimeOffset)SerializationManager.DeepCopyInner(input.BirthDate, context);

            return(result);
        }
            /// <inheritdoc />
            public object DeepCopy(object source, ICopyContext context)
            {
                var type         = source.GetType();
                var callbacks    = _serializationCallbacks.GetReferenceTypeCallbacks(type);
                var serializable = (ISerializable)source;
                var result       = FormatterServices.GetUninitializedObject(type);

                context.RecordCopy(source, result);

                // Shallow-copy the object into the serialization info.
                var originalInfo     = new SerializationInfo(type, _formatterConverter);
                var streamingContext = new StreamingContext(StreamingContextStates.All, context);

                callbacks.OnSerializing?.Invoke(source, streamingContext);
                serializable.GetObjectData(originalInfo, streamingContext);

                // Deep-copy the serialization info.
                var copyInfo = new SerializationInfo(type, _formatterConverter);

                foreach (var item in originalInfo)
                {
                    copyInfo.AddValue(item.Name, SerializationManager.DeepCopyInner(item.Value, context));
                }
                callbacks.OnSerialized?.Invoke(source, streamingContext);
                callbacks.OnDeserializing?.Invoke(result, streamingContext);

                // Shallow-copy the serialization info into the result.
                var constructor = _constructorFactory.GetSerializationConstructorDelegate(type);

                constructor(result, copyInfo, streamingContext);
                callbacks.OnDeserialized?.Invoke(result, streamingContext);
                if (result is IDeserializationCallback callback)
                {
                    callback.OnDeserialization(context);
                }

                return(result);
            }