为什么没有属性的 protobuf-net 序列化?
Why protobuf-net serializes without atrributes?
示例如下:
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 没有任何属性或契约,但 protobuf-net 没有抛出任何异常。为什么?
反序列化后 a1.x 包含 1.
目标框架是 4.5。 Protobuf-net版本是2.0.0.668,安装了nuget。
是v2的特性,来自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
(强调我的)
示例如下:
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 没有任何属性或契约,但 protobuf-net 没有抛出任何异常。为什么? 反序列化后 a1.x 包含 1.
目标框架是 4.5。 Protobuf-net版本是2.0.0.668,安装了nuget。
是v2的特性,来自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
(强调我的)