public static Dictionary<int, SAIUserControlState> StatesFromJson(string json, Control control) { Dictionary<int, SAIUserControlState> states = null; try { var objs = JsonConvert.DeserializeAnonymousType(json, new { Workspaces = new[] { new { Workspace = 0, Value = new List<StateObject>() } } }); var controls = GetChildControls(control).ToList(); var grouped = ( from c in controls from w in objs.Workspaces from s in w.Value where s.Key == c.Name let z = new { Workspace = w, Control = c, State = s } group z by z.Workspace.Workspace into g select g); states = new Dictionary<int, SAIUserControlState>(); foreach (var group in grouped) { SAIUserControlState state = new SAIUserControlState(); foreach (var g in group) { if (g.Control is CustomObjectListView) { var scripts = new List<DatabaseClass>(); var arr = JArray.FromObject(g.State.Value); var containers = arr.ToObject<List<ScriptContainer>>(); foreach (var container in containers) { Type sType = Type.GetType(container.TypeName); try { var dc = (DatabaseClass)JObject.FromObject(container.Value).ToObject(sType); scripts.Add(dc); } catch (Exception ex) { Console.WriteLine(ex); } } state.Controls.Add(g.Control, scripts); } else { state.Controls.Add(g.Control, g.State.Value); } } states.Add(group.Key, state); } } catch (Exception ex) { Console.WriteLine(ex); } return states; }
public static Dictionary <int, SAIUserControlState> StatesFromJson(string json, Control control) { Dictionary <int, SAIUserControlState> states = null; try { var objs = JsonConvert.DeserializeAnonymousType(json, new { Workspaces = new[] { new { Workspace = 0, Value = new List <StateObject>() } } }); var controls = GetChildControls(control).ToList(); var grouped = ( from c in controls from w in objs.Workspaces from s in w.Value where s.Key == c.Name let z = new { Workspace = w, Control = c, State = s } group z by z.Workspace.Workspace into g select g); states = new Dictionary <int, SAIUserControlState>(); foreach (var group in grouped) { SAIUserControlState state = new SAIUserControlState(); foreach (var g in group) { if (g.Control is CustomObjectListView) { var scripts = new List <DatabaseClass>(); var arr = JArray.FromObject(g.State.Value); var containers = arr.ToObject <List <ScriptContainer> >(); foreach (var container in containers) { Type sType = Type.GetType(container.TypeName); try { var dc = (DatabaseClass)JObject.FromObject(container.Value).ToObject(sType); scripts.Add(dc); } catch (Exception ex) { Console.WriteLine(ex); } } state.Controls.Add(g.Control, scripts); } else { state.Controls.Add(g.Control, g.State.Value); } } states.Add(group.Key, state); } } catch (Exception ex) { Console.WriteLine(ex); } return(states); }