“无法将类型为'系统字符串'的对象转换为类型'工资单.EIR'”

本文关键字:类型 转换 对象 工资单 EIR 系统 字符串 | 更新日期: 2023-09-27 18:30:29

好吧,我有一个类"EIR",它是一个基于其基类"费用报告"的列表,如下所示:

    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
{
    [Serializable]
    public class EIR : List<ExpenseItem>
    {   
    public void WriteToFile(string filename)
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                FileStream stream = new FileStream("D:''myExpensesUpdated.bin", FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, filename);
                stream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to serialize Expenses: {0}", ex.Message);
            }
        }
        public static EIR ReadFromFile(string filename)
    {
        EIR iRost = new EIR();
        try
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream("D:''myExpensesUpdated.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
            iRost = (EIR)formatter.Deserialize(stream);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to deserialize Expenses: {0}", ex.Message);
        }
        return iRost;            
    }            
        }
    }
}

这个ReadFromFile部分是我遇到问题的地方:

在主程序中,这部分尝试调用 ReadFromFile 并显示新更新的项目,但我收到错误"无法将类型'System.String'的对象转换为类型'Payroll.EIR'"。

try
        {
            EIR expensesUpdated = EIR.ReadFromFile("D:''myExpensesUpdated.bin");
            foreach (var e in expensesUpdated) Console.WriteLine("Updated: {0}", e.ToString());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

如何让 iRost 投射到工资单.EIR 这是一个类?有没有办法将序列化字符串转换为类?

“无法将类型为'系统字符串'的对象转换为类型'工资单.EIR'”

您的问题已经出在WriteToFile函数中,您实际上是在序列化文件名而不是类。我没有尝试过,但这应该有效:

public void WriteToFile(string filename)
{
    try
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None); // filename parameter instead of fixed path
        formatter.Serialize(stream, this); // class instance instead of filename parameter
        stream.Close();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Unable to serialize Expenses: {0}", ex.Message);
    }
}
public static EIR ReadFromFile(string filename)
{
    EIR iRost = new EIR();
    try
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); // filename parameter instead of fixed path
        iRost = (EIR)formatter.Deserialize(stream);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Unable to deserialize Expenses: {0}", ex.Message);
    }
    return iRost;            
}            

您确定静默捕获所有异常并仅向控制台发送消息是个好主意吗?