示例#1
0
        public static void Load(object data, RegistryKey key)
        {
            foreach (var propertyInfo in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                RegistrySaveAttribute valueAttr = GetAttribute <RegistrySaveAttribute>(propertyInfo);
                if (valueAttr != null)
                {
                    if (valueAttr.IsObject)
                    {
                        RegistryKey subkey = key.OpenSubKey(valueAttr.Name);
                        if (subkey != null)
                        {
                            // Try to get an existing value of the property.
                            object value = propertyInfo.GetValue(data, null);

                            // In the property has no value, and the subkey exists, create a new value object.
                            if (value == null)
                            {
                                value = Activator.CreateInstance(propertyInfo.PropertyType);
                                propertyInfo.SetValue(data, value, null);
                            }

                            // Load the value members (no matter whether the value existed or has been created).
                            Load(value, subkey);

                            // Close the subkey.
                            subkey.Close();
                        }
                        else
                        {
                            // If there's no subkey, erase the object.
                            propertyInfo.SetValue(data, null, null);
                        }
                    }
                    else
                    {
                        // Load the simple value.
                        string strValue = key.GetValue(valueAttr.Name) as string;
                        object value    = strValue != null?InvariantConverter.FromString(strValue, propertyInfo.PropertyType) : valueAttr.DefaultValue;

                        propertyInfo.SetValue(data, value, null);
                    }
                }
            }
        }
示例#2
0
        private static void Save(object data, RegistryKey root, string keyName)
        {
            RegistryKey currentKey = root.CreateSubKey(keyName);

            if (currentKey != null)
            {
                foreach (var propertyInfo in data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    RegistrySaveAttribute valueAttr = GetAttribute <RegistrySaveAttribute>(propertyInfo);
                    if (valueAttr != null)
                    {
                        object value = propertyInfo.GetValue(data, null);

                        if (valueAttr.IsObject)
                        {
                            if (value == null)
                            {
                                root.DeleteSubKey(keyName + '\\' + valueAttr.Name);
                            }
                            else
                            {
                                Save(value, root, keyName + '\\' + valueAttr.Name);
                            }
                        }
                        else
                        {
                            currentKey.SetValue(valueAttr.Name, InvariantConverter.ToString(value));
                        }

                        //if (propertyInfo.PropertyType.GetInterface(IList.GetType().ToString()) != null)
                    }
                }

                // All done.
                currentKey.Close();
            }
        }