无法获取要反序列化的类列表
本文关键字:列表 反序列化 获取 | 更新日期: 2023-09-27 18:30:35
我有一个包含这段代码的类
public static ExpenseItemRoster ReadFromFile(string filename)
{
List<ExpenseItemRoster> deserializedExp = null;
try
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream("D:''myExpensesUpdated.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
deserializedExp = (List<ExpenseItemRoster>)formatter.Deserialize(stream);
stream.Close();
}
catch (Exception ex)
{
Console.WriteLine("Unable to deserialize Expenses: {0}", ex.Message);
}
return null;
}
}
}
当我尝试在我的主程序中提出它时,我得到这个:
Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[Payroll.ExpenseItemRoster]'.
Object reference not set to an instance of an object.
我几乎可以肯定这是因为我没有正确返回反序列化列表,但我不完全确定我是否需要将反序列化列表转换为字符串,或者如何做到这一点。
编辑:这是它被称为...
using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Linq;
using System.Text;
namespace Payroll
{
class PayrollProgram
{
static void Main(string[] args)
{
//Create new expenses list
ExpenseItemRoster expenses = new ExpenseItemRoster();
//Add original expense items to the expenses list
expenses.Add(new ExpenseItem("Airline ticket", new DateTime(2010, 3, 7), 365.98m, "myDiscover", "Chicago sales meeting", "Business class"));
expenses.Add(new ExpenseItem("Dinner", new DateTime(2010, 1, 5), 489m, "cash", "Local sales call", "Client Sam Adams"));
expenses.Add(new ExpenseItem("Conference fee", new DateTime(2009, 2, 1), 600m, "myDiscover", "Chicago conference", "Prepaid conference fee"));
expenses.Add(new ExpenseItem("airline ticket", new DateTime(2009, 12, 20), 1350m, "myVisa", "Emergency customer call", "First class fare"));
expenses.Add(new ExpenseItem("Lunch", new DateTime(2010, 5, 1), 77m, "myVisa", "Chicago sales meeting", ""));
expenses.Add(new ExpenseItem("Dry cleaning", new DateTime(2010, 5, 2), 88m, "cash", "Chicago sales meeting", ""));
expenses.Add(new ExpenseItem("Airline Ticket", new DateTime(2010, 11, 12), 8765m, "myVisa", "Australian sales meeting", "first class travel"));
expenses.Add(new ExpenseItem("Taxi", new DateTime(2011, 3, 13), 101m, "cash", "New York sales manager meeting", "Trip to airport"));
expenses.Add(new ExpenseItem("Entertainment", new DateTime(2011, 3, 12), 202m, "cash", "New York sales manager meeting", "Airport closed"));
ExpenseItem e2 = new ExpenseItem("Conference fee", new DateTime(2009, 2, 1), 600m, "myDiscover", "Chicago conference", "Prepaid conference fee");
try
{
expenses.Add(e2);
}
catch (Exception ex)
{
Console.WriteLine("{0}: {1}", ex.Message, e2.ToString());
}
ExpenseItem e1 = new ExpenseItem("Lunch", new DateTime(2010, 5, 1), 77m, "myVisa", "Chicago sales meeting", "");
expenses.Delete(e1);
ExpenseItem find = expenses.Find(e1);
if (find == null)
Console.WriteLine("Not Found: {0}", e1.ToString());
else
Console.WriteLine("Found: {0}", find.ToString());
try
{
expenses.WriteToFile("D:''myExpensesUpdated.bin");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
ExpenseItemRoster expensesUpdated = ExpenseItemRoster.ReadFromFile("D:''myExpensesUpdated.bin");
foreach (var e in expensesUpdated) Console.WriteLine("Updated: {0}", e.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
我知道返回 null 没有任何用处,但是我收到错误"无法将类型'字符串'转换为'工资单.费用项目名册'"
我需要以某种方式将反序列化的名单转换为可用的东西。
您正在将filename
字符串写入文件。我不明白你为什么期望将其反序列化为List<ExpenseItemRoster>
会起作用。
您可能想做的是:
- 摆脱硬编码的文件名并使用
filename
代替它。 - 添加类型为
List<ExpenseItemRoster>
的参数以WriteToFile()
并对其进行序列化。 - 将
ReadFromFile()
的返回类型更改为List<ExpenseItemRoster>
,并实际return
反序列化列表。
您正在序列化文件名(字符串),然后您希望它返回给您一些列表?如果以后要获取列表,则需要序列化列表。