adnan
adnan

Reputation: 1459

How to convert Dictionary into Binary Serialize in C#

i have the following code convert Dictionary into Binary Serialize

private IDictionary<string, ConnectionManager> dictionary = new SortedDictionary<string, ConnectionManager>();
ConnectionManager conn;
dictionary.Add("testid", conn = new ConnectionManager());
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(@"c:\test1.txt",
FileMode.Create,FileAccess.Write, FileShare.None);
formatter.Serialize(stream, dictionary);

see the class code here in detail

ConnectionManager.cs

https://github.com/Sicos1977/MailKitImapIdler/tree/master/MailKitImapIdler

I have a problem to convert Dictionary into binary serialize. I have the following error while pass dictionary into binary serializes. I have already applied [Serializeable] tag on top of the ConnectionManager class

{"Type 'System.Threading.AutoResetEvent' in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."}

enter image description here

please suggest me, how can I resolve my problem. I appreciate your valuable time. thanks.

Upvotes: 0

Views: 700

Answers (1)

vasek
vasek

Reputation: 2849

This is a typical XY problem. You should not make an attempt to serialize complex ConnectionManager class full of transient members, e.g. event handles. Yous should pick neccessary configuration members from respective Connection classes and serialize only those.

For example, your code might look like this:

[Serializable]
ConnectionConfiguration
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Host { get; set; }
    public int Port { get; set; }

    // some other members might follow here
}

And then you serialize and deserialize only array of these objects. You might also add some type property with POP3/IMAP account type.

Then after deserialization you just simply call AddImapConnection or AddPop3Connection on your empty ConnectionManager which provides neccessary initialization for the manager object.

Upvotes: 1

Related Questions