Esempio n. 1
0
        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();
                }
            }
        }