protobuf网AsReference需要Activator.CreateInstance公共构造? [英] ProtoBuf-net AsReference require public constructor in Activator.CreateInstance?

查看:91
本文介绍了protobuf网AsReference需要Activator.CreateInstance公共构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这看起来像这样(最小)

While working on 2 of my classes that looks like this (minimal)

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<Child>() { 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<Family>(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<Child> m_Children;

        /// <summary>
        /// ProtoBuf deserialization constructor (fails here)
        /// </summary>
        private Parent() { m_Children = new List<Child>(); }

        public Parent(List<Child> children)
        {
            m_Children = children;
            m_Children.ForEach(x => x.Parent = this);
        }
    }

    [ProtoContract()]
    public class Family
    {
        [ProtoMember(1)]
        protected List<Parent> m_Parents;

        public void Add(Parent parent)
        {
            m_Parents.Add(parent);
        }

        public Family()
        {
            m_Parents = new List<Parent>();
        }
    }
}



在反序列化,我遇到除了无参数的构造函数此对象定义。创建于ProtoBuf.BclHelper父对象附近

During deserialization, I encounter the exception "No parameterless constructor defined for this object." for creating the Parent object in ProtoBuf.BclHelper near

case FieldObject:
// ...
value = ((options & NetObjectOptions.UseConstructor) == 0) ? BclHelpers.GetUninitializedObject(type) : Activator.CreateInstance(type);



然后当我改变了默认的构造函数父()公开,异常消失。

Then when I changed the default constructor Parent() to public, the exception goes away.

任何想法<击>我可能忽略是AsRerference在这种情况下,正确的用法?

Any idea what I may have overlooked is the correct usage for AsRerference in this case?

BOUNTY
虽然马克把他的时间来解决这个问题,我需要一个明确的解决方案中使用protobuf网在这种情况下,各地无论是protobuf网属性,方法或工作其他的技巧。否则,我将不得不完全放弃使用protobuf网的。 。感谢您的帮助。

BOUNTY: 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.

推荐答案

我相信你能做到这一点来解决这个问题:

I believe you can do this to fix this problem:

[ProtoContract(SkipConstructor = true)]
public class Parent
{
    [ProtoMember(1)]
    protected List<Child> m_Children;

    private Parent() { Initialize(); }

    [ProtoBeforeDeserialization] // could also use OnDeserializing
    private void Initialize()
    {
        m_Children = new List<Child>();
    }

    public Parent(List<Child> children)
    {
        m_Children = children;
        m_Children.ForEach(x => x.Parent = this);
    }



}

}

这篇关于protobuf网AsReference需要Activator.CreateInstance公共构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆