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

  • SEARCH
  • Home
  • 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 3936340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:56:09+00:00 2026-05-19T23:56:09+00:00

I trying to serialize a custom type which holds a dictionary among other members.

  • 0

I trying to serialize a custom type which holds a dictionary among other members. The types associated with key and value of the dictionary are interfaces which are implemented.

The dictionary looks like

 Dictionary<ITypeA, ITypeA> 

TypeA implements ITypeA, 
SubTypeOfA inherits from TypeA
SubTypeOfB inherits from SubTypeOfA

pseudo code looks something like this:

            List<Type> knownTypes = new List<Type>() { 
                typeof(TypeA), 
                typeof(SubTypeOfA),
                typeof(SubTypeOfB)
            };

DataContractSerializer serializer =
                new DataContractSerializer(typeof(DataHolder), knownTypes);

            using (FileStream fs = new FileStream(completeFilePath, FileMode.Create))
            {
                serializer.WriteObject(fs, templateData);
                success = true;
            }

I get a StackOverflowException when WriteObject() is getting called, am clueless on what is causing that to happen.

All the classes in the hierarchy are decorated with [DataContract] and the members to be serialized are decoreated with [DataMember].

Any help would be appreciated.

  • 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-19T23:56:09+00:00Added an answer on May 19, 2026 at 11:56 pm

    I might expect something like this if you have a cycle in the graph, but which is somehow not detected as an object identity failure. By cyclic, I mean:

    using System.Runtime.Serialization;
    [DataContract] class Foo {
        public Foo() { Bar = this; }
        [DataMember] public Foo Bar { get; set; }
        static void Main() {
            new DataContractSerializer(typeof(Foo)).WriteObject(
                System.IO.Stream.Null, new Foo());
        }
    }
    

    which throws the error:

    Object graph for type ‘Foo’ contains cycles and cannot be serialized if reference tracking is disabled.

    This is because it is trying to walk the tree (not a graph), and noticing a repeat (identical object reference), and stopping. However, by testing the above (and seeing when the get is called), it looks like DCS actually does this by spotting pain – the depth before it aborts is very high.

    Locally, I get 528 calls to Bar before it dies. If you already have complex code above this in the stack, it could account for a stack overflow, for sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to serialize a custom class that needs to use multiple elements of
I'm trying to serialize a Type object in the following way: Type myType =
I'm trying to serialize a type using the DataContractSerializer and am getting the exception
I am trying to serialize/deserialize objects that have factory-created members. For example, lets say
I am trying to persist a large class called an Authorizer which holds a
I am trying to serialize a queryset into json using my custom iterator. On
I'm trying to serialize one of my models which has an ImageField . The
I am trying to serialize an object to a sql compact database. I am
I'm trying to serialize an object to XML that has a number of properties,
I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to

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.