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 7502909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:02:32+00:00 2026-05-29T21:02:32+00:00

I’m trying to serialize Dictionary<int, int> as Dictionary<string, string> . So I’ve created Test2

  • 0

I’m trying to serialize Dictionary<int, int> as Dictionary<string, string>.

So I’ve created Test2 type from Dictionary<int, int> as follows:

    [Serializable]
    internal sealed class Test2 : Dictionary<int, int>
    {
        internal Test2()
        {
        }

        private Test2(SerializationInfo info, StreamingContext context)
        {
            var data = (Dictionary<string, string>)
                       info.GetValue("data", typeof(Dictionary<string, string>));
            foreach (var item in data)
                Add(int.Parse(item.Key), int.Parse(item.Value));
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            var data = new Dictionary<string, string>();
            foreach (var item in this)
                data[item.Key.ToString()] = item.Value.ToString();
            info.AddValue("data", data, typeof(Dictionary<string, string>));
        }
    }

and use the following code to test the serialization:

        var test2 = new Test2 {{10, 10}};
        var formatter = new BinaryFormatter();
        using (var stream = new MemoryStream())
        {
            formatter.Serialize(stream, test2);
            stream.Position = 0;
            var clone = (Test2) formatter.Deserialize(stream);
        }

For some reason the clone does not contains any data (Count eq to 0).

Update:

“int” and “string” are here only for testing. In the real application I use something like Primary Key instead of string and big object instead of int and the serialized array contains relations between them. By cutting down the unrelated code and replacing the types I end up with with the example above.

I can only use .NET framework features.

  • 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-29T21:02:35+00:00Added an answer on May 29, 2026 at 9:02 pm

    There is no need for you to convert integers to strings during serialization or define your own serializable class – the framework takes care of that for you. This code (modified from this question) will do what you want just fine:

    var test2 = new Dictionary<int, int> { { 10, 10 } } ;
    var formatter = new BinaryFormatter();
    using (MemoryStream stream = new MemoryStream())
    {
        formatter.Serialize(stream, test2);
        stream.Position = 0;
        var clone = (Dictionary<int, int>) formatter.Deserialize(stream);
    }
    

    I’m uncertain why your original code didn’t work, but I think it has something to do with the SerializationInfo collection being unable to serialize/deserialize dictionaries correctly. I replaced your class with this code that uses List<Tuple<string,string>> in place of Dictionary<string,string> and it works fine:

    [Serializable]
    internal sealed class Test2 : Dictionary<int, int>
    {
        internal Test2()
        {
        }
    
        private Test2(SerializationInfo info, StreamingContext context)
        {
            var data = (List<Tuple<string, string>>)
                       info.GetValue("data", typeof(List<Tuple<string, string>>));
            foreach (var item in data)
                Add(int.Parse(item.Item1), int.Parse(item.Item2));
        }
    
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            var data = new List<Tuple<string, string>>();
            foreach (var item in this)
                data.Add(Tuple.Create(item.Key.ToString(), item.Value.ToString()));
            info.AddValue("data", data, typeof(List<Tuple<string, string>>));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.