- ©«¤l
- 26
- ¥DÃD
- 2
- ºëµØ
- 0
- ¿n¤À
- 52
- ÂI¦W
- 0
- §@·~¨t²Î
- Mac OS X
- ³nÅ骩¥»
- Office 2010
- ¾\ŪÅv
- 20
- ©Ê§O
- ¨k
- µù¥U®É¶¡
- 2010-8-13
- ³Ì«áµn¿ý
- 2010-10-29
|
[¤À¨É] C# ª«¥ó§Ç¦C¤Æ»P¤Ï§Ç¦C¤Æ
C#ªºª«¥ó§Ç¦C¤Æ¥i±N°õ¦æ®É´Áªºª«¥óª¬ºA¿é¥X¦Üstream¡C¥»¤å¤¶²Ð¨Ï¥ÎBinaryFormatterªº¤èªk¡C
¨Ï¥ÎBinaryFormatter»Ý¤Þ¥Î¤U¦C©R¦WªÅ¶¡¡G
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
n«O¦sª¬ºAªºÃþ§O©w¸q»Ýn¼Ð°O[Serializable]ÄÝ©Ê¡A¨Ò¦p¤@Ó²³æªºÃþ§O«Å§i¦p¤U¡G
[Serializable]
class MyContact
{
public string name;
public int age;
public MyContact(string name, int age)
{
this.name = name;
this.age = age;
}
}
±N°õ¦æÓÅ骺ª¬ºA«O¦s¦¨ÀɮסG
MyContact data = new MyContact("John", 14);
IFormatter fmt = new BinaryFormatter();
Stream ostream = new FileStream("MyData.binary", FileMode.Create, FileAccess.Write, FileShare.None);
fmt.Serialize(ostream, data);
ostream.Close();
³oÃä¨Ï¥Î¤FFileStream(¤Þ¥ÎSystem.IO)¡C
¤èªk¦P¼Ë¾A¥Î©óStreamªºl¥ÍÃþ§O¡C |
|