English 中文(简体)
“不能投放系统的物体。 努力打造工资等级。
原标题:"Unable to cast object of type System.String to type Payroll.EIR "
  • 时间:2012-04-08 17:41:03
  •  标签:
  • c#

右上,我有一个“EIR”类,即“Expense Report”这一基类清单:

    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;            
    }            
        }
    }
}

阅读法语区是个问题:

在主要方案中,这一部分试图称作“ReodFromFile”,并显示新更新的项目,但我发现的错误是“无法投放系统的物体”。 努力打造工资类型。

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是哪类? 是否有某种办法将序列式插座转换成一个类别?

最佳回答

你的问题已经在<条码>Write ToFile功能中解决,在这种职能中,你实际上将档案名称而不是分类编号。 我没有尝试,但这项工作应当做到:

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;            
}            

你们是否相信,沉默地抓住所有例外,只是写一个通向独角的讯息?

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签