我需要将其拆分为另一个函数,然后返回以查看它是否包含某些值,但是我不确定如何制作它。例子
exList = getList(ref Path, type);
if(exList.Count > 0){
Do something...
}
我不太确定这部分怎么写...这是我的一半工作
static object getList(ref string Path, string type)
{
exList = new List<Email>();
string[] jsonFileList = Directory.GetFiles(Path, type + "_*.json");
if (jsonFileList.Length > 0)
{
//read json file
foreach (string file in jsonFileList)
{
if (File.Exists(file))
{
exList.Add(JsonConvert.DeserializeObject<ExceptionEmail>(File.ReadAllText(file)));
File.Delete(file);
}
}
}
return something;
}
答案 0 :(得分:0)
检查下面的代码
static List<Email> getList(ref string Path, string type)
{
exceptionList = new List<Email>();
string[] jsonFileList = Directory.GetFiles(Path, type + "_*.json");
if (jsonFileList.Length > 0)
{
//read json file
foreach (string file in jsonFileList)
{
if (File.Exists(file))
{
List.Add(JsonConvert.DeserializeObject<ExceptionEmail>(File.ReadAllText(file)));
File.Delete(file);
}
}
}
return exceptionList;
}
答案 1 :(得分:0)
您的函数需要返回一个列表,并且您需要将返回值保存到变量中。
// not List = ..., List is a class, you need a new instance of a list.
List<Email> list = getList(path, type);
if (list.Count > 0)
{
// Do Something
}
// [...]
private List<Email> getList(string path, string type)
{
List<Email> ret = new List<Email>();
string[] jsonFileList = Directory.GetFiles(path, type + "_*.json");
if (jsonFileList.Length > 0)
{
//read json file
foreach (string file in jsonFileList)
{
if (File.Exists(file))
{
// not List.Add(), List is a class, you need to add to the instance of a list.
ret.Add(JsonConvert.DeserializeObject<ExceptionEmail>(File.ReadAllText(file)));
// File.Delete(file); // The method shouldn't delete files when it's name is getList, delete them after handling in the calling method.
}
}
}
return ret;
}
另外,您应该按照自己的风格进行工作。