如何访问 Hashtable 中的结构 属性?

How can i access Structure property that is inside Hashtable?

我有以下代码将修改结构内的 属性,并且该结构位于散列 table 内。哈希 table 中的每个项目都有 (Int) 数据类型的键和 (struct Bag) 的键,这是我的代码:

struct Bag {

  string apple_type;
  string orange_type;

};

 // Make a new hashtable that will have a key of (int) data type, value of (Bag)
   public static Hashtable bags = new Hashtable();

然后我有一个方法可以从数据库中读取数据,读取行并添加,只要有一行,它就会向散列中添加一个项目 (bag(object))table:

public void initHashtbl(){


OleDbConnection db_connector = new OleDbConnection(connection_string);

            // Open connection to oracle
            db_connector.Open();

            OleDbCommand sql_commander = new OleDbCommand(sql_statement, db_connector);

            OleDbDataReader data_Reader = sql_commander.ExecuteReader();


            // Read data from sql db server and store it in memory ...


            Bag tempBag = new Bag();

            // row counter used for list view
            int row_counter = 0;

            while (data_Reader.Read()) { // Keep reading data from memory until there is no row

      tempBag.apple_type = data_Reader[0].ToString();

      bags.Add(row_counter, tempBag);
      row_counter++;
}


   for(int bag_item=0;bag_item < bags.Count;bag_item++){

         // Get orange type value from another method that uses another sql statement from another table in db ..
     ((bag) bags[bag_item]).orange_type = getOrangeType(((bag) bags[bag_item]).apple_type);    
}

}

如果我想访问已经在散列 table 中的结构 属性,我以后如何访问它?

编辑: 我收到此错误: "Cannot modify the result of an unboxing conversion."

这段代码让我阅读了一个项目:

        string orangeType = ((Bag) bags[0]).orange_type;

要修改一个项目,我认为你必须创建一个新的 Bag 并像这样替换现有的:

        bags[0] = newBag;

如果您尝试修改 HashTable 中包的属性,则会收到上面报告的错误。

Dictionary will not allow me to modify that directly

Hashtable 也不会。这与 HashtableDictionary 无关。问题是您的 "value" 在任何一种情况下都是值类型,因此您 不能 直接在集合中修改它。如果你真的需要一个结构,那么你必须创建一个值并将它back 进入哈希表:

bags.Add(1, new Bag() {apple_type="apple1",orange_type="orange1"});

//((Bag)bags[1]).apple_type="apple2";
var bag = (Bag)bags[1];
bag.apple_type = "appple2";
bags[1] = bag;

但是可变结构通常是不好的,所以我会在 第一次 时间获得结构的正确值(而不是在初始加载循环之外修改它):

// row counter used for list view
int row_counter = 0;

while (data_Reader.Read()) { // Keep reading data from memory until there is no row
{
    var appleType = data_Reader[0].ToString();
    Bag tempBag = new Bag() {
        apple_type = appleType,
        orange_type = getOrangeType(appleType)
        };
    bags.Add(row_counter, tempBag);
    row_counter++;
}

或使用 class.

请注意,无论您使用 Hashtable 还是 Dictionary,相同的代码都以完全相同的方式工作。

此外,由于您的 "key" 只是一个递增的数字(与值无关),您也可以使用 List<Bag> 并按索引访问项目。

由于值类型的工作方式,装箱的 Bag 是原件的副本,"unboxing" 它通过转换回 Bag 创建了另一个副本。 From the C# language spec:

When a value of a value type is converted to type object, an object instance, also called a “box,” is allocated to hold the value, and the value is copied into that box. Conversely, when an object reference is cast to a value type, a check is made that the referenced object is a box of the correct value type, and, if the check succeeds, the value in the box is copied out.

修改副本无论如何都不会改变原件,所以允许它没有多大意义。

更正:

要修复错误并允许修改 Bag 的哈希表,您应该将值类型更改为引用类型:

    public class Bag
    {
        public string apple_type { get; set; }
        public string orange_type { get; set; }
    };