【发布时间】:2020-06-09 23:50:26
【问题描述】:
我将创建需要能够按时间排序的日期和字符串变量。此信息集合需要在脚本运行时添加和删除条目。根据我的研究,似乎一种可行的方法是创建一个哈希表,该哈希表使用密钥创建一个时间的纪元值,并具有关联的自定义类,该类具有 DateTime 和 String 信息的属性。这将允许我按键对哈希进行排序,并且可以根据需要从哈希表中添加和删除项目。我创建的自定义类是下面的Executor。
public class Executor {
public DateTime StartTime { get; set; }
public string Name { get; set; }
public string Executable { get; set; }
public Executor(DateTime starttime, string name, string executable) {
StartTime = starttime;
Name = name;
Executable = executable;
}
}
使用这个类,我可以创建一个循环来创建实例并将其添加到哈希表中,以实现 while 循环所允许的各种情况。下面的代码正在创建需要存储在哈希表中的信息,并接受一个列表,该列表提供生成哈希的规则输入。为简单起见,从下面的示例中删除了一些定义时间和循环列表的代码。
static void CreateScriptScehdule(List<Script> scriptList) {
// Declare and initialize variables
Hashtable htExecutionList = new Hashtable();
// Code to create a list of the times and manage the item properties
...
...
// Create a loop to define the exeuction times of the script between the start and stop time.
// use a tempTime to compare to the stop time
while (DateTime.Compare(tempTime,timeStop) < 0) {
// Create an object for the executable based on the script rules
Executor exe = new Executor(tempTime,item.Name,item.Executable);
// Add the executable object to the hashtable htExecutionList
double exeTimeEpoch = ttoe(tempTime);
exeKey = exeTimeEpoch.ToString() + item.Name;
htExecutionList.Add(exeKey,exe);
}
// Loop through the hashtable to print out the stored information to verify the creation for debugging
foreach (DictionaryEntry s in htExecutionList) {
//Console.WriteLine(s.Value);
}
}
当我检查 s.Value 的值时,我收到了值 ReceiveConfigFile.Executor。这让我相信对象 Executor 是从 ReceiveConfigFile 脚本中存储的,但是当我尝试检索诸如 StartTime 或 Name 之类的属性时,我收到一个错误.我一直在尝试将值打印为 s.Value.Name 以为我可以获得对象属性。我收到的错误是:
error CS1061: 'System.Collection.DictionaryEntry' does not contain a definition for 'Namel accepting a first argument of type 'System.Collections.DictionaryEntry' could be found (are you missing a using directive or an assembly reference)
【问题讨论】: