我们如何通过函数重载方法传递不同类的不同对象?
示例代码:
static public void SerializeToXML(Movie movie)
{
XmlSerializer serializer = new XmlSerializer(typeof(Movie));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movie);
textWriter.Close();
}
当我使用此代码时:
static public void SerializeToXML(FbTextView p, FbTextField q)
{
try
{
XmlSerializer textviewserilizer = new XmlSerializer(typeof(FbTextView));
XmlSerializer textfieldserilizer = new XmlSerializer(typeof(FbTextField));
TextWriter textWriter = new StreamWriter(@"D:\movie.xml");
textviewserilizer.Serialize(textWriter, p);
textfieldserilizer.Serialize(textWriter, q);
textWriter.Close();
}
catch (Exception ex)
{
while (ex != null)
{
Trace.WriteLine(ex.Message);
ex = ex.InnerException;
MessageBox.Show(ex.Message);
}
throw;
}
}
它的输出 xml 显示一些结果,即:
XML Parsing Error: junk after document element
Location: file:///D:/movie.xml
Line Number 6, Column 14:</FbTextView><?xml version="1.0" encoding="utf-8"?>
-------------^