我剛剛查看了 Microsoft 社交網路中有關如何使用應用程式設定的示例,即:
public bool IsUSer(SettingsProperty Setting)
{
bool flag = Setting.Attributes[typeof(UserScopedSettingAttribute)] is UserScopedSettingAttribute;
bool flag1 = Setting.Attributes[typeof(ApplicationScopedSettingAttribute)] is ApplicationScopedSettingAttribute;
if (flag && flag2)
{
throw new ConfigurationErrorsException(SR.GetString("BothScopeAttributes"));
}
if (!flag && !flag2)
{
throw new ConfigurationErrorsException(SR.GetString("NoScopeAttributes"));
}
return flag;
}
這將檢查設定是否既是用戶范圍又是應用程式范圍或兩者都不是。有沒有可能出現這兩種情況。當然,API 一開始就不允許這種情況發生。我們應該檢查這個還是這個例子有點過頭了。我知道這更像是一種討論性的咆哮,但真的,這肯定不會真的發生嗎?
示例鏈接:https ://social.msdn.microsoft.com/Forums/en-US/4c0d2eae-2f0b-41c8-bb60-c4b0ffd3cd0b/how-to-retrieve-usersettings-v-defaultsettings-c-vbe2008?forum= netfxbcl
謝謝丹尼
uj5u.com熱心網友回復:
[UserScopedSetting]并且[ApplicationScopedSetting]是屬性,因此您可以像這樣使用它們:
public class MyUserSettings : ApplicationSettingsBase
{
[UserScopedSetting()]
[DefaultSettingValue("white")]
public Color BackgroundColor
{
get
{
return ((Color)this["BackgroundColor"]);
}
set
{
this["BackgroundColor"] = (Color)value;
}
}
}
(以上代碼的來源)
如果您參考這個問題,您會發現無法防止兩個屬性同時發生。因此,您顯示的代碼實際上可以防止以下兩種情況之一:
1 - 應用兩個屬性:
public class MyUserSettings : ApplicationSettingsBase
{
[UserScopedSetting()]
[ApplicationScopedSetting()]
[DefaultSettingValue("white")]
public Color BackgroundColor
{
get
{
return ((Color)this["BackgroundColor"]);
}
set
{
this["BackgroundColor"] = (Color)value;
}
}
}
2 - 兩個屬性都不適用:
public class MyUserSettings : ApplicationSettingsBase
{
[DefaultSettingValue("white")]
public Color BackgroundColor
{
get
{
return ((Color)this["BackgroundColor"]);
}
set
{
this["BackgroundColor"] = (Color)value;
}
}
}
所以最終它是檢查這兩個屬性中的一個是否被應用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/496266.html
上一篇:如何將變數內容寫入控制臺?
下一篇:從物體框架中的嵌套表中獲取單行
