我有一个父类PartNumber和两个子类PartNumberChild1和PartNumberChild2。在我的主要创建一个PartNumberContainer并添加了两个子对象和两个子对象2,我想将不同的子类存储在PartNumberContainer类的PartNumber列表中,并以XML格式编写和读取PartNumberContainer。
当我运行时,当我尝试写出XML文件时,我得到一个InvalidOperationException。
[Serializable]
[XmlInclude(typeof(PartNumberChild1))]
[XmlInclude(typeof(PartNumberChild2))]
public class PartNumber
{
private string partNumber = "";
public abstract ClassType;
public PartNumber()
{
}
public PartNumber(string partNod)
{
partNumber = partNo;
}
public string PartNumber
{
get { return partNumber; }
set { partNumber = value; }
}
}
[Serializable]
Public class PartNumberChild1 : PartNumber
{
private string subPartNumber;
private string classType = "Child1";
public PartNumberChild1(string partNo, string subPartNo)
:base(partNo)
{
subPartNumber = subPartNo;
}
public PartNumberChild1()
{
}
public string SubPartNumber
{
get { return subPartNumber; }
set { subPartNumber = value; }
}
public string ClassType
{
get { return classType; }
set { classType = value; }
}
}
[Serializable]
Public class PartNumberChild2 : PartNumber
{
private string subPartNumber;
private string classType = "Child2";
public PartNumberChild1(string partNo, string subPartNo)
:base(partNo)
{
subPartNumber = subPartNo;
}
public PartNumberChild2()
{
}
public string SubPartNumber
{
get { return subPartNumber; }
set { subPartNumber = value; }
}
public string ClassType
{
get { return classType; }
set { classType = value; }
}
}
[Serializable]
public class PartNumberContainer
{
public static string PART_NUMBER_FILENAME_NAME = " Part_Numbers.xml";
private List<PartNumber> partNumbers;
public PartNumberContainer()
{
partNumbers = new List<PartNumber>();
}
public List<PartNumber> PartNumbers
{
get { return partNumbers; }
set { partNumbers = value; }
}
public void AddPartNumber(PartNumber partNo)
{
partNumbers.Add(partNo);
}
public void AddPartNumber(string partNo)
{
partNumbers.Add(new PartNumber(partNo));
}
public void WriteMeToFile()
{
try
{
我在这一行得到错误。
XmlSerializer x = new XmlSerializer(this.GetType());
StreamWriter writer = new StreamWriter(PART_NUMBER_FILENAME_NAME);
writer.Flush();
x.Serialize(writer, this);
writer.Close();
}
catch (IOException)
{
MessageBox.Show("Exception", "Error Message", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
public static PartNumberContainer ReadMeFromFile()
{
PartNumberContainer fileData = new PartNumberContainer();
object myObj = fileData;
try
{
XMLFileReaderWriter.ReadConfigurationFile(ref myObj, GetConfigurationFileFolderPath(), PART_NUMBER_FILENAME_NAME);
try
{
XmlSerializer x = new XmlSerializer(myObj.GetType());
StreamReader reader = new StreamReader(PART_NUMBER_FILENAME_NAME);
myProperties = x.Deserialize(reader);
}
catch
{
MessageBox.Show("Exception", "Error Message", MessageBoxButton.OK, MessageBoxImage.Warning);
}
fileData = (PartNumberContainer)myObj;
}
catch
{ // Error reading file
MessageBox.Show(err, "Error Message", MessageBoxButton.OK, MessageBoxImage.Warning);
}
return fileData;
}
public MainWindow()
{
public static PartNumberContainer myPartNoContainer = new UUTPartNumberContainer();
myPartNoContainer.AddPartNumber(new PartNumberChild1("onePartNo","OneSubPartNo"));
myPartNoContainer.AddPartNumber(new PartNumberChild1("twoPartNo","twoSubPartNo"));
myPartNoContainer.AddPartNumber(new PartNumberChild2("onePartNo","OneSubPartNo"));
myPartNoContainer.AddPartNumber(new PartNumberChild2("twoPartNo","twoSubPartNo"));
myPartNoContainer.WriteMeToFile();
PartNumberContainer myPartNoContainerRead = PartNumberContainer.ReadMeFromFile();
Foreach(PartNumber partNo in myPartNoContainerRead. PartNumbers)
{
WriteLine(string.format(“The Part No = {0}, The subPartNumber = {1},
the ClassType = {2} “,partNo.PartNumber, partNo. SubPartNumber , partNo. ClassType )
}
答案 0 :(得分:0)
我必须做的是将两个子类公开 - 公共类PartNumberChild2:PartNumber我必须添加构造函数public PartNumberChild2(){}