猿问

如何让protubuf-net在反序列化时不考虑默认数组值

尝试这个:


static class Program

{

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

        Application.EnableVisualStyles();

        Application.SetCompatibleTextRenderingDefault(false);


        var f = new Form1();

        f.Mode = false;

        Application.Run(f);

        bool playerMode = f.Mode;

    }

}

和:


public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent();

    }


    public bool Mode = false;


    private void button1_Click(object sender, EventArgs e)

    {

        this.Mode = true;

        this.Close();

    }


    private void button2_Click(object sender, EventArgs e)

    {

        this.Mode = false;

        this.Close();

    }

}

这对我bool playerMode根据我单击的按钮进行设置很好用。


qq_笑_17
浏览 173回答 1
1回答

qq_遁去的一_1

这里的问题是 protobuf-net 通过附加数据来反序列化列表(等)数据。解决方案是将列表初始化从 ctor 移动到具有设置属性 SkipConstructor=true 的构造函数。[ProtoContract(SkipConstructor=true)]public class Data{&nbsp; &nbsp; [ProtoMember(1)]&nbsp; &nbsp; public bool[] Flags;&nbsp; &nbsp; public Data()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Falgs = new bool[3] { true, true, true }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答