コード例 #1
0
ファイル: Utils.cs プロジェクト: Steex/FB2Formatter
 public static T ReadRegistryValue <T>(RegistryKey key, string name, T defaultValue)
 {
     try
     {
         string strValue = (string)key.GetValue(name, InvariantConverter.ToString(defaultValue));
         return(InvariantConverter.FromString <T>(strValue));
     }
     catch
     {
         return(defaultValue);
     }
 }
コード例 #2
0
ファイル: Utils.cs プロジェクト: Steex/FB2Formatter
 public static IEnumerable <T> ReadRegistryList <T>(RegistryKey key, string baseName)
 {
     for (int index = 1; ; ++index)
     {
         object value = key.GetValue(baseName + index.ToString());
         if (value != null)
         {
             yield return(InvariantConverter.FromString <T>(value.ToString()));
         }
         else
         {
             break;
         }
     }
 }
コード例 #3
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);
                    }
                }
            }
        }