【发布时间】:2020-07-26 07:54:03
【问题描述】:
我有一个简单的控制台应用程序,其中包含 Program.cs 中 3 个用户的列表。在 Program.cs 中,我想调用 FileOperations.cs 中的一个方法。
首先我的Program.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using CsvHelper;
namespace GimpiesConsoleOOcsvListUI
{
class Program
{
static void Main(string[] args)
{
// Create user default instances
User user0 = new User("Beheer", "beheer@gimpies.nl", "123");
User user1 = new User("Inkoop", "inkoop@gimpies.nl", "123");
User user2 = new User("Verkoop", "verkoop@gimpies.nl", "123");
// List of users (with a list you can add, get and remove items in the list)
List<User> users = new List<User>();
users.Add(user0);
users.Add(user1);
users.Add(user2);
// Create login instance
LoginManager loginMgr = new LoginManager();
Start:
// Welcome message
Console.WriteLine("Welcome to GimpiesTerminal! Choose 1 to login or 2 to register:");
// Get input from user
string input = Console.ReadLine();
bool successfull = false;
while (!successfull)
{
if(input == "1")
{
Console.WriteLine("Enter your username:");
string username = Console.ReadLine();
Console.WriteLine("Enter your password:");
string password = Console.ReadLine();
foreach (User user in users)
{
if (username == user.UserName && password == user.PassWord)
{
Console.WriteLine("You have successfully logged in !!!");
Console.ReadLine();
successfull = true;
break;
}
}
if (!successfull)
{
Console.WriteLine("Your username or password is incorrect, try again !!!");
}
}
else if (input == "2")
{
// Get user input
Console.WriteLine("Enter your username:");
string username = Console.ReadLine();
Console.WriteLine("Enter your email:");
string email = Console.ReadLine();
Console.WriteLine("Enter your password:");
string password = Console.ReadLine();
// Create fresh instance to save input in memory
User user = new User(username, email, password);
// Adds the user to the excisting list
users.Add(user);
successfull = true;
goto Start;
}
else if (input == "3")
{
// Here I want to call the method from FileOperations.cs to write the List here to a CSV file.
}
else
{
Console.WriteLine("Try again !!!");
break;
}
}
// // Loop over stored users within instances inside the list where users are added
// foreach (User user in users)
// {
// if(loginMgr.Login(user))
// {
// // Login successfull
// Console.WriteLine("Login user " + user.UserName);
// }
// else
// {
// // Not successfull
// }
// }
}
}
}
这是我的FileOperations.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using CsvHelper;
namespace GimpiesConsoleOOcsvListUI
{
public class FileOperations
{
// Handles CRUD within CSV files and able to save them
public void SaveToCSV(User user)
{
using (var mem = new MemoryStream())
using (var writer = new StreamWriter(mem))
using (var csvWriter = new CsvWriter(writer))
{
csvWriter.Configuration.Delimiter = ";";
csvWriter.Configuration.HasHeaderRecord = true;
csvWriter.Configuration.AutoMap<User>();
csvWriter.WriteHeader<User>();
csvWriter.WriteRecords(user);
writer.Flush();
var result = Encoding.UTF8.GetString(mem.ToArray());
Console.WriteLine(result);
}
}
}
}
我在FileOperations.cs 的第 16 行收到以下错误:
StreamWriter 编写器
参数 1:无法从 System.IO.StreamWriter 转换为 CsvHelper.ISerializer
第 23 行:
用户用户
参数 1:无法从 GimpiesConsoleOOcsvListUI.User 转换为 System.Collections.IEnumerable
我是 C# 的新手,所以我希望你不介意它是否很容易解决问题! :-)
【问题讨论】:
-
我不明白你的代码流程;您有一个要保存的用户列表,并且您有一个类,该类的方法仅将单个用户保存到内存流中(因此不保存到磁盘中),然后立即将内存流丢弃,因此任何地方都不会保存任何内容。可能值得仔细阅读您的代码并评论算法,以便您拥有一套本地语言的步骤来实现目标,然后您可以评估评论下的 c# 是否真的按照评论所说的那样做; “先用英文(/你的母语思维语言)写,然后翻译成c#”
-
不要使用
goto
标签: c# .net list csv csvhelper