为什么以下引发编译错误[] cannot be applied to object.
(德语粗略翻译)?
Hashtable entrys = new Hashtable();
string keyPath = "HKEY_CURRENT_USER\\Software\\Test";
string entryName = "testName";
entrys.Add(entryName, new object[]{256, RegistryValueKind.DWord}); // seems to work
foreach(DictionaryEntry entry in entrys)
{
Registry.SetValue(keyPath,
(string)entry.Key,
entry.Value[0], // error here
entry.Value[1]); // and here
}
我希望entry.Value
是一个对象数组,但显然编译器认为它只是一个对象。这有什么不对?
答案 0 :(得分:2)
错误即将发生,因为DictionaryEntry
没有数组作为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]);