簡體   English   中英

C# 在散列中存儲和檢索對象屬性

[英]C# Storing and Retrieving Object Properties in a Hash

我將創建日期和字符串變量,我需要能夠按時間排序。 在腳本運行時,需要在此信息集合中添加和刪除條目。 根據我的研究,似乎一種可行的方法是創建一個哈希表,該表使用鍵創建時間的紀元值,並使用具有日期時間和字符串信息屬性的關聯自定義類。 這將允許我按鍵對哈希進行排序,並且我可以根據需要從哈希表中添加和刪除項目。 我創建的自定義類是下面的 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 腳本中存儲,但是當我嘗試檢索諸如StartTimeName 之類的屬性時,我收到一個錯誤。 我一直試圖將值打印為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)

我能夠通過一個使用字符串作為鍵和一個自定義類作為值的對象來實現我的目標。 一項讓我開始遇到麻煩的項目是我需要加載 System.Collections.Generic。

using System.Collections.Generic;

首先,我將字典初始化為字符串鍵,以 Executor 類作為值。

Dictionary<string, Executor> ExecutionDict = new Dictionary<string, Executor>();

接下來,我需要將鍵定義為唯一的值,因此我創建了一個與特定時間和描述相關聯的變量,以了解即使時間相關聯,它也不會重復。 exe 變量存儲了 Executor 類的實例,其中包含在代碼中之前定義的變量。 最后一行將條目添加到字典中。 我在循環中使用了這段代碼,因此我每次都能夠繼續添加具有新值的相同變量。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM