通过密钥和&获取哈希表obj改变其公共财产

时间:2016-10-04 14:42:37

标签: c# hashtable

首先,我声明一个哈希表及其值。哈希表条目的关键是GUID,值是具有几个字符串值的对象。

    Guid g = Guid.NewGuid();
    Hashtable hash = new Hashtable();
    InstallationFiles instFiles = new InstallationFiles(string1, string2, string3);
    hash.Add(g, instFiles);
    //...add many other values with different GUIDs...

我的目标是为用户提供编辑字符串1,字符串2,字符串3的可能性。长话短说,我处在一个可以获得需要编辑的条目“GUID g”的位置:

   public void edit() 
   {
         //here I retrieve the GUID g of the item which has to be edited:
         object objectHash = item.Tag;
         //here i loop through all hash entries to find the editable one:
         foreach(DictionaryEntry de in hash)
         {
            if(de.Key.ToString() == objectHash) 
            {
            //here I would like to access the selected entry and change string1 - 
           //the line below is not working.

            hash[de.Key].string1 = "my new value"; 
            }
         }

   }

如何使这条线路正常工作?

    hash[de.Key].string1 = "my new value"; 

1 个答案:

答案 0 :(得分:1)

使用@Configuration public class MvcConfig extends WebMvcConfigurerAdapter { @Bean public YourOwnInterceptor yourOwnInterceptor() { return new YourOwnInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); registry.addInterceptor(yourOwnInterceptor()). addPathPatterns("/<your-url-to-intercept>/**"); } 代替Dictionary<Guid, InstallationFiles>

UPD。你可以用它。

HashTable

好的,解释:

因为Hashtable不是泛型类型,所以它包含对键和值的引用作为对象。

这就是为什么当您访问您的值 (hash[de.Key] as InstallationFiles).string1 = "asdasd" 时,您引用了hashtable[mykey]。要使其成为您的类型(Object)的参考,您必须参考InstallationFiles&#34;获取&#34;引用Object&#34;。我是我用的样本&#34; InstallationFiles&#34;运营商这样做。