在这个场景中,程序获取目录中的 xmlfiles。如果每个 xmlfile 已添加到 listToWatch 列表中,则将在第二种方法中对其进行评估。然而, firstMethod 也被循环用于评估每个目录(下面没有写)。
该程序会检测 xml 文件中已添加的所有文件。但是如果程序转到另一个目录(因为 firstMethod 在另一个类中循环),则 listToWatch = new List() 会被传递,擦除之前的 listToWatch 并创建一个新对象。
我想使用相同的对象而不被新列表覆盖。我不能将 listToWatch = new List 放在 secondMethod 中,因为有一个 for 循环,它只会用新对象覆盖 listToWatch。我也不能把它放在 firstMethod 中,因为它需要在 secondMethod 中设置。我也不能把它放在类 Sample 中。我应该把 listToWatch = new List() 放在哪里?
class Sample
{
public static List<string> listToWatch
{
get;
set;
}
public static void firstMethod()
{
string getFiles = Directory.GetFiles(directory, "*.xml");
foreach (string xmlFile in getFiles)
{
secondMethod(xmlFile);
}
}
public static void secondMethod(xmlFile)
{
listToWatch = new List<string>();
foreach (string file in xmlFile)
{
if (listToWatch.Contains(file))
{
sw.WriteLine(file + " is already added!");
}
else
{
listToWatch.add();
}
}
}