我在我的配置文件类中添加了一个新的布尔属性。
我似乎无法找到一种方法,但默认情况下它的值是真实的。
Profile.ShowDocumentsNotApplicable
未显式设置为 true 时返回 false...
web.config 内容:
<!-- snip -->
<profile inherits="Company.Product.CustomerProfile">
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
<!-- snap -->
客户资料:
public class CustomerProfile: ProfileBase
{
private bool _showDocumentsNotApplicable = true;
public bool ShowDocumentsNotApplicable
{
get { return Return("ShowDocumentsNotApplicable", _showDocumentsNotApplicable); }
set { Set("ShowDocumentsNotApplicable", value, () => _showDocumentsNotApplicable = value); }
}
private T Return<T>(string propertyName, T defaultValue)
{
try
{
return (T)base[propertyName];
}
catch (SettingsPropertyNotFoundException)
{
return defaultValue;
}
}
private void Set<T>(string propertyName, T setValue, System.Action defaultAction)
{
try
{
base[propertyName] = setValue;
}
catch (SettingsPropertyNotFoundException)
{
defaultAction();
}
}
}