C# 试图读取bat文件,但它给了我一个错误
我有2个bat文件一个customer.bat和另一个barista.bat,我可以从customer.bat读取,但是我不能从barista.bat读取,它给了我这个错误 “System.InvalidCastException:'无法将'System.Collections.Generic.List'类型的对象强制转换为'System.Collections.Generic.List'。' " 我有2个bat文件一个customer.bat和另一个barista.bat,我可以从customer.bat读取,但是我不能从barista.bat读取,它给了我这个错误 “System.InvalidCastException:'无法将'System.Collections.Generic.List'类型的对象强制转换为'System.Collections.Generic.List'.' 这应该行得通,因为有些人是顾客。但是我不知道发生了什么事 program.csC# 试图读取bat文件,但它给了我一个错误,c#,C#,我有2个bat文件一个customer.bat和另一个barista.bat,我可以从customer.bat读取,但是我不能从barista.bat读取,它给了我这个错误 “System.InvalidCastException:'无法将'System.Collections.Generic.List'类型的对象强制转换为'System.Collections.Generic.List'。' " 我有2个bat文件一个customer.bat和另一个barista.bat,我可以从custom
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 ");
}
}
}
}
}
}
}
名称空间控制台EAPP1
{
班级计划
{
静态int ShowMenu()
{
Console.WriteLine(“选择;”);
Console.WriteLine(--------------”;
Console.WriteLine(“1.新客户”);
Console.WriteLine(“2.Login”);
Console.WriteLine(“3.退出”);
int choice=Convert.ToInt32(Console.ReadLine());
回报选择;
}
静态void Main(字符串[]参数)
{
int-choice=0;
做
{
choice=ShowMenu();
开关(选择)
{
案例1:
AddCustomer();
打破
案例2:
登录();
打破
}
}
while(选项!=3);
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
私有静态void AddCustomer()
{
Console.Clear();
客户c=新客户();
控制台。写(“名称:”);
c、 Name=Console.ReadLine();
控制台。写(“姓氏:”);
c、 姓氏=Console.ReadLine();
控制台。写入(“ID:”);
c、 ID=Convert.ToInt32(Console.ReadLine());
控制台。写入(“电子邮件地址:”);
c、 EmailAddress=Console.ReadLine();
控制台。写入(“家庭地址:”;
c、 HomeAddress=Console.ReadLine();
控制台。写入(“手机号码:”);
c、 MobNumber=Convert.ToInt32(Console.ReadLine());
控制台。写入(“用户名:”;
c、 Username=Console.ReadLine();
控制台。写入(“密码:”);
c、 Password=Console.ReadLine();
//控制台。写(“咖啡点:”);
//c.CoffeeP=Convert.ToInt32(Console.ReadLine());
ManagerOrder.Instance.AddCustomer(c);
ManagerOrder.Instance.SaveChanges();
}
公共静态void登录()
{
常量字符串文件名\u BARISTA=“BARISTA.dat”;
常量字符串FILENAME\u CUSTOMER=“CUSTOMER.dat”;
Console.Clear();
控制台。写入(“用户名:”;
字符串username=Console.ReadLine();
控制台。写入(“密码:”);
字符串密码=Console.ReadLine();
列出pccustomer=Customer.ReadData(文件名\客户);
Customer.CustomerCreatedInstances=pccustomer;
IEnumerable cu=pccustomer.Where(customers=>customers.Username==Username&customers.Password==Password);
字符串validUsername;
字符串有效密码;
foreach(PCCustomer中的var c)
{
validUsername=c.用户名;
validPassword=c.密码;
if(validUsername==用户名和validPassword==密码)
{
Console.WriteLine(“客户登录”);
}
其他的
{
Console.WriteLine(“咖啡师登录”);
列出myBarista=Barista.ReadData(文件名\u Barista);
Barista.BaristaCreatedInstances=myBarista;
IEnumerable barist=myBarista.Where(barista=>barista.Username==Username&barista.Password==Password);
foreach(myBarista中的var ba)
{
validUsername=ba.Username;
validPassword=ba.密码;
if(validUsername==用户名和validPassword==密码)
{
Console.WriteLine(“咖啡师登录”);
}
其他的
{
控制台。写入线(“工作”);
}
}
}
}
}
}
}
咖啡师
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;
//}
}
}
公共级咖啡师
{
公共静态列表;
公共字符串名称{get;set;}
公共字符串姓氏{get;set;}
公共int ID{get;set;}
公共字符串电子邮件地址{get;set;}
公共字符串家庭地址{get;set;}
公共整数编号{get;set;}
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
公共咖啡师()
{
Name=string.Empty;
姓氏=字符串。空;
ID=0;
EmailAddress=string.Empty;
HomeAddress=string.Empty;
MobNumber=0;
Username=string.Empty;
Password=string.Empty;
}
公共咖啡师(字符串n、字符串s、整数id、字符串ea、字符串ha、整数mobn、字符串用户、字符串通行证)
{
n=名称;
s=姓氏;
id=id;
ea=电子邮件地址;
ha=家庭地址;
mobn=MobNumber;
用户=用户名;
pass=密码;
}
公共静态void WriteData(列表对象序列化,字符串文件名\u BARISTSA)
{
WriteData(objectToSerialize,FILENAME_BARISTSA);
}
公共静态列表读取数据(字符串文件名\u BARISTA)
{
返回Instance.ReadData(文件名\u BARISTA);
}
//公共登录(字符串用户名、字符串密码)
//{
//this.u=user1;
//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;
}
}
公共类实例
{
静止的