可以用静态构造函数序列化一个类吗?

时间:2015-07-29 06:13:43

标签: c# .net serialization

我有以下类,我正在序列化。是否可以在二进制序列化器中序列化。我在MSDN上使用以下帖子尝试过它 https://social.msdn.microsoft.com/Forums/vstudio/en-US/9e8d37f0-8df2-45d9-9902-6ab343371c2d/how-do-i-do-xml-serialization-of-classes-with-static-properties-in-c?forum=csharpgeneral

但得到以下错误

  

错误1可访问性不一致:属性类型   'System.Collections.Generic.List'不太容易访问   比属性'kafka01.HAND._HH'c:\ users \ superfast \ documents \ visual   studio 2013 \ Projects \ kafka01 \ kafka01 \ HAND.cs 15 32 kafka01

[Serializable()]
    public class HAND : ISerializable
    {

        public static List<HH> _HH { get; private set; }



         static HAND()
        {
            _HH = new List<HH>()
            {
            new HH(), new HH(), new HH(),
            new HH(), new HH(), new HH()
            };

        }


        internal  class HH
        {
            public string hand { get; set; }
            public string hand_sort { get; set; }
            public string board { get; set; }
            public int board_length { get; set; }

            public bool transformed { get; set; }
            public int z { get; set; }
            public int bb { get; set; }
            public double eff { get; set; }
            public double s_us { get; set; }
            public double s_vil { get; set; }
            public double bet_us { get; set; }
            public double bet_vil { get; set; }
            public double blind_us { get; set; }
            public double blind_vil { get; set; }
            public int pos { get; set; }
            public string pre { get; set; }


            public bool pre_set { get; set; }
            public bool new_pre { get; set; }
            public bool image_processing_lock { get; set; }

            public string node_action { get; set; }
            public string pre_action { get; set; }
            public string last_action { get; set; }
            public bool action_switch { get; set; }
            public bool action_switch1 { get; set; }


        }
        //Deserialization constructor.
        public HAND(SerializationInfo info, StreamingContext ctxt)
        {
            _HH = (List<HH>)info.GetValue("innerClass", typeof(List<HH>));
            //Get the values from info and assign them to the appropriate properties

        }

        //Serialization function.
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("innerClass", _HH);
            //You can use any custom name for your name-value pair. But make sure you
            // read the values with the same name. For ex:- If you write _HH as "innerClass"
            // then you should read the same with "innerClass"
        //  
        }


    }

这是我的主要方法

HAND h = new HAND();
            HAND._HH[0].board = "my val";
            FileStream fileStream = new FileStream("test.binary", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fileStream, h);
            fileStream.Dispose();
            BinaryDeserialize();
            Console.ReadLine();

这是我的BinaryDeserialize方法

>  private static void BinaryDeserialize()
>         {
>             FileStream fileStream = new FileStream("test.binary", FileMode.Open);
>             BinaryFormatter bf = new BinaryFormatter();
>             HAND myClass = (HAND)bf.Deserialize(fileStream);
>             Console.WriteLine(HAND._HH[0].board.ToString());
>             //MessageBox.Show(MyClass.StartIndex.HasValue.ToString());
>            // MessageBox.Show(MyClass.StartIndex.Value.ToString());
>         }

2 个答案:

答案 0 :(得分:0)

1-制作&#34; HH&#34;作为公众的阶级 2-制作&#34; Hand&#34;作为公众的阶级 再次编译,错误应该消失

答案 1 :(得分:0)

回答您的问题:是的,请务必注意,在反序列化对象时不会调用构造函数。

修复您的问题:制作HH课程public并将List<HH> _HH { get; public set; }属性设置者公开。