private void Deserialise(object o, Type type, RegistryKey key, bool ignorePropertyErrors) { ISupportInitialize supportInitialize = o as ISupportInitialize; if (supportInitialize != null) { supportInitialize.BeginInit(); } IRegistryKeySerialisable registryKeySerialisable = o as IRegistryKeySerialisable; if (registryKeySerialisable != null && !registryKeySerialisable.BeforeDeserialise(this, key)) { return; } foreach (PropertyInfo propertyInfo in o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(RegistryValueAttribute), true); if (customAttributes.Length > 0) { string name = ((RegistryValueAttribute)customAttributes[0]).Name ?? propertyInfo.Name; object obj = key.GetValue(name); if (obj != null) { try { if (!propertyInfo.PropertyType.IsAssignableFrom(obj.GetType())) { obj = Convert.ChangeType(obj, propertyInfo.PropertyType); } propertyInfo.SetValue(o, obj, (object[])null); } catch (Exception ex) { if (!ignorePropertyErrors) { throw; } else { Trace.TraceError("Error converting property {0}.{1}: {2} Registry key is: {3}, Value is: {4}", (object)type.FullName, (object)propertyInfo.Name, (object)ex.Message, (object)key.Name, (object)obj.ToString()); } } } } else if (propertyInfo.GetCustomAttributes(typeof(RegistryKeyNameAttribute), true).Length > 0) { propertyInfo.SetValue(o, (object)key.Name.Substring(key.Name.LastIndexOf('\\') + 1), (object[])null); } } if (registryKeySerialisable != null) { registryKeySerialisable.AfterDeserialise(this, key); } if (supportInitialize == null) { return; } supportInitialize.EndInit(); }
public void Serialise(object o, RegistryKey parentKey, bool isParentKey) { if (o == null) { throw new ArgumentNullException("o"); } if (parentKey == null) { throw new ArgumentNullException("parentKey"); } PropertyInfo keyNameProperty = RegistrySerialiser.GetKeyNameProperty(o.GetType()); if (keyNameProperty == null) { throw new ArgumentException("At least one property must have a RegistryKeyName attribute."); } RegistryKey key; if (isParentKey) { string subkey = keyNameProperty.GetValue(o, (object[])null) as string; if (subkey == null) { throw new SerializationException("Key name cannot be null."); } key = parentKey.CreateSubKey(subkey); } else { key = parentKey; } try { IRegistryKeySerialisable registryKeySerialisable = o as IRegistryKeySerialisable; if (registryKeySerialisable != null && !registryKeySerialisable.BeforeSerialise(this, key)) { return; } foreach (PropertyInfo propertyInfo in o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(RegistryValueAttribute), true); if (customAttributes.Length > 0) { if (!propertyInfo.PropertyType.IsPrimitive && propertyInfo.PropertyType != typeof(string)) { throw new SerializationException("Cannot serialise an object of type " + propertyInfo.PropertyType.FullName + " to a registry value."); } RegistryValueAttribute registryValueAttribute = (RegistryValueAttribute)customAttributes[0]; string name = registryValueAttribute.Name ?? propertyInfo.Name; object obj = propertyInfo.GetValue(o, (object[])null); if (obj != null && registryValueAttribute.ValueKind != RegistryValueKind.Unknown && !RegistrySerialiser.GetRegistryType(registryValueAttribute.ValueKind).IsAssignableFrom(obj.GetType())) { obj = Convert.ChangeType(obj, RegistrySerialiser.GetRegistryType(registryValueAttribute.ValueKind)); } object defaultValue; RegistrySerialiser.TryGetDefaultValue((ICustomAttributeProvider)propertyInfo, out defaultValue); if (obj != null && obj != defaultValue) { key.SetValue(name, obj); } else { key.DeleteValue(name, false); } } } if (registryKeySerialisable == null) { return; } registryKeySerialisable.AfterSerialise(this, key); } finally { if (isParentKey) { key.Close(); } } }