I'm trying to do something that I think should be simple: Convert a List<object>
into a stream
.
using ProtoBuf;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApp1
{
class MyObject
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<MyObject> myObjects = new();
myObjects.Add(new() { Prop1 = "foo1", Prop2 = "bar1", Prop3 = "something1" });
myObjects.Add(new() { Prop1 = "foo2", Prop2 = "bar2", Prop3 = "something2" });
myObjects.Add(new() { Prop1 = "foo3", Prop2 = "bar3", Prop3 = "something3" });
using var stream = new MemoryStream();
Serializer.Serialize(stream, myObjects); // Error is here
byte[] bytes = stream.ToArray();
}
}
}
This is the error:
System.InvalidOperationException HResult=0x80131509 Message=No serializer for type ConsoleApp1.MyObject is available for model (default) Source=protobuf-net.Core
CodePudding user response:
You have to add ProtoContract to your class like this:
[ProtoContract]
class MyObject
{
[ProtoMember(1)]
public string Prop1 { get; set; }
[ProtoMember(2)]
public string Prop2 { get; set; }
[ProtoMember(3)]
public string Prop3 { get; set; }
}
https://github.com/protobuf-net/protobuf-net/blob/main/README.md