52746

C# - Serializing and deserializing static member

I have a static dictionary in my class which holds 12000 values. When I try to save my class I need to refresh and add some data in static dictionary at server side.

The problem is that after adding the values into static dicitionary, it still retains 12000 values, not 12001 (the last one doesn't get added). It's not able to serialize and deserialize the static member.

I think, as static member are not part of the object, so it doesn't get seralized. I can implement ISerializable interface and add the last member. But I think it's not a good idea.

Is there a better way to do that? I'm working on C# Windows application.

Answer1:

You may serialize. Here is a code,

[Serializable ] public class Numbers { public int no; public static int no1; } class Test { static void Deser() { Numbers a; FileStream fs = new FileStream("a1.txt", FileMode.Open ); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); a = (Numbers)bs.Deserialize(fs); Numbers.no1 = (int)bs.Deserialize(fs); fs.Close(); Console.WriteLine(a.no + " " + Numbers.no1); } static void Ser() { Numbers a = new Numbers(); a.no = 100; Numbers.no1 = 200; FileStream fs = new FileStream("a1.txt", FileMode.Create); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bs.Serialize(fs, a); bs.Serialize(fs, Numbers.no1); fs.Close(); } }

Answer2:

What behaviour would you expect if you sent data from several different clients to the server?

Suppose client A had added items X and Y, and client B had added items Y and Z. I'm guessing that you'd want the static dictionazry to end up with item X, Y and Z, but not two Ys.

I think you will need to special code in your ISerializable implementation, and I think that's quite legitimate.

I would have an extra non-static member list variable called something like "myDictionaryAdditions" when ever I add to static dictionary I would add to this list. Presumably this will get correctly trasnfered to the server. Now you just need some code in teh de-serializer to transfer non-dups to the static dictionary.

Recommend

  • JsonConvert string to Collection
  • Should any domain object, not be serializable?
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Django simple Captcha “No module named fields” error
  • copying resource to sdcard gives a damaged file in android
  • Database structure design with variable amounts of fields
  • how to adjust image in a panel in Java swing?
  • Spring Data JPA custom method causing PropertyReferenceException
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Read text file and split every line in MSBuild
  • Different response to non-authenticated users and AJAX calls
  • Problems to linebreak with an int in JLabel
  • How reduce the height of an mschart by breaking up the y-axis
  • Bug in WPF DataGrid
  • Sony Xperia Z Tablet not found by adb
  • Java applet as stand-alone Windows application?
  • output of program is not same as passed argument
  • Incrementing object id automatically JS constructor (static method and variable)
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • Javascript convert timezone issue
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Do create extension work in single-user mode in postgres?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Matrix multiplication with MKL
  • How to include full .NET prerequisite for Wix Burn installer
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How do you join a server to an Active Directory (domain)?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • File not found error Google Drive API
  • How can I remove ASP.NET Designer.cs files?
  • Is it possible to post an object from jquery to bottle.py?
  • Sorting a 2D array using the second column C++
  • Converting MP3 duration time
  • java string with new operator and a literal