残るといっても暗号化されているので大過ないといえばないですが、一応、暗号化したファイルをLocalFolder/RoamingFolderに保存する(LocalFolder/RoamingFolderはアンインストール時に削除される)ものも追加していたら全体がややこしくなってきたので、まとめ直しました。
これを使った設定クラスは、例えば以下のようになります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Settings : SettingsBase | |
{ | |
private Settings() | |
{ } | |
public static Settings Current { get; } = new Settings(); | |
[Roaming] | |
public string Name | |
{ | |
get { return GetValue<string>(); } | |
set { SetValue(value); } | |
} | |
[Roaming] | |
public int Age | |
{ | |
get { return GetValue<int>(); } | |
set { SetValue(value); } | |
} | |
public RaceType Race | |
{ | |
get { return GetValue(RaceType.Human); } | |
set { SetValue(value); } | |
} | |
[Roaming] | |
public Schedule Schedule | |
{ | |
get { return GetValue<Schedule>(); } | |
set { SetValue(value); } | |
} | |
[CryptFile] | |
[Roaming] | |
public Point WayPoint | |
{ | |
get { return GetValue<Point>(); } | |
set { SetValue(value); } | |
} | |
[CryptFile] | |
public Color SymbolColor | |
{ | |
get { return GetValue(Colors.RoyalBlue); } | |
set { SetValue(value); } | |
} | |
[CryptVault] | |
public string SecretPhrase | |
{ | |
get { return GetValue<string>(); } | |
set { SetValue(value); } | |
} | |
} |
- RoamingAttribute : ローミングする。
- CryptVaultAttibute : PasswordVaultに保存する。
- CryptFileAttribute : 暗号化したファイルをLocalFolder/RoamingFolderに保存する。
型は、直接扱えないものはシリアライズして保存するので制限はない、はずですが、シリアライズ特有の問題は起こり得るので、それぞれ対処を。各プロパティの初回アクセス時にリフレクションを使ってどう扱うかを決めますが、結果をキャッシュしたものを次回から使うので、コストはそんなには高くないと思います。
まあ実際はここまでやらずとも、DictionaryにまとめてシリアライズしてRoamingFolderに保存一本でもいいんですが、あるものは使ってみたいという性分なので。
[追記] LocalSettings/RoamingSettingsでサポートされる型
LocalSettings/RoamingSettingsはIDictionaryになっていて、この値の型はObjectですが、実際に保存できるのはLocalSettingsの説明によればWindows Runtime base data typesとされています。
このリストの型をテストしてみたところ(voidを除く)、なぜかUriだけはサポートされてない型という例外が出て駄目でした。一方、ここにはないbyte[]でも保存できるので、どんな型でもシリアライズしてbyte[]にするか、そのままbyte[]に変換すれば、直接出し入れは可能です。
[修正]
例のシングルトンインスタンスの生成方法を修正。C# 6.0の理解が足りてませんでした。
0 コメント :
コメントを投稿