public void ReadSettings() { UserPreference = JsonConvert.DeserializeObject <UserPreference>(Properties.Settings.Default["preference"].ToString()); List <IController> all_ctrls = new List <IController>(); if (UserPreference != null) { Type t = typeof(Helper); string ns = t.Namespace; foreach (string vendor in UserPreference.Vendors) { AccountOption accOpt = (dynamic)UserPreference.GetType().GetProperty(vendor + "Account").GetValue(UserPreference); foreach (var acc in accOpt.Accounts) { if (acc.IsActivate) { string clsName = ns + "." + vendor + "Controller"; Type type = Type.GetType(clsName); IController ctrl = Activator.CreateInstance(type, this) as IController; ctrl.ConnParam = acc; // if some connection is connected, then remain unchanged IController ic = Controllers.FirstOrDefault(x => x.DisplayName == ctrl.DisplayName); if (ic != null) { ic.ConnParam = acc; all_ctrls.Add(ic); } else { all_ctrls.Add(ctrl); } } } // add new controllers foreach (var ctrl in all_ctrls) { IController ic = Controllers.FirstOrDefault(x => x.DisplayName == ctrl.DisplayName); if (ic == null) { Controllers.Add(ctrl); } } // remove out-of-date controllers foreach (var ctrl in Controllers.ToList()) { IController ic = all_ctrls.FirstOrDefault(x => x.DisplayName == ctrl.DisplayName); if (ic == null) { foreach (var symbol in SymbolInActions) { symbol.AppliedControllers.Remove(ctrl); } Controllers.Remove(ctrl); } } } } }