Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从文件中反序列化数据并将其转换回原始对象?_C#_Serialization - Fatal编程技术网

C# 如何从文件中反序列化数据并将其转换回原始对象?

C# 如何从文件中反序列化数据并将其转换回原始对象?,c#,serialization,C#,Serialization,由于某种原因,当我试图编写代码将数据反序列化回一个对象时,我不断收到错误“无法将类型方法转换为Form1.Account”。我试着做一个明确的演员阵容,但这也不起作用。有人能告诉我我做错了什么吗。正如您从下面的屏幕截图中看到的,我的对象是帐户 这是我想要反序列化并转换回对象帐户的序列化数据(我理解正确吗?) 这是最初创建帐户(对象)的代码,也是我序列化数据的代码 private int _nextIndex = 0; List<Account> accou

由于某种原因,当我试图编写代码将数据反序列化回一个对象时,我不断收到错误“无法将类型方法转换为Form1.Account”。我试着做一个明确的演员阵容,但这也不起作用。有人能告诉我我做错了什么吗。正如您从下面的屏幕截图中看到的,我的对象是帐户

这是我想要反序列化并转换回对象帐户的序列化数据(我理解正确吗?)

这是最初创建帐户(对象)的代码,也是我序列化数据的代码

        private int _nextIndex = 0;
    List<Account> accounts = new List<Account>();

    const string FILENAME = "Data.ser";
    FileStream outFile = new FileStream(FILENAME,
        FileMode.Create, FileAccess.Write);
    BinaryFormatter bFormatter = new BinaryFormatter();

            if (checkingRadioButton1.Checked == true)
            {
                _nextIndex++;
                transactionLabel3.Text = "Checking Account: #" + _nextIndex + " created with a starting balance of $" + balance;
                accountTextBox1.Text = "" + _nextIndex;
                accounts.Add(new CheckingAccount(balance)
                {
                    AccountID = _nextIndex
                    ,
                    Student = isStudent
                });
                bFormatter.Serialize(outFile, accounts);
            }
            else if (savingsRadioButton2.Checked == true)
            {
                _nextIndex++;
                transactionLabel3.Text = "Savings Account: #" + _nextIndex + "  created with a starting balance of $" + balance;
                accountTextBox1.Text = "" + _nextIndex;
                accounts.Add(new SavingsAccount(balance)

                {
                    AccountID = _nextIndex
                   ,
                    Senior = isSenior
                });
                bFormatter.Serialize(outFile, accounts);
            }
private int\u nextIndex=0;
列表帐户=新列表();
常量字符串FILENAME=“Data.ser”;
FileStream outFile=新FileStream(文件名,
FileMode.Create、FileAccess.Write);
BinaryFormatter bFormatter=新的BinaryFormatter();
if(checkingRadioButton1.Checked==true)
{
_nextIndex++;
transactionLabel3.Text=“支票账户:#“+_nextIndex+”创建时起始余额为$”+余额;
accountTextBox1.Text=“+”下一个索引;
账户。添加(新支票账户(余额)
{
AccountID=_nextIndex
,
学生
});
b格式化。序列化(输出文件、帐户);
}
else if(savingsRadioButton2.Checked==true)
{
_nextIndex++;
transactionLabel3.Text=“储蓄账户:#”+_nextIndex+”创建时起始余额为“$”+余额;
accountTextBox1.Text=“+”下一个索引;
账户。添加(新储蓄账户(余额)
{
AccountID=_nextIndex
,
老年人
});
b格式化。序列化(输出文件、帐户);
}
下面的代码我也序列化了数据,最后我尝试反序列化数据,但是我一直得到上面提到的错误

            if (depositRadioButton3.Checked == true)
            {
                selectedAccount.DepositFunds(amount);
                bFormatter.Serialize(outFile, accounts);
                transactionLabel3.Text = $"Account: #{selectedAccount.AccountID} You made a deposit of ${amount}";
            }
            else if (withdrawRadioButton4.Checked == true)
            {
                var balance = selectedAccount.GetAvailableBalanceForAccount(accountID);
                if (selectedAccount.HasAvailableFunds && amount <= balance)
                {
                    selectedAccount.WithdrawFromAccount(amount);
                    bFormatter.Serialize(outFile, accounts);
                    transactionLabel3.Text = $"Account: #{selectedAccount.AccountID} You made a withdrawal of ${amount}";
                    outFile.Close();
                    FileStream inFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
                    while (inFile.Position < inFile.Length)
                    {
                        accounts = (Account)bFormatter.Deserialize,(inFile);
                        accounts.

                    }
                }
if(depositRadioButton3.Checked==true)
{
所选账户存款(金额);
b格式化。序列化(输出文件、帐户);
transactionLabel3.Text=$“账户:{selectedAccount.AccountID}您存入了${amount}”;
}
else if(撤销RadioButton4.Checked==true)
{
var余额=selectedAccount.GetAvailableBalanceForAccount(accountID);
如果(selectedAccount.HasAvailableFunds&&amount您似乎已经序列化了一个列表或数组-很可能是一个
列表
。我这样说是因为它说“Count:1”

这里有两种方法:

  • 试试看:
    var list=(list)bFormatter.Deserialize(infle);
    (并将其存储在某个地方,可能会返回到
    accounts
    字段中)

  • 或者,检查对象:
    objectobj=bFormatter.Deserialize(infle);Console.WriteLine(obj.GetType().FullName);

  • 注意:根据代码中的
    List accounts=new List();
    ,1可能可以正常工作

    我还应该补充一点,我通常建议不要使用
    BinaryFormatter
    。当您对类型进行版本设置时,它非常脆弱且不友好,我已经看到很多人在使用它时遇到了问题。它也很容易意外地将意外对象吸入序列化图中,特别是通过事件。还有其他系列我会推荐的序列化工具-一系列xml、json、protobuf等序列化程序,它们是可信的,并且众所周知是无麻烦的。就我个人而言,我倾向于protobuf net,但是…再一次:偏见。

    您似乎序列化了一个列表或数组-很可能是一个
    列表。我之所以这样说是因为它说“Count:1”

    这里有两种方法:

  • 试试看:
    var list=(list)bFormatter.Deserialize(infle);
    (并将其存储在某个地方,可能会返回到
    accounts
    字段中)

  • 或者,检查对象:
    objectobj=bFormatter.Deserialize(infle);Console.WriteLine(obj.GetType().FullName);

  • 注意:根据代码中的
    List accounts=new List();
    ,1可能可以正常工作


    我还应该补充一点,我通常建议不要使用
    BinaryFormatter
    。当您对类型进行版本设置时,它非常脆弱且不友好,我已经看到很多人在使用它时遇到了问题。它也很容易意外地将意外对象吸入序列化图中,特别是通过事件。还有其他系列我推荐的序列化工具-一系列xml、json、protobuf等序列化程序,这些序列化程序是可信的,并且众所周知是无麻烦的。就我个人而言,我倾向于protobuf net,但是…再一次:bias.

    Typo.应该是
    bFormatter.Deserialize(infle)
    不带逗号。是的,已删除,但仍然是相同的错误。必须是在我尝试其他方法修复问题时插入的。用于反序列化的类必须与用于序列化的类完全相同。如果修改了代码,则必须在反序列化之前返回并序列化数据@jdweng不,我的代码没有更改。我唯一更改的是我尝试反序列化数据的代码。我使用的类没有更改。如果您删除了逗号,请告诉我们您现在遇到的错误类型。Typo.应该是
    bFormatter.deserialize(infle)
    不带逗号。是的,已删除,但仍然是相同的错误。必须是在我尝试其他方法修复问题时插入的。用于反序列化的类必须与用于序列化的类完全相同。如果是