在 .Net 4 或 4.5 中,您将如何设计一个可序列化的类,其中包含一组类中的一个类的实例?例如,假设我有一个车库类,它可以包含任何“车辆”类型类的实例,比如汽车、船、摩托车、房车。但是车库只能容纳其中一个类的一个实例。我已经尝试了几种不同的方法来做到这一点,但我的问题是让它可序列化。
这是一个起始示例,其中 Garage 类中的实例只有一个选项。您应该能够将其直接插入新的控制台应用程序并进行尝试。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Patterns
{
[Serializable()]
public class Garage
{
private Vehicle _MyVehicle;
public Garage()
{
}
public string GarageOwner { get; set; }
public Vehicle MyVehicle
{
get { return _MyVehicle; }
set { _MyVehicle = value; }
}
}
[Serializable()]
public class Vehicle
{
public string VehicleType { get; set; }
public int VehicleNumber { get; set; }
}
class Serializer
{
static string _StartupPath = @"C:\Projects\Patterns\Data\";
static string _StartupFile = "SerializerTest.xml";
static string _StartupXML = _StartupPath + _StartupFile;
static void Main(string[] args)
{
Console.Write("Press w for write. Press r for read:");
ConsoleKeyInfo cki = Console.ReadKey(true);
Console.WriteLine("Pressed: " + cki.KeyChar.ToString());
if (cki.KeyChar.ToString() == "w")
{
Garage MyGarage = new Garage();
MyGarage.GarageOwner = "John";
MyGarage.MyVehicle = new Vehicle();
MyGarage.MyVehicle.VehicleType = "Car";
MyGarage.MyVehicle.VehicleNumber = 1234;
WriteGarageXML(MyGarage);
Console.WriteLine("Serialized");
}
else if (cki.KeyChar.ToString() == "r")
{
Garage MyGarage = ReadGarageXML();
Console.WriteLine("Deserialized Garage owned by " + MyGarage.GarageOwner);
}
Console.ReadKey();
}
public static void WriteGarageXML(Garage pInstance)
{
XmlSerializer writer = new XmlSerializer(typeof(Garage));
using (FileStream file = File.OpenWrite(_StartupXML))
{
writer.Serialize(file, pInstance);
}
}
public static Garage ReadGarageXML()
{
XmlSerializer reader = new XmlSerializer(typeof(Garage));
using (FileStream input = File.OpenRead(_StartupXML))
{
return reader.Deserialize(input) as Garage;
}
}
}
}
最佳答案
基于另一个SO article ,这就是最终对我有用的东西。 它可以干净地序列化和反序列化。使用这个例子,我可以设计一个对象“树”,其中有使用的选项。因此,这可以扩展为一辆汽车可以有一个具有多种不同引擎类型类别的引擎和一个具有多种不同内饰类型的内饰……等等。
代码通过添加以下语句开始工作:[XmlInclude(typeof(Car))]
但是如果有更好的方法请告诉我!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace Patterns
{
public class Garage
{
private Vehicle _MyVehicle;
public Garage()
{
}
public string GarageOwner { get; set; }
public Vehicle MyVehicle
{
get { return _MyVehicle; }
set { _MyVehicle = value; }
}
}
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Boat))]
[XmlInclude(typeof(Motorcycle))]
[XmlInclude(typeof(Motorhome))]
public abstract class Vehicle
{
public string VehicleType { get; set; }
public int VehicleNumber { get; set; }
}
public class Car : Vehicle
{
public int Doors { get; set; }
}
public class Boat : Vehicle
{
public int Engines { get; set; }
}
public class Motorcycle : Vehicle
{
public int Wheels { get; set; }
}
public class Motorhome : Vehicle
{
public int Length { get; set; }
}
class Serializer
{
static string _StartupPath = @"C:\Projects\Patterns\Data\";
static string _StartupFile = "SerializerTest.xml";
static string _StartupXML = _StartupPath + _StartupFile;
static void Main(string[] args)
{
Console.Write("Press w for write. Press r for read:");
ConsoleKeyInfo cki = Console.ReadKey(true);
Console.WriteLine("Pressed: " + cki.KeyChar.ToString());
if (cki.KeyChar.ToString() == "w")
{
Garage MyGarage = new Garage();
MyGarage.GarageOwner = "John";
Car c = new Car();
c.VehicleType = "Lexus";
c.VehicleNumber = 1234;
c.Doors = 4;
MyGarage.MyVehicle = c;
WriteGarageXML(MyGarage);
Console.WriteLine("Serialized");
}
else if (cki.KeyChar.ToString() == "r")
{
Garage MyGarage = ReadGarageXML();
Console.WriteLine("Deserialized Garage owned by " + MyGarage.GarageOwner);
}
Console.ReadKey();
}
public static void WriteGarageXML(Garage pInstance)
{
XmlSerializer writer = new XmlSerializer(typeof(Garage));
using (FileStream file = File.OpenWrite(_StartupXML))
{
writer.Serialize(file, pInstance);
}
}
public static Garage ReadGarageXML()
{
XmlSerializer reader = new XmlSerializer(typeof(Garage));
using (FileStream input = File.OpenRead(_StartupXML))
{
return reader.Deserialize(input) as Garage;
}
}
}
}
关于c# - 如何创建一个可序列化的类,其中包含一组类中的一个类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14326832/