0

Possible Duplicate:
Serialize a Static Class?

Is it possible to serialize class such as:

public static class Foo
{
    public static int foo1 = 1;
    public static int foo2 = 2;
    public static string foo3 = "test";
}

into JSON using JsonSerializer in C#?

If I use:

  JsonWriter jw = new JsonTextWriter(streamWriter)
  JsonSerializer serializer = new JsonSerializer();
  serializer.Serialize(jw, Foo);

appears error: error CS0118: 'Example.Foo' is a 'type' but is used like a 'variable' Thanks in advance for any responses.

@Update Serializing static classess is impossible, but you can create static instance of this class and serialize it. Sample:

    public static FooClass Foo = new FooClass();
    [Serializable]
    public class FooClass : System.Runtime.Serialization.ISerializable
    {
        public FooClass()
        {

        }
        public static int foo1 = 1;
        public static int foo2 = 2;
        public static string foo3 = "test";

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("static.foo1", FooClass.foo1, typeof(int));
            info.AddValue("static.foo2", FooClass.foo2, typeof(int));
            info.AddValue("static.foo3", FooClass.foo3, typeof(string));
        }

        public FooClass(SerializationInfo info, StreamingContext context)
        {
            FooClass.foo1 = info.GetInt32("static.foo1");
            FooClass.foo2 = info.GetInt32("static.foo2");
            FooClass.foo3 = info.GetString("static.foo3");
        }
    }
    ...
    serializer.Serialize(jw, Foo);
Community
  • 1
  • 1
Charlie Hopperson
  • 415
  • 1
  • 9
  • 23
  • 1
    What happened when you tried it? – Steve Wellens Dec 27 '12 at 02:08
  • 1
    Look at the following link http://stackoverflow.com/questions/7307100/servicestack-text-how-to-serialize-class-to-json or http://stackoverflow.com/questions/1056121/how-to-create-json-string-in-c-sharp or http://stackoverflow.com/questions/1293496/serialize-a-static-class – MethodMan Dec 27 '12 at 02:17
  • error CS0118: 'Example.Foo' is a 'type' but is used like a 'variable' using: `serializer.Serialize(jw, Foo);` – Charlie Hopperson Dec 27 '12 at 11:16
  • Your "update" should be an answer (which you can then accept) – JDB Dec 28 '12 at 16:37
  • I disagree with closing this question. Though it deals with serialization, I think the OP's goal is to pass the state of a static class through a web service. In that case, simply saying you can't serialize a static class doesn't really get to the heart of the OP's question. – JDB Dec 28 '12 at 16:41
  • 1
    If @CharlieHopperson post an answer and accept it, we can re-open the question. – j0k Dec 29 '12 at 13:25
  • @j0k - it's been a while, but an answer was posted and accepted. I nominate we reopen this one. – JDB Apr 24 '15 at 23:11

1 Answers1

1

Or, if you don't want to implement all the serialization junk, you can just serialize an anonymous type:

JsonWriter jw = new JsonTextWriter(streamWriter)
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(jw, 
    new {
        foo1 = Foo.foo1,
        foo2 = Foo.foo2,
        foo3 = Foo.foo3
    });
JDB
  • 25,172
  • 5
  • 72
  • 123