讓你看起來類是這樣的:
[Serializable()] //Set this attribute to all the classes that want to serialize
public class User : ISerializable //derive your class from ISerializable
{
public int userInt;
public string userName;
//Default constructor
public User()
{
userInt = 0;
userName = "";
}
//Deserialization constructor.
public User(SerializationInfo info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
userInt = (int)info.GetValue("UserInt", typeof(int));
userName = (String)info.GetValue("UserName", typeof(string));
}
//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
//You can use any custom name for your name-value pair. But make sure you
// read the values with the same name. For ex:- If you write userInt as "UserInt"
// then you should read the same with "UserInt"
info.AddValue("UserInt", userInt);
info.AddValue("UserName", userName);
}
}
現在讀寫,你可以做這些:
User user=new User();
using(StreamWriter sw=new StreamWriter(/*Filename goes here*/))
{
using(BinaryFormatter bformatter=new BinaryFormatter())
{
bformatter.Serialize(sw, user);
}
}
using(StreamReader sr=new StreamReader(/*Filename goes here*/))
{
using(BinaryFormatter bformatter=new BinaryFormatter())
{
user=(User)bformatter.Deserialize(sr);
}
}
我得到了很多的代碼從http://www.codeproject.com/Articles/1789/Object-Serialization-using-C
那麼,什麼是你的問題?如果您不明確要查找的內容*特定*,則此問題將被視爲不清楚。 –
'user'的定義是什麼?請發佈**所有**的相關代碼,而不是一些不可編譯的代碼片段。 –
問題是什麼 –