C# 如何在C中以Hashvalue的形式访问对象数组#
为什么下面抛出编译错误C# 如何在C中以Hashvalue的形式访问对象数组#,c#,arrays,hash,C#,Arrays,Hash,为什么下面抛出编译错误[]无法应用于对象。(德语的粗略翻译) 我期望entry.Value是一个对象数组,但显然编译器认为它只是一个对象。这里出了什么问题?出现错误是因为DictionaryEntry没有数组作为值的属性。下面是字典入口的结构。您必须使用entry.Value而不是entry.Value[0] // Summary: // Defines a dictionary key/value pair that can be set or retrieved. [
[]无法应用于对象。
(德语的粗略翻译)
我期望
entry.Value
是一个对象数组,但显然编译器认为它只是一个对象。这里出了什么问题?出现错误是因为DictionaryEntry
没有数组作为值的属性。下面是字典入口
的结构。您必须使用entry.Value
而不是entry.Value[0]
// Summary:
// Defines a dictionary key/value pair that can be set or retrieved.
[Serializable]
[ComVisible(true)]
public struct DictionaryEntry
{
public DictionaryEntry(object key, object value);
// Summary:
// Gets or sets the key in the key/value pair.
//
// Returns:
// The key in the key/value pair.
public object Key { get; set; }
//
// Summary:
// Gets or sets the value in the key/value pair.
//
// Returns:
// The value in the key/value pair.
public object Value { get; set; }
}
编辑
要使它工作,你必须投下它。使用以下代码
Registry.SetValue(keyPath,
(string)entry.Key,
((object[])(entry.Value))[0]);
检查Registry.SetValue的正确用法-使用
字典
而不是哈希表
。Hashtable不是强类型,您需要强制转换它。这意味着entrys.Add(entryName,newobject[]{256,RegistryValueKind.DWord})
实际上无效,因为DictionaryEntry不能将数组作为值保存?我以为entry.Value
返回我的对象数组,我可以通过[]
-操作符访问它?我应该使用ArrayList
吗?我已经编辑了我的答案。请参见编辑部分。它的working.entry.Value是一个对象数组,字典只是没有意识到这一点。这就是为什么铸造工作。您可能需要使用System.Collection.Generic.HashSet
,因为它有一个通用实现(允许您为项目选择类型)。
Registry.SetValue(keyPath,
(string)entry.Key,
((object[])(entry.Value))[0]);