| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2470 人关注过本帖
标题:请教:泛型类 可以被序列化吗?
只看楼主 加入收藏
learnerok
Rank: 2
等 级:论坛游民
帖 子:387
专家分:47
注 册:2011-5-21
结帖率:91.67%
收藏
已结贴  问题点数:20 回复次数:6 
请教:泛型类 可以被序列化吗?
请教老师达人:泛型类 可以被序列化吗?

比如:

[Serializable]
class S<T>
{
   int a = 0;
    void count ()
  {
    a = a+1
    Console.Write (a);
    Console.Read();
  }
 .........
}


S<int> a=new S<int>();
Stream steam = File.Open(filename, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(steam,a);
steam.Close();

======================
这样行吗?
搜索更多相关主题的帖子: steam void count 
2012-08-23 23:58
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:7 
可以的,泛型在编译时就已经被变成具体的类了。

授人以渔,不授人以鱼。
2012-08-24 01:01
comeguojiaxi
Rank: 2
等 级:论坛游民
威 望:3
帖 子:30
专家分:73
注 册:2012-6-7
收藏
得分:7 
泛型类可以被序列化、但前提是泛型参数可以序列化时。
2012-08-24 09:12
comeguojiaxi
Rank: 2
等 级:论坛游民
威 望:3
帖 子:30
专家分:73
注 册:2012-6-7
收藏
得分:0 
你的程序是可以的所谓的序列化类、不过就是序列化参数
如:
S<int> a=new S<int>();
Stream steam = File.Open(filename, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(steam,a);
2012-08-24 09:17
learnerok
Rank: 2
等 级:论坛游民
帖 子:387
专家分:47
注 册:2011-5-21
收藏
得分:0 
下面的代码 在表示层用 TabControl  -  TabPage  - 里面用 Lable 来 模拟 影院 生成的座位。

请教老师大侠,如何序列化保存生成的座位,以便在下次运行时可以看到所生成的座位?

可以做成“泛型类”-- 也就是说把 private void InitSeats(TabPage tp )下的代码和private void lblSeat_Click(object sender, EventArgs e) 下的代码全部放在“泛型类”中,然后实例化对象 ---- 然后序列化这个泛型类对象,可以吗?

拜谢老师先!!!

=====================================
namespace MyTest
{
    public partial class FrmSeat : Form
    {
        public FrmSeat()
        {
            InitializeComponent();
         

        }

      

        private void InitSeats(TabPage tp )
        {
            //用Label控件来表示座位
            Label label;

            //清空Label
            tp.Controls.Clear();


            int line;
            int column;
            //if (txtLine.Text.Trim() !="" && txtColumn.Text.Trim() != "")
            //{
                //行
                line = Convert.ToInt32(txtLine.Text.Trim());
                //列
                column = Convert.ToInt32(txtColumn.Text.Trim());
            //}

            
            for (int i = 0; i < line; i++)
            {
                for (int j = 0; j <= column; j++)
                {
                    label = new Label();
                    //设置背景颜色
                    label.BackColor = Color.Orange;
                    //设置尺寸
                    label.AutoSize = false;
                    label.Size = new System.Drawing.Size(35, 15);
                    //设置座位号
                    label.Text = (i + 1).ToString() + "-" + (j + 1).ToString();
                    label.TextAlign = ContentAlignment.MiddleCenter;
                    //设置边距位置
                    label.Location = new Point(50 + j * 38, 50 + (i * 25));
                    //所有的标签都绑定到同一事件
                    label.Click += new System.EventHandler(lblSeat_Click);
                    //添加Label
                    tp.Controls.Add(label);
                }
            }
        }


        private void lblSeat_Click(object sender, EventArgs e)
        {
            if (((Label)sender).BackColor == Color.Red)
            {
                MessageBox.Show("此票已经卖出:" + ((Label)sender).Text.ToString(), "提示");
                return;
            }
            MessageBox.Show("选择了:" + ((Label)sender).Text.ToString(), "提示");
            //修改座位的颜色
            ((Label)sender).BackColor = Color.Red;
        }


        

        private void FrmSeat_Load(object sender, EventArgs e)
        {
            this.txtLine.Text = null;
            this.txtColumn.Text = null;

        }

        private void btnCreatSeat_Click(object sender, EventArgs e)
        {


            InitSeats(tpCinema);


        }

      

        private void tpCinema_Click_1(object sender, EventArgs e)
        {

        }

    }

}
2012-08-24 23:37
learnerok
Rank: 2
等 级:论坛游民
帖 子:387
专家分:47
注 册:2011-5-21
收藏
得分:0 
我想上一张照片,更直观地解释我的东东,但无法上图
2012-08-24 23:39
learnerok
Rank: 2
等 级:论坛游民
帖 子:387
专家分:47
注 册:2011-5-21
收藏
得分:0 
我是这样做的“泛型类”--- 然后序列化 这个“泛型类” + 反序列化。

1)但是运行时报错: private void FrmSeat_Load(object sender, EventArgs e) 里的 ReadSeat()下面出现红线,
提示:“当前上下文中不存在名称为“ReadSeat()””

可是这个“ReadSeat()” 明明就在上面呀,到底哪出错了呢?

2) 我这样做对吗?

求老师大侠指教,多谢了!!!

=========================

public partial class FrmSeat : Form
    {
        public FrmSeat()
        {
            InitializeComponent();
        }


        [Serializable]
        public class List<Seat>
        {
           
            
            private static void InitSeats(TabPage tp)
            {
                //用Label控件来表示座位
                Label label;

                //清空Label
                tp.Controls.Clear();//tb 是 InitSeat (TabPage tb) 方法的参数


                int line = 0;
                int column = 0;
                //if (txtLine.Text.Trim() !="" && txtColumn.Text.Trim() != "")
                //{
                //行

                FrmSeat fs = new FrmSeat();
                line = Convert.ToInt32(fs.txtLine.Text.Trim());
                column = Convert.ToInt32(fs.txtColumn.Text.Trim());

                // line = Convert.ToInt32(FrmSeat.txtLine.Text.Trim());
                //列
                //column = Convert.ToInt32(FrmSeat.txtColumn.Text.Trim());
                //}

                //添加座位
                for (int i = 0; i < line; i++)
                {
                    for (int j = 0; j <= column; j++)
                    {
                        label = new Label();
                        //设置背景颜色
                        label.BackColor = Color.Orange;
                        //设置尺寸
                        label.AutoSize = false;
                        label.Size = new System.Drawing.Size(35, 15);
                        //设置座位号
                        label.Text = (i + 1).ToString() + "-" + (j + 1).ToString();
                        label.TextAlign = ContentAlignment.MiddleCenter;
                        //设置边距位置
                        label.Location = new Point(50 + j * 38, 50 + (i * 25));
                        //所有的标签都绑定到同一事件


                        label.Click += new System.EventHandler(lblSeat_Click);//把lblSeat_Click()方法作为参数放进来
                        //添加Label
                        tp.Controls.Add(label);
                    }
                }
            }


            private static void lblSeat_Click(object sender, EventArgs e) // 这个方法作为参数 放到new System.EventHandler(lblSeat_Click)- 实现该功能
            {
                if (((Label)sender).BackColor == Color.Red)
                {
                    MessageBox.Show("此票已经卖出:" + ((Label)sender).Text.ToString(), "提示");
                    return;
                }
                MessageBox.Show("选择了:" + ((Label)sender).Text.ToString(), "提示");
                //修改座位的颜色
                ((Label)sender).BackColor = Color.Red;
            }



            //序列化 - 保存生成的座位

            public static void SerializeSeat(Seat seat)
            {

                List<Seat> seats = new List<Seat>();

                using (FileStream fs = new FileStream("seats.bin", FileMode.Create))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, seats);
                }
            }



            // 反序列化 - 读取生成的座位

            public List<Seat> ReadSeat()  // 需要给参数吗?
            {
                List<Seat> list = new List<Seat>();
                if (!File.Exists("seats.bin"))
                {
                    return list;
                }
                FileStream fs = new FileStream("seats.bin", FileMode.Open);
                BinaryFormatter bf = new BinaryFormatter();
                list = (List<Seat>)bf.Deserialize(fs);
                fs.Close();
                return list;
            }
         
            
        }

        private void FrmSeat_Load(object sender, EventArgs e)
        {
            
            
            FrmSeat fs = new FrmSeat();
               fs.txtLine.Text = null;   
            fs.txtColumn.Text = null;
            ReadSeat();
        }
    }
2012-08-25 00:10
快速回复:请教:泛型类 可以被序列化吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035996 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved