对编程相当陌生。我只是想不通如何让它反向工作。我有一个保存方法和一个打开方法。节省:
IDictionary<string, IDictionary<string, object>> pluginStates = new Dictionary<string, IDictionary<string, object>>();
signaller.RaiseSaveRequest(pluginStates); <--goes out and gets packed plugins
//loop through plugins to get values and types
//holds all of the plugin arrays
Dictionary<string, object> dictProjectState = new Dictionary<string, object>();
foreach (KeyValuePair<string,IDictionary<string,object>> plugin in pluginStates)
{
//holds jsonRepresented values
Dictionary<string, object> dictJsonRep = new Dictionary<string, object>();
//holds object types
Dictionary<string, object> dictObjRep = new Dictionary<string, object>();
object[] arrayDictHolder = new object[2]; //holds all of the dictionaries
string pluginKey = plugin.Key;
IDictionary<string, object> pluginValue = new Dictionary<string, object>();
pluginValue = plugin.Value;
foreach (KeyValuePair<string, object> element in pluginValue)
{
string jsonRepresentation = JsonConvert.SerializeObject(element);
object objType = element.Value.GetType().ToString();
dictJsonRep.Add(element.Key, jsonRepresentation);
dictObjRep.Add(element.Key, objType);
}
arrayDictHolder[0] = dictJsonRep;
arrayDictHolder[1] = dictObjRep;
dictProjectState.Add(pluginKey, arrayDictHolder);
}
JsonSerializer serializer = new JsonSerializer();
using (StreamWriter sw = new StreamWriter(strPathName))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, dictProjectState);
}
因此,当有人保存时,事件处理程序会发出并获取每个插件的 packedState,并将其添加到字典 pluginStates 中。然后,我遍历 pluginStates 中的每个插件,将值的键和 json 字符串版本添加到 1 个字典,将键、对象类型添加到另一个字典,将这两个字典添加到数组中,然后打包一个包含pluginKey 和每个插件的数组。推理:在反序列化时,我遇到了从 JArray 到类型 DataTable 以及字典中的其他类型的问题,这些类型在打开时被传递回插件以解压。我试图弄清楚如何扭转这一点,以便当用户打开项目时,我有 dictProjectState 并且需要通过代码将它一直带回以最终得到 1 个包含插件的字典。我如何在公开场合镜像这个保存?谢谢!