Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(disclosure: I'm the author of protobuf-net)</p> <p><code>BinaryFormatter</code> is a metadata-based serializer; i.e. it sends .NET type information about every object serialized. protobuf-net is a contract-based serializer (the binary equivalent of <code>XmlSerializer</code> / <code>DataContractSerializer</code>, which will also reject this).</p> <p>There is no current mechanism for transporting <em>arbitrary</em> objects, since the other end will have <em>no way</em> of knowing what you are sending; however, if you have a <em>known set</em> of <em>different</em> object types you want to send, there may be options. There is also work in the pipeline to allow runtime-extensible schemas (rather than just attributes, which are fixed at build) - but this is far from complete.</p> <hr> <p>This isn't ideal, but it works... it should be easier when I've completed the work to support runtime schemas:</p> <pre><code>using System; using System.Collections.Generic; using ProtoBuf; [ProtoContract] [ProtoInclude(10, typeof(DataItem&lt;int&gt;))] [ProtoInclude(11, typeof(DataItem&lt;string&gt;))] [ProtoInclude(12, typeof(DataItem&lt;DateTime&gt;))] [ProtoInclude(13, typeof(DataItem&lt;Foo&gt;))] abstract class DataItem { public static DataItem&lt;T&gt; Create&lt;T&gt;(T value) { return new DataItem&lt;T&gt;(value); } public object Value { get { return ValueImpl; } set { ValueImpl = value; } } protected abstract object ValueImpl {get;set;} protected DataItem() { } } [ProtoContract] sealed class DataItem&lt;T&gt; : DataItem { public DataItem() { } public DataItem(T value) { Value = value; } [ProtoMember(1)] public new T Value { get; set; } protected override object ValueImpl { get { return Value; } set { Value = (T)value; } } } [ProtoContract] public class Foo { [ProtoMember(1)] public string Bar { get; set; } public override string ToString() { return "Foo with Bar=" + Bar; } } static class Program { static void Main() { var items = new List&lt;DataItem&gt;(); items.Add(DataItem.Create(12345)); items.Add(DataItem.Create(DateTime.Today)); items.Add(DataItem.Create("abcde")); items.Add(DataItem.Create(new Foo { Bar = "Marc" })); items.Add(DataItem.Create(67890)); // serialize and deserialize var clone = Serializer.DeepClone(items); foreach (DataItem item in clone) { Console.WriteLine(item.Value); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload