请问在C#中如何实现对象的写入和读出文件。 希望将对象直接写入文件,而且在使用的时候直接读出。比如有个简单的类class student{int num;string name;int[] Score;}对于以上类所生成的对象如何直接写入文件中? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [Serializable]public class MyObject { public int n1 = 0; public int n2 = 0; public String str = null;}MyObject obj = new MyObject();obj.n1 = 1;obj.n2 = 24;obj.str = "Some String";IFormatter formatter = new BinaryFormatter();Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);formatter.Serialize(stream, obj);stream.Close(); [serializable]class student { int num; string name; int[] Score; } public static void main(){student c=new student();//soap进制序列化System.Runtime.Serialization.Formatters.Soap.SoapFormatter w=new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();w.serialize(new filestream("c:\123.txt"),c)//反序列化student e=w.deserialize(new filestream("c:\123.txt"))//2进制序列化System.Runtime.Serialization.Formatters.Binary.BinaryFormatter k=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()k.serialize(new filestream("c:\123.txt"),c)//反序列化student e=k.deserialize(new filestream("c:\123.txt"))//xml序列化System.Xml.Serialization.XmlSerializer n=new System.Xml.Serialization.XmlSerializer(c.gettype());n.serialize(new filestream("c:\123.txt"),c)//反序列化student e=n.deserialize(new filestream("c:\123.txt"))} SerializeableAttribute,实现ISerializable接口任何可以序列化的类都必须用 SerializableAttribute 进行标记。如果某个类需要控制其序列化进程,它可以实现 ISerializable 接口。Formatter 在序列化时调用 GetObjectData,并使用表示对象所需的全部数据来填充所提供的 SerializationInfo。Formatter 使用图形中对象的类型来创建 SerializationInfo。需要自己发送代理的对象可以使用 SerializationInfo 上的 FullTypeName 和 AssemblyName 方法来更改所传输的信息。在类继承的情况下,可以序列化从实现 ISerializable 的基类中派生的类。这种情况下,派生的类应在 GetObjectData 的实现内调用 GetObjectData 的基类实现。否则,不会序列化来自基类的数据。ISerializable 接口表示带有 Constructor 签名(SerializationInfo 信息、StreamingContext 上下文)的构造函数。在反序列化时,仅在格式化程序已反序列化 SerializationInfo 中的数据后才调用当前构造函数。一般而言,如果该类未密封,则应保护此构造函数。无法保证对象被反序列化的顺序。例如,如果一种类型引用尚未反序列化的类型,则会引发异常。如果创建具有这种依赖关系的类型,可以通过实现 IDeserializationCallback 接口和 OnDeserialization 方法来解决该问题。序列化结构处理像处理扩展 Object 的类型一样,处理扩展 MarshalByRefObject 的对象类型。这些类型都可以使用 SerializableAttribute 来标记,并且可以将 ISerializable 接口实现为其他任何对象类型。它们的对象状态将被捕获并在流中持续。当通过 System.Runtime.Remoting 使用这些类型时,远程处理结构会提供一个代理项,它将取代常用的序列化,而将代理序列化为 MarshalByRefObject。代理项是知道如何将特定类型的对象序列化和反序列化的帮助器。代理在大多数情况下对于用户不可见,其类型将是 ObjRef。作为一种常规的设计模式,类很少会既使用可序列化属性来标记,又扩展 MarshalByRefObject。当组合这两项特性时,开发人员应仔细考虑可能的序列化和远程处理方案。MemoryStream 就是一个适用的示例。当 MemoryStream ( Stream ) 的基类从 MarshalByRefObject 扩展时,可以捕获 MemoryStream 的状态并随时将其还原。因此,这样做可能是有意义的:将该流的状态序列化到数据库中,并在稍后某一时间将其还原。但是,当通过远程处理来使用时,这种类型的对象将设置代理。有关序列化 MarshalByRefObject 的派生类的更多信息,请参见 RemotingSurrogateSelector。有关如何实现 ISerializable 的更多信息,请参见 自定义序列化。 WPF中如何用代码设置控件的Margin 建立TXT文件后 一使用就 说 文件正在使用 c# 如何让窗体只有最大 最小化两种状态 请问大家都用 VS 2005的哪个版本? 高分求解,关于TCP消息异步收发的问题,只能收到前面两条???up有分!来人有分! .net中修改xml文件的问题 高手来帮忙 在C#中怎样动态创建WORD文档、EXCEL文档? C# 将32位BMP转为8位BMP! WPF双击事件 别走弯路 C#中如何获取对象实例大小? 请问:&和&&的区别在哪?
public class MyObject {
public int n1 = 0;
public int n2 = 0;
public String str = null;
}
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
class student
{
int num;
string name;
int[] Score;
}
public static void main()
{
student c=new student();//soap进制序列化
System.Runtime.Serialization.Formatters.Soap.SoapFormatter w=new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
w.serialize(new filestream("c:\123.txt"),c)//反序列化
student e=w.deserialize(new filestream("c:\123.txt"))//2进制序列化
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter k=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
k.serialize(new filestream("c:\123.txt"),c)//反序列化
student e=k.deserialize(new filestream("c:\123.txt"))
//xml序列化
System.Xml.Serialization.XmlSerializer n=new System.Xml.Serialization.XmlSerializer(c.gettype());
n.serialize(new filestream("c:\123.txt"),c)
//反序列化student e=n.deserialize(new filestream("c:\123.txt"))}