Note that there are some explanatory texts on larger screens.

plurals
  1. POProtoBuf-net AsReference require public constructor in Activator.CreateInstance?
    primarykey
    data
    text
    <p>While working on 2 of my classes that looks like this (minimal)</p> <pre><code>using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.IO; using ProtoBuf; namespace Sandbox { public partial class Form1 : Form { public Form1() { Family family = new Family(); Child child1 = new Child(1); Child child2 = new Child(2); Parent parent = new Parent(new List&lt;Child&gt;() { child1, child2 }); family.Add(parent); string file = "sandbox.txt"; try { File.Delete(file); } catch { } using (var fs = File.OpenWrite(file)) { Serializer.Serialize(fs, family); } using (var fs = File.OpenRead(file)) { family = Serializer.Deserialize&lt;Family&gt;(fs); } System.Diagnostics.Debug.Assert(family != null, "1. Expect family not null, but not the case."); } } [ProtoContract()] public class Child { [ProtoMember(1, AsReference = true)] internal Parent Parent; private Child() { } public Child(int i) { } } [ProtoContract()] public class Parent { [ProtoMember(1)] protected List&lt;Child&gt; m_Children; /// &lt;summary&gt; /// ProtoBuf deserialization constructor (fails here) /// &lt;/summary&gt; private Parent() { m_Children = new List&lt;Child&gt;(); } public Parent(List&lt;Child&gt; children) { m_Children = children; m_Children.ForEach(x =&gt; x.Parent = this); } } [ProtoContract()] public class Family { [ProtoMember(1)] protected List&lt;Parent&gt; m_Parents; public void Add(Parent parent) { m_Parents.Add(parent); } public Family() { m_Parents = new List&lt;Parent&gt;(); } } } </code></pre> <p>During deserialization, I encounter the exception "No parameterless constructor defined for this object." for creating the Parent object in ProtoBuf.BclHelper near</p> <pre><code>case FieldObject: // ... value = ((options &amp; NetObjectOptions.UseConstructor) == 0) ? BclHelpers.GetUninitializedObject(type) : Activator.CreateInstance(type); </code></pre> <p>Then when I changed the default constructor Parent() to public, the exception goes away.</p> <p>Any idea what <strike>I may have overlooked</strike> is the correct usage for AsRerference in this case?</p> <p><strong>BOUNTY</strong>: While Marc takes his time to fix the issue, I would require a definitive solution to use protobuf-net in this situation, working around either by protobuf-net attributes, methods or other tricks. Otherwise I will have to abandon the use of protobuf-net altogether. Thanks for any help.</p>
    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.
 

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