I need to serialize static class with BinaryFormatter, here is the code:
void Serialize()
{
IFormatter formatter = new BinaryFormatter();
using (FileStream s = File.Create("ServerInfo.bin"))
formatter.Serialize(s, Server); // ERROR: Error 44 'Server' is a 'type' but is used like a 'variable'
}
How can i fix this?
You normally serialize instances. You can't have an instance of a static class, so it makes no sense to serialize it.
If you need to serialize the static state of a static class, then you should probably make it non-static to start with. If you really need to keep the static class static but serialize the static state, you could create a normal class with the same variables (but instance variables instead of static ones), and make methods in your static class to create an instance from the current static state, or replace the current static state based on an instance passed in. But fundamentally it's not terribly pleasant to do so...
See more on this question at Stackoverflow