Вот простой пример того, что я хочу сделать (после этого идет реальный код):
Код: Выделить всё
dict["some key"] = "some value";
// Use reflection to do a SetValue dict.entries[0] to "some new value"
Printing out dict["some key"] should now display "some new value"
Вот код:
Код: Выделить всё
const BindingFlags BINDING_FLAGS_ALL = (BindingFlags) 65535;
Dictionary dict = new Dictionary();
dict["key1"] = "original value";
object entry = ((Array) dict.GetType().GetField("entries", BINDING_FLAGS_ALL).GetValue(dict)).GetValue(0);
object key = entry.GetType().GetField("key", BINDING_FLAGS_ALL).GetValue(entry);
FieldInfo val_field = entry.GetType().GetField("value", BINDING_FLAGS_ALL);
object val = val_field.GetValue(entry);
Console.WriteLine($"original value at '{key}' == '{val}'");
val_field.SetValue(entry, "changed value!");
val = val_field.GetValue(entry);
Console.WriteLine($"new value at '{key}' == '{val}'");
val = dict[(string) key];
Console.WriteLine($"dict[{key}] == '{val}'");
Код: Выделить всё
original value at 'key1' == 'original value'
new value at 'key1' == 'changed value!'
dict[key1] == 'original value'
дд
Подробнее здесь: https://stackoverflow.com/questions/786 ... dictionary