Why protobuf-net serializes without atrributes?

475 views Asked by At

Here is the example:

class A
{
    public int x { get; private set; }

    public A(){}
    public A(int x)
    {
        this.x = x;
    }
}
class Program
{
    static void Main(string[] args)
    {
        A a = new A(1);
        A a1;
        using (FileStream fs = new FileStream("data", FileMode.Create, FileAccess.Write))
        {
            Serializer.Serialize(fs, a);
        }

        using (FileStream fs = new FileStream("data", FileMode.Open, FileAccess.Read))
        {
            a1 = Serializer.Deserialize<A>(fs);
        }
        Console.ReadLine();
    }  
}

Class A doesn't have any attribute or contract, but protobuf-net doesn't throw any exception. Why? After deserialization a1.x contains 1.

Target framework is 4.5. Protobuf-net version is 2.0.0.668, installed with nuget.

1

There are 1 answers

7
Scott Chamberlain On BEST ANSWER

It is a feature of v2, from the webpage

"v2" released

"v2" is a major overhaul to the core engine to allow much greater flexibility, and avoid a number of problems with over-use of generics. It is wire-compatible with your existing data, and the old API still exists. Simply: the library is much cleaner and leaner, and is much more versatile for onwards development. In particular v2 allows:

  • allow use on more platforms (iOS, WP7, Mono for Android, WinRT, etc)
  • allow use without attributes if you wish
  • allow pre-generation of a serialization assembly, to remove all reflection at runtime
  • and generally: just more features

(Emphasis mine)