Exemplo n.º 1
0
        /// <summary>
        /// 引数 <paramref name="context"/> から指定したキー(<paramref name="key"/>)のデータを、
        /// 指定した型(<typeparamref name="T"/> )で取得する
        /// </summary>
        /// <typeparam name="T">
        /// 取得するデータの型
        /// </typeparam>
        /// <param name="context">
        /// 現在のアプリケーションの設定情報を格納した、コンテキストオブジェクト
        /// </param>
        /// <param name="key">
        /// 取得するデータのキー
        /// </param>
        /// <returns>
        /// 指定したキー(<paramref name="key"/>)に該当する、<typeparamref name="T"/> 型のデータ
        /// 取得できない場合はNULL
        /// </returns>
        private T GetDataFromSettingsContext <T>(SettingsContext context, string key)
            where T : class
        {
            // NULLチェック
            if (context == null || key == null)
            {
                // 引数がNULLの場合、データを取得できないためNULLを返却
                return(null);
            }

            // キーに対応するデータを取得
            object data = context.ContainsKey(key) ? context[key] : null;

            // 取得したデータが指定された型である場合はその型にキャストして返却
            // そうでない場合はNULLを返却
            return(data is T ? (T)data : null);
        }
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            string sectionName = string.Empty;
            var    values      = new SettingsPropertyValueCollection();

            if (context.ContainsKey("GroupName"))
            {
                sectionName = context["GroupName"] as string;
            }

            foreach (SettingsProperty setting in collection)
            {
                SettingsPropertyValue value = new SettingsPropertyValue(setting)
                {
                    IsDirty         = false,
                    SerializedValue = this.GetValue(setting, sectionName)
                };
                values.Add(value);
            }

            return(values);
        }
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            string sectionName = string.Empty;

            if (context.ContainsKey("GroupName"))
            {
                sectionName = context["GroupName"] as string;
            }

            foreach (SettingsPropertyValue propval in collection)
            {
                this.SetValue(propval, sectionName);
            }

            try
            {
                this.SettingsFile.Save(Path.Combine(this.GetAppSettingsPath(), this.GetAppSettingsFilename()));
            }
            catch (Exception ex)
            {
                Debug.WriteLine("No se puede guardar El Archivo de Configuracion: " + ex.Message);
            }
        }