//C#中
//我自定义了一个类
class MyClass
{
    string _musterName;
    ArrayList _musicName;
    public MyClass(string muster,ArrayList music)
    {
       this._musterName=muster;
       this._musicName=music;
    }
}
//如果我要过这个自定义类的实例进行IO的读写操作,应该如何实现呢?( 用byte[]?)
//也要实现从文件中读取后可直接赋给该类的另一个应用?
//高手帮帮小弟啊~!

解决方案 »

  1.   

    http://www.pconline.com.cn/pcedu/empolder/net/0405/368027.html序列化
      

  2.   

    你的IO操作跟类没有关系,问题不太明确
    IO操作中,TextReader,TextWrite,Stream,FILE等等都是IO,建议你把问题想清楚,打开帮助查查
      

  3.   

    一、修饰你的类为可系列化的(Serializable)
    二、用SoapFormatter或者BinaryFormatter来作系列化(XML或二进制流)请参考以下代码:
    [Serializable]                    //<---------------
    class MyClass
    {
        string _musterName;
        ArrayList _musicName;
        public MyClass(string muster, ArrayList music)
        {
            this._musterName = muster;
            this._musicName = music;
        }    public override string ToString()
        {
            string[] strs = new string[_musicName.Count];
            _musicName.CopyTo(strs);
            return _musterName + ":\n\n" + string.Join("\n", strs);
        }
    }         private void button1_Click(object sender, EventArgs e)
    {
        MyClass m = new MyClass("Mozart", new ArrayList(new string[]{"Hello", "Trout"}));    BinaryFormatter formatter = new BinaryFormatter();   //在System.Runtime.Serialization.Formatters.Binary命名空间
        using (Stream stream = new FileStream("data.bin", FileMode.Create))
        {
            formatter.Serialize(stream, m);
        }
    }private void button2_Click(object sender, EventArgs e)
    {
        MyClass m;
        BinaryFormatter formatter = new BinaryFormatter();
        using (Stream stream = new FileStream("data.bin", FileMode.Open))
        {
            m = formatter.Deserialize(stream) as MyClass;
        }
        MessageBox.Show(m.ToString());
    }