Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 657097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:47:09+00:00 2026-05-13T22:47:09+00:00

While using this code to serialize an object: public object Clone() { var serializer

  • 0

While using this code to serialize an object:

public object Clone()
{
    var serializer = new DataContractSerializer(GetType());
    using (var ms = new System.IO.MemoryStream())
    {
        serializer.WriteObject(ms, this);
        ms.Position = 0;
        return serializer.ReadObject(ms);
    }
}

I have noticed that it doesn’t copy the relationships.
Is there any way to make this happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:47:10+00:00Added an answer on May 13, 2026 at 10:47 pm

    Simply use the constructor overload that accepts preserveObjectReferences, and set it to true:

    using System;
    using System.Runtime.Serialization;
    
    static class Program
    {
        public static T Clone<T>(T obj) where T : class
        {
            var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);
            using (var ms = new System.IO.MemoryStream())
            {
                serializer.WriteObject(ms, obj);
                ms.Position = 0;
                return (T)serializer.ReadObject(ms);
            }
        }
        static void Main()
        {
            Foo foo = new Foo();
            Bar bar = new Bar();
            foo.Bar = bar;
            bar.Foo = foo; // nice cyclic graph
    
            Foo clone = Clone(foo);
            Console.WriteLine(foo != clone); //true - new object
            Console.WriteLine(clone.Bar.Foo == clone); // true; copied graph
    
        }
    }
    [DataContract]
    class Foo
    {
        [DataMember]
        public Bar Bar { get; set; }
    }
    [DataContract]
    class Bar
    {
        [DataMember]
        public Foo Foo { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While using this code, i can't get any results... Ext.define('Teste.view.Main', { extend: 'Ext.Container', initialize:
I am Getting a run time Exception while using this code Runtime rt =
I am new to maven, while using mvn install I've got this error, any
I am using the following code to serialize an object and to attach it
I made an image slider using this plugin http://slidesjs.com/ while ago for a mobile
I have been using this copy method for quite a while, in lots of
I followed the steps using this site : http://wiki.apache.org/nutch/RunNutchInEclipse I encountered a problem while
After a while using the website, loading contents, etc.. this message shows Fill: SelectCommand.Connection
While developing a firefox extension, I create a wizard window from overlay.js using: this.wizard
Writing a python program, and I came up with this error while using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.