weixin_44353675 2019-08-07 20:10 采纳率: 50%
浏览 447

C# 关于二进制序列化时的数据没法正常保存的问题

程序可以正常运行,但是无法保存数据

class Program
    {
        static void Main(string[] args)
        {

            Size size = new Size ();

            BinaryFormatter bf = new BinaryFormatter();

            using (FileStream fs = new FileStream("date.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                size.Bil = 4;
                bf.Serialize(fs, size);
                Console.WriteLine("成功写入!");
            }

            try
            {
                FileStream fs1 = new FileStream("date.bin", FileMode.OpenOrCreate, FileAccess.Read);

                size = (Size)bf.Deserialize(fs1);
                Console.WriteLine("ag.bil:" + size.Bil);


                Console.WriteLine("static: " + Size.bil + "   " + Size.m);
            }catch(System.Net.Sockets.SocketException ex) { Console.WriteLine(ex); }
            Console.ReadKey();
        }

[Serializable]
    class Size
    {

        public static int bil;
        public static string m;
        public int Bil
        {
            get { return bil; }
            set { bil = value; }
        }
        public string M
        {
            get { return m; }
            set { m=value; }
        }
    }

结果最后储存的文件!

图片说明

有点不知所措了

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-08-07 20:53
    关注
        public static int bil;
        public static string m;
    这里去掉static
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!