public void MergeWith(AbstractSettings secondary) { PropertyDescriptorCollection thisPropertyDescriptors = TypeDescriptor.GetProperties(this); PropertyDescriptorCollection secondaryPropertyDescriptors = TypeDescriptor.GetProperties(secondary); for (int i = 0; i < thisPropertyDescriptors.Count; i++) { PropertyDescriptor thisDescriptor = thisPropertyDescriptors[i]; PropertyDescriptor secondaryDescriptor = secondaryPropertyDescriptors[i]; var defaultValueAttr = thisDescriptor.Attributes.OfType <DefaultValueAttribute>(); if (defaultValueAttr.Count() >= 0) { object defaultValue = defaultValueAttr.First().Value; object thisValue = thisDescriptor.GetValue(this); object secondaryValue = secondaryDescriptor.GetValue(secondary); // our object is default, so copy the secondary value if (Object.Equals(defaultValue, thisValue)) { thisDescriptor.SetValue(this, secondaryValue); } else { // the guy we are merging with is NOT default value if (!Object.Equals(defaultValue, secondaryValue)) { thisDescriptor.SetValue(this, secondaryValue); } } } } }
public void Reload(XElement element) { JavaScriptSerializer serializer = new JavaScriptSerializer(); BrowserSourceSettings = AbstractSettings.DeepClone(BrowserSettings.Instance.SourceSettings); BrowserInstanceSettings = AbstractSettings.DeepClone(BrowserSettings.Instance.InstanceSettings); String instanceSettingsString = element.GetString("instanceSettings"); String sourceSettingsString = element.GetString("sourceSettings"); if (sourceSettingsString != null && sourceSettingsString.Count() > 0) { try { BrowserSourceSettings = serializer.Deserialize <BrowserSourceSettings>(sourceSettingsString); } catch (ArgumentException e) { API.Instance.Log("Failed to deserialized source settings and forced to recreate; {0}", e.Message); } } if (instanceSettingsString != null && instanceSettingsString.Count() > 0) { BrowserInstanceSettings.MergeWith(serializer.Deserialize <BrowserInstanceSettings>(instanceSettingsString)); } }
public void Reload(XElement element) { JavaScriptSerializer serializer = new JavaScriptSerializer(); BrowserSourceSettings = AbstractSettings.DeepClone(BrowserSettings.Instance.SourceSettings); BrowserInstanceSettings = AbstractSettings.DeepClone(BrowserSettings.Instance.InstanceSettings); try { byte[] instanceSettingsBytes = Convert.FromBase64String( element.GetString("instanceSettings")); byte[] sourceSettingsBytes = Convert.FromBase64String( element.GetString("sourceSettings")); string instanceSettingsString = Encoding.UTF8.GetString( instanceSettingsBytes); string sourceSettingsString = Encoding.UTF8.GetString( sourceSettingsBytes); if (sourceSettingsString != null && sourceSettingsString.Count() > 0) { MemoryStream stream = new MemoryStream( Encoding.UTF8.GetBytes(sourceSettingsString)); DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof(BrowserSourceSettings)); BrowserSourceSettings = ser.ReadObject(stream) as BrowserSourceSettings; } //overiding source setting BrowserSourceSettings.Width = Int32.Parse(element.GetString("width")); BrowserSourceSettings.Height = Int32.Parse(element.GetString("height")); BrowserSourceSettings.Url = "about:blank"; BrowserSourceSettings.LoadUrl = element.GetString("url"); if (instanceSettingsString != null && instanceSettingsString.Count() > 0) { BrowserInstanceSettings.MergeWith(serializer.Deserialize <BrowserInstanceSettings>(instanceSettingsString)); try { MemoryStream stream = new MemoryStream( Encoding.UTF8.GetBytes(instanceSettingsString)); DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof(BrowserInstanceSettings)); BrowserInstanceSettings serializedSettings = ser.ReadObject(stream) as BrowserInstanceSettings; BrowserInstanceSettings.MergeWith(serializedSettings); } catch (Exception e) { API.Instance.Log( "Failed to deserialized source settings and forced to recreate"); API.Instance.Log("Exception: {0}", e); } } } catch (Exception e) { API.Instance.Log( "Failed to deserialized source settings and forced to recreate"); API.Instance.Log("Exception: {0}", e); } }