C# 在散列中存储和检索对象属性
C# Storing and Retrieving Object Properties in a Hash
我将创建需要能够按时间排序的日期和字符串变量。此信息集合将需要在脚本运行时添加和删除条目。根据我的研究,似乎一种可行的方法是创建一个哈希表,该哈希表使用键创建时间的纪元值,并带有关联的自定义 class,该自定义具有 DateTime 和 String 信息的属性。这将允许我通过键对散列进行排序,并且我可以根据需要在散列表中添加和删除项目。我创建的自定义class就是下面的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;
}
}
有了这个 class 我可以创建一个循环来为 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[=22 等属性时=] 我收到一个错误。我一直在尝试将值打印为 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)
我能够通过一个使用字符串作为键和自定义 class 作为值的对象来实现我的目标。有一项让我在开始时遇到一些麻烦,那就是我需要加载 System.Collections.Generic.
using System.Collections.Generic;
首先,我将字典初始化为字符串键,并将执行器 class 作为值。
Dictionary<string, Executor> ExecutionDict = new Dictionary<string, Executor>();
接下来,我需要将键定义为唯一的值,因此我创建了一个与特定时间和描述相关联的变量,以了解即使时间相关联也不会重复。 exe 变量存储了 Executor class 的一个实例,其中包含在代码前面定义的变量。最后一行将条目添加到字典中。我把这段代码放在一个循环中,所以我每次都能继续添加具有新值的相同变量。
While (condition) {
Executor exe = new Executor(tempTime,item.Name,item.Executable);
exeKey = exeTimeEpoch.ToString() + item.Name;
ExecutionDict.Add(exeKey,exe);
}
最后,我创建了一个遍历字典的循环来查找所有实例并打印出值以测试变量的创建。
foreach(KeyValuePair<string,Executor> temp in ExecutionDict) {
Console.WriteLine("For the Key {0} the Name is {1} and the Name is {2} and the Name is {3}", temp.Key, temp.Value.Name, temp.Value.Executable, temp.Value.StartTime);
}
我将创建需要能够按时间排序的日期和字符串变量。此信息集合将需要在脚本运行时添加和删除条目。根据我的研究,似乎一种可行的方法是创建一个哈希表,该哈希表使用键创建时间的纪元值,并带有关联的自定义 class,该自定义具有 DateTime 和 String 信息的属性。这将允许我通过键对散列进行排序,并且我可以根据需要在散列表中添加和删除项目。我创建的自定义class就是下面的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;
}
}
有了这个 class 我可以创建一个循环来为 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[=22 等属性时=] 我收到一个错误。我一直在尝试将值打印为 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)
我能够通过一个使用字符串作为键和自定义 class 作为值的对象来实现我的目标。有一项让我在开始时遇到一些麻烦,那就是我需要加载 System.Collections.Generic.
using System.Collections.Generic;
首先,我将字典初始化为字符串键,并将执行器 class 作为值。
Dictionary<string, Executor> ExecutionDict = new Dictionary<string, Executor>();
接下来,我需要将键定义为唯一的值,因此我创建了一个与特定时间和描述相关联的变量,以了解即使时间相关联也不会重复。 exe 变量存储了 Executor class 的一个实例,其中包含在代码前面定义的变量。最后一行将条目添加到字典中。我把这段代码放在一个循环中,所以我每次都能继续添加具有新值的相同变量。
While (condition) {
Executor exe = new Executor(tempTime,item.Name,item.Executable);
exeKey = exeTimeEpoch.ToString() + item.Name;
ExecutionDict.Add(exeKey,exe);
}
最后,我创建了一个遍历字典的循环来查找所有实例并打印出值以测试变量的创建。
foreach(KeyValuePair<string,Executor> temp in ExecutionDict) {
Console.WriteLine("For the Key {0} the Name is {1} and the Name is {2} and the Name is {3}", temp.Key, temp.Value.Name, temp.Value.Executable, temp.Value.StartTime);
}