I am playing with the beta of Protobuf-net v2 (r363 from the SVN). I built it using Visual Studio 2010, the project Proto 2010.sln, and from there protobuf-net_Phone7 using the Silverlight 2 configuration. I can reference the resulting dll from Windows Phone 7 projects. I am mentioning all this because I am not 100% sure that this is the right way to build it.
Assuming it is the right one, I tried to build a very simple project but it fails. I get a MissingMethodException at ProtoBuf.Serializers.TypeSerializer.CreateInstance(ProtoReader source) when attempting to Deserialize. This same code (but in a Form instead of a Page) works fine on the same version of protobuf-net v2 that I built for Windows Mobile 6.5, so I wonder if I either built it incorrectly or there is a different way to use it in WP7.
[ProtoContract]
public class Person
{
[ProtoMember(1)]
public int Id { get; set; }
[ProtoMember(2)]
public string Name { get; set; }
[ProtoMember(3)]
public Address Address { get; set; }
}
[ProtoContract]
public class Address
{
[ProtoMember(1)]
public string Line1 { get; set; }
开发者_JAVA技巧[ProtoMember(2)]
public string Line2 { get; set; }
}
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Person person = new Person();
person.Address = new Address();
person.Address.Line1 = "First Line";
person.Address.Line2 = "Second Line";
person.Id = 1;
person.Name = "Name";
MemoryStream ms = new MemoryStream();
Serializer.Serialize(ms, person);
ms.Position = 0;
Person person2 = Serializer.Deserialize(ms);
ms.Position = 0;
}
}
This blog post deals with some options - http://blog.chrishayuk.com/2010/12/protocol-buffers-generator.html - and says its built for WP7
精彩评论