По какой-то причине, когда я пытаюсь написать код для десериализации данных обратно в объект, я получаю сообщение об ошибке «Невозможно преобразовать метод типа в Form1.Account».Я пытался сделать явное приведение, но это тоже не работает.Может кто-то пожалуйста посоветовать, что я делаю не так.Как видно из скриншота ниже, мой объект - это учетные записи
![enter image description here](https://i.stack.imgur.com/nJTHw.jpg)
Это данные, которые были сериализованы, и я хочу десериализовать их и вернуть обратно вучетные записи объектов (правильно ли я понимаю?)
![enter image description here](https://i.stack.imgur.com/TVRuw.jpg)
Это код, в котором изначально была создана учетная запись (объект), а также где я сериализовалdata.
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);
}
Приведенный ниже код также сериализует данные, и в конце я пытаюсь десериализовать данные, но продолжаю получать ошибку, о которой я упоминал выше.
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.
}
}