我有 2 个 bat 文件,一个是 customer.bat,另一个是 barista.bat,我可以从 customer.bat 中读取,但我无法从 barista.bat 中读取,它给了我这个错误
“System.InvalidCastException:‘无法将类型为‘System.Collections.Generic.List1[ConsoleApp1.Customer]’的对象转换为键入‘System.Collections.Generic.List
1[ConsoleApp1.Barista] '.'
"
我有 2 个 bat 文件,一个是 customer.bat,另一个是 barista.bat,我可以从 customer.bat 中读取,但我无法从 barista.bat 中读取,它给了我这个错误
“System.InvalidCastException:‘无法将类型为‘System.Collections.Generic.List1[ConsoleApp1.Customer]’的对象转换为键入‘System.Collections.Generic.List
1[ConsoleApp1.Barista] '.'”
它应该可以工作,因为它有一些像 customer.bat 但我不知道出了什么问题
程序.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 ");
}
}
}
}
}
}
}
咖啡师.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;
//}
}
}
实例.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
最佳答案
我已经解决了 Opewix 是正确的问题,我正在创建一个客户对象而不是咖啡师对象
谢谢
关于c# - 试图从 bat 文件中读取但它给了我一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54194232/