我有2个bat文件,一个是customer.bat,另一个是barista.bat,我可以从customer.bat中读取,但是我不能从barista.bat中读取,这给了我这个错误
“ System.InvalidCastException:'无法转换类型为'System.Collections.Generic.List 1[ConsoleApp1.Customer]' to type 'System.Collections.Generic.List
1 [ConsoleApp1.Barista]的对象。”
“
我有2个bat文件,一个是customer.bat,另一个是barista.bat,我可以从customer.bat中读取,但是我不能从barista.bat中读取,这给了我这个错误
“ System.InvalidCastException:'无法转换类型为'System.Collections.Generic.List 1[ConsoleApp1.Customer]' to type 'System.Collections.Generic.List
1 [ConsoleApp1.Barista]'的对象。”
它应该可以正常工作,因为它与customer.bat一样,但是我不知道会发生什么事
program.cs
namespace ConsoleApp1
{
class Program
{
static int ShowMenu()
{
Console.WriteLine("Choose; ");
Console.WriteLine("---------");
Console.WriteLine("1. New Customer. ");
Console.WriteLine("2. Login. ");
Console.WriteLine("3. Quit.");
int choice = Convert.ToInt32(Console.ReadLine());
return choice;
}
static void Main(string[] args)
{
int choice = 0;
do
{
choice = ShowMenu();
switch (choice)
{
case 1:
AddCustomer();
break;
case 2:
Login();
break;
}
}
while (choice != 3);
Console.WriteLine("Press any key to quit ");
Console.ReadKey();
}
private static void AddCustomer()
{
Console.Clear();
Customer c = new Customer();
Console.Write("Name: ");
c.Name = Console.ReadLine();
Console.Write("Surname: ");
c.Surname = Console.ReadLine();
Console.Write("ID: ");
c.ID = Convert.ToInt32(Console.ReadLine());
Console.Write("Email Address: ");
c.EmailAddress = Console.ReadLine();
Console.Write("Home Address: ");
c.HomeAddress = Console.ReadLine();
Console.Write("Mobile Number: ");
c.MobNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Username: ");
c.Username = Console.ReadLine();
Console.Write("Password: ");
c.Password = Console.ReadLine();
// Console.Write("Coffee Points: ");
// c.CoffeeP = Convert.ToInt32(Console.ReadLine());
ManagerOrder.Instance.AddCustomer(c);
ManagerOrder.Instance.SaveChanges();
}
public static void Login()
{
const string FILENAME_BARISTA = "barista.dat";
const string FILENAME_CUSTOMER = "customer.dat";
Console.Clear();
Console.Write("Username: ");
string username = Console.ReadLine();
Console.Write("Password: ");
string password = Console.ReadLine();
List<Customer> pCustomer = Customer.ReadData<Customer>(FILENAME_CUSTOMER);
Customer.CustomerCreatedInstances = pCustomer;
IEnumerable<Customer> cu = pCustomer.Where(customers => customers.Username == username & customers.Password == password);
string validUsername;
string validPassword;
foreach (var c in pCustomer)
{
validUsername = c.Username;
validPassword = c.Password;
if (validUsername == username && validPassword == password)
{
Console.WriteLine("Customer login");
}
else
{
Console.WriteLine("Barista Login");
List<Barista> myBarista = Barista.ReadData<Barista>(FILENAME_BARISTA);
Barista.BaristaCreatedInstances = myBarista;
IEnumerable<Barista> barist = myBarista.Where(barista => barista.Username == username & barista.Password == password);
foreach (var ba in myBarista)
{
validUsername = ba.Username;
validPassword = ba.Password;
if (validUsername == username && validPassword == password)
{
Console.WriteLine("barista Login");
}
else
{
Console.WriteLine("worng ");
}
}
}
}
}
}
}
Barista.cs
public class Barista
{
public static List<Barista> BaristaCreatedInstances;
public string Name { get; set; }
public string Surname { get; set; }
public int ID { get; set; }
public string EmailAddress { get; set; }
public string HomeAddress { get; set; }
public int MobNumber { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public Barista()
{
Name = string.Empty;
Surname = string.Empty;
ID = 0;
EmailAddress = string.Empty;
HomeAddress = string.Empty;
MobNumber = 0;
Username = string.Empty;
Password = string.Empty;
}
public Barista(string n, string s, int id, string ea, string ha, int mobn ,string user, string pass)
{
n = Name;
s = Surname;
id = ID;
ea = EmailAddress;
ha = HomeAddress;
mobn = MobNumber;
user = Username;
pass = Password;
}
public static void WriteData(List<Barista> objectToSerialize, string FILENAME_BARISTSA)
{
Instance<Barista>.WriteData(objectToSerialize, FILENAME_BARISTSA);
}
public static List<Barista> ReadData<ba>(string FILENAME_BARISTA)
{
return Instance<Barista>.ReadData<Barista>(FILENAME_BARISTA);
}
//public Login(String username, string password)
//{
// this.u = user1;
// this.p = pass1;
//}
}
}
Instance.cs
public class Instance<T>
{
static Stream stream;
static IFormatter formatter = new BinaryFormatter();
public static void WriteData(List<T> objectToSerialize, string FILENAME_CUSTOMER)
{
stream = new FileStream(FILENAME_CUSTOMER, FileMode.Append, FileAccess.Write);
formatter.Serialize(stream, objectToSerialize);
Console.WriteLine("Write done");
stream.Close();
}
//Stream file = File.Open("FILENAME_CUSTOMER" + Customer.CustomerCreatedInstances, FileMode.Open);
//BinaryFormatter bfor = new BinaryFormatter();
//Customer cust = (Customer)BinaryFormatter.Deserialize(stream);
//stream.Close()
public static List<T> ReadData<T>(string filePath)
{
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
Object deserializedObject = formatter.Deserialize(stream);
Console.WriteLine("Read done");
stream.Close();
return (List<T>)deserializedObject;
}
}
我希望从文件中读取内容,并且登录名可以使用,但是却给我一个错误 https://ibb.co/HdBbCLN
答案 0 :(得分:0)
我已经解决了Opewix是正确的问题,我正在创建客户对象而不是咖啡师对象
谢谢