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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:16:21+00:00 2026-06-12T19:16:21+00:00

I have a similar Problem to Serializing a HashSet I have a Class with

  • 0

I have a similar Problem to Serializing a HashSet

I have a Class with a Member of type Dictionary<String,HashSet<T>>

When i serialize the Object with BinaryFormatter, and then deserialize it, it comes up empty.

I don’t know how, or where to call the workaround posted here.

Any Hints? thanks in advance.

edit:
i tried to convert the hashset to a list, as one of the comments in the other thread suggested.

the object looks like this:

public class THashSet : HashSet<T> , ISerializable
{

    public THashSet(SerializationInfo info, StreamingContext context)
    {
        var list = (List<T>)info.GetValue("hashset", typeof(List<T>));     
        foreach (T t in list)
            this.Add(t);
    }

    public override void GetObjectData(SerializationInfo info,StreamingContext context)
    {
        info.AddValue("hashset", this.ToList<T>());
    }

when the object containing the THashSet is deserialized (and the constructor is called), the list is recovered correctly, as i see in the debugger.

but after the serializer is done, the object only contains an empty hashset.

  • 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-06-12T19:16:22+00:00Added an answer on June 12, 2026 at 7:16 pm

    Suppose for your T object you haven’t override methods GetHashCode and Equals. You need to do it.

    UPD: All depends from your object implementation. And object is not so easy as you say. Your sitation, object:

    [Serializable]
    public class DataClass
    {       
        public DataClass()
        {
        }
    
        public DataClass(string name, string description)
        {
            Name = name;
            Description = description;
    
            this.Dictionary = new Dictionary<string, HashSet<DataClass>>();
        }
    
        public string Name { get; set; }
    
        public string Description { get; set; }
    
        public Dictionary<string, HashSet<DataClass>> Dictionary { get; set; }
    }
    

    And serialization/deserialization code:

    DataClass dataClass = new DataClass("name", "descr");
    dataClass.Dictionary.Add("key1", new HashSet<DataClass>() { new DataClass("sample11", "descr11"), new DataClass("sample12", "descr12") });
    dataClass.Dictionary.Add("key2", new HashSet<DataClass>() { new DataClass("sample21", "descr21"), new DataClass("sample22", "descr22") });
    dataClass.Dictionary.Add("key3", new HashSet<DataClass>() { new DataClass("sample31", "descr31"), new DataClass("sample32", "descr32") });
    
    byte[] serialized;
    var formatter = new BinaryFormatter();
    
    using (MemoryStream stream = new MemoryStream())
    {
        formatter.Serialize(stream, dataClass);
        serialized = stream.ToArray();
    }
    using (MemoryStream streamDeserial = new MemoryStream())
    {
        streamDeserial.Write(serialized, 0, serialized.Length);
        streamDeserial.Seek(0, SeekOrigin.Begin);
        var dictDeserial = formatter.Deserialize(streamDeserial) as DataClass;
    } 
    

    This code works well.

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

Sidebar

Related Questions

I have similar problem as is described in Spring Framework - New object is
I have a problem on serializing a class to XML. I have created a
I have a similar problem with this ( https://wordpress.stackexchange.com/questions/9593/custom-post-type-archive-with-pagination ) and can't really figure
I have similar problem to one discussed here , but with stronger practical usage.
I notice this thread: Fastish Python/Jython IPC , and I have a similar problem,
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a problem similar to the egg and the chicken. I have two
I suppose similar problem would have been discussed here, but I couldn't find it.
My problem is similar to Django Passing Custom Form Parameters to Formset Ive have
I have a cross-tab report which similar to this Problem is when there are

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.