反序列化一些 类
Deserialize some classes
我创建了一个可序列化的 class 并在反序列化此文件时序列化了一些 class 实例后,我得到了一个 class,但我需要获取所有 class ]es.
我使用 BinaryFormatter 序列化 classes。我使用 .NET Framework 在 C# 上编码。
我现在的反序列化:
using (var fs = new FileStream("doing.dat", FileMode.OpenOrCreate))
{
ActionsClass = formatter.Deserialize(fs) as Doing;
items.Add(ActionsClass.action);
}
Class 正在做:
[Serializable]
public class Doing : ActionsForm
{
public Doing(DateTime leadtime, string act)
{
if (leadtime < DateTime.Now) throw new ArgumentException("Some error text", nameof(leadtime));
if (act == "") throw new ArgumentException("Some error text", nameof(act));
PostTime = DateTime.Now;
TimeLead = leadtime;
action = act;
}
}
和class ActionsForm:
[Serializable]
public abstract class ActionsForm
{
public DateTime PostTime { get; protected set; }
public DateTime TimeLead { get; protected set; }
public string action { get; protected set; }
}
您必须反序列化您序列化对象的时间。
BinaryFormatter formatter = new BinaryFormatter();
DerivedClass object1 = new DerivedClass() { _baseField = "Object 1" };
DerivedClass object2 = new DerivedClass() { _baseField = "Object 2" };
using (FileStream writestream = new FileStream(@"data.bin", FileMode.Create, FileAccess.Write))
{
formatter.Serialize(writestream, object1);
formatter.Serialize(writestream, object2);
}
FileStream readstream = new FileStream(@"data.bin", FileMode.Open, FileAccess.Read);
DerivedClass deserializedobject1 = (DerivedClass)formatter.Deserialize(readstream);
DerivedClass deserializedobject2 = (DerivedClass)formatter.Deserialize(readstream);
readstream.Dispose();
我创建了一个可序列化的 class 并在反序列化此文件时序列化了一些 class 实例后,我得到了一个 class,但我需要获取所有 class ]es.
我使用 BinaryFormatter 序列化 classes。我使用 .NET Framework 在 C# 上编码。
我现在的反序列化:
using (var fs = new FileStream("doing.dat", FileMode.OpenOrCreate))
{
ActionsClass = formatter.Deserialize(fs) as Doing;
items.Add(ActionsClass.action);
}
Class 正在做:
[Serializable]
public class Doing : ActionsForm
{
public Doing(DateTime leadtime, string act)
{
if (leadtime < DateTime.Now) throw new ArgumentException("Some error text", nameof(leadtime));
if (act == "") throw new ArgumentException("Some error text", nameof(act));
PostTime = DateTime.Now;
TimeLead = leadtime;
action = act;
}
}
和class ActionsForm:
[Serializable]
public abstract class ActionsForm
{
public DateTime PostTime { get; protected set; }
public DateTime TimeLead { get; protected set; }
public string action { get; protected set; }
}
您必须反序列化您序列化对象的时间。
BinaryFormatter formatter = new BinaryFormatter();
DerivedClass object1 = new DerivedClass() { _baseField = "Object 1" };
DerivedClass object2 = new DerivedClass() { _baseField = "Object 2" };
using (FileStream writestream = new FileStream(@"data.bin", FileMode.Create, FileAccess.Write))
{
formatter.Serialize(writestream, object1);
formatter.Serialize(writestream, object2);
}
FileStream readstream = new FileStream(@"data.bin", FileMode.Open, FileAccess.Read);
DerivedClass deserializedobject1 = (DerivedClass)formatter.Deserialize(readstream);
DerivedClass deserializedobject2 = (DerivedClass)formatter.Deserialize(readstream);
readstream.Dispose();