这是我的代码:
public partial class MainWindow : Window
{
List<Cliente> CContent;
string mainPath = @"D:\70-536\Clientes.dat";
public MainWindow()
{
InitializeComponent();
Do();
}
private void Do()
{
FileInfo fi = new FileInfo(mainPath);
if (fi.Exists)
{
CContent = ReturnListOfPersistentFile<Cliente>(mainPath);
}
else
{
CContent = new List<Cliente>();
}
}
public List<T> ReturnListOfPersistentFile<T> (string Filename)
{
SoapFormatter sf = new SoapFormatter();
using (Stream fStream = new FileStream(Filename,FileMode.Open, FileAccess.Read,FileShare.None))
{
List<T> list = new List<T>();
list = (List<T>)sf.Deserialize(fStream);
return list;
}
}
这是我内心感受的堆栈跟踪:
en System.Xml.XmlTextReaderImpl.Throw(Exception e)zh System.Xml.XmlTextReaderImpl.Throw(String res,String arg)zh System.Xml.XmlTextReaderImpl.ThrowUnclosedElements()zh System.Xml.XmlTextReaderImpl.ParseAttributes()zh System.Xml.XmlTextReaderImpl.ParseElement()zh System.Xml.XmlTextReaderImpl.ParseDocumentContent()zh System.Xml.XmlTextReaderImpl.Read()zh System.Xml.XmlTextReader.Read()zh System.Runtime.Serialization.Formatters.Soap.SoapParser.ParseXml()
en System.Runtime.Serialization.Formatters.Soap.SoapParser.Run()zh System.Runtime.Serialization.Formatters.Soap.ObjectReader.Deserialize(HeaderHandler handler,ISerParser serParser)en System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(流 serializationStream,HeaderHandler handler)en System.Runtime.Serialization.Formatters.Soap.SoapFormatter.Deserialize(流 serializationStream)en Solution1.MainWindow.ReturnListOfPersistentFile [T](String Filename)en c:\ users \ u201114160 \ documents \ visual studio 2010 \ Projects \ Solution1 \ Solution1 \ MainWindow.xaml.cs:línea99en Solution1.MainWindow.Do()c:\ users \ u201114160 \ documents \ visual studio 2010 \ Projects \ Solution1 \ Solution1 \ MainWindow.xaml.cs:línea45
en Solution1.MainWindow..ctor()zh c:\ users \ u201114160 \ documents \ visual studio 2010 \ Projects \ Solution1 \ Solution1 \ MainWindow.xaml.cs:línea36
它让我疯狂,有些想法?抛出这个异常是因为“ReturnListOfPersistentFile”泛型方法,但是为什么?
提前致谢。
解答: 我试图使用SOAP格式化程序序列化通用列表,但SOAP格式化程序不支持通用列表。
我在forums.microsoft.com.遇到了这个帖子。让我引用微软员工所说的话:
我们已决定不再为Whidbey的SoapFormatter投入任何重要的新功能
谢谢大家。