“无法将”System.String“类型的对象强制转换为”Payroll.EIR“类型”

时间:2012-04-08 17:41:04

标签: c#

好吧,我有一个类“EIR”,这是一个基于它的基类“ExpenseReport”的List:

    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投射到Payroll.EIR这是一个类?有没有办法将序列化字符串转换为类?

1 个答案:

答案 0 :(得分:1)

您的问题已经在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;            
}            

你确定默默地捕获所有异常并将消息写入控制台是个好主意吗?