C# 在何处初始化属性。使用应用程序设置对象?

C# 在何处初始化属性。使用应用程序设置对象?,c#,properties,settings,nullreferenceexception,C#,Properties,Settings,Nullreferenceexception,我使用Properties>Settings对话框将Settings.Settings文件添加到我的项目中。我创建了一个新的配置元素,其类型为自定义类型。自动生成代码的输出如下: [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] public global::Moslem.Model.Confi

我使用
Properties>Settings
对话框将
Settings.Settings
文件添加到我的项目中。我创建了一个新的配置元素,其类型为自定义类型。自动生成代码的输出如下:

[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::Moslem.Model.Configuration.ApplicationConfiguration AppConfig
{
    get 
    {
        return ((global::Moslem.Model.Configuration.ApplicationConfiguration)(this["AppConfig"]));
    }
}
到目前为止,还可以。默认情况下,
ApplicationScopedSettingAttribute
的所有设置都是只读的。因此,我不能简单地在应用程序的其他地方启动它。显然,任何对
ApplicationConfiguration
对象的任何属性的使用都会抛出
NullReferenceException

我的问题是:

  • 是否有一种标准方法来启动所有具有应用范围的对象
  • 简单地编写自己的构造函数并启动对象安全吗?(因为
    Properties.Settings
    自动生成为部分)

  • 我不知道第二个问题的答案,但这是我从数据库查询中初始化设置值的方式。我认为这将符合运行时通常期望的生命周期

    首先,为自动生成的设置类创建匹配的分部类实现:

    [SettingsProvider(typeof(CustomSettingsProvider))]
    internal sealed partial class Settings : ApplicationSettingsBase 
    {
        public Settings()
        {
            CustomSettingsProvider.UpdateCustomSettings += delegate(object sender, EventArgs e)
            {
                this.Reload();
            };
        }
    }
    
    SettingsProvider属性标识作为设置源的类

    事件处理程序是可选的。如果值异步更改,则可以通过此方法强制在运行时重新加载设置

    以下是设置Provider的外观:

    public partial class CustomSettingsProvider : System.Configuration.SettingsProvider, System.Configuration.IApplicationSettingsProvider
    {
        // raise this event to cause the settings to be reloaded at runtime after initialization
        public static event EventHandler UpdateCustomSettings;
    
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            SettingsPropertyValueCollection result = new SettingsPropertyValueCollection();
    
            // the collection identifies properties to initialize
            foreach (SettingsProperty property in collection)
            {
                SettingsPropertyValue spv = new SettingsPropertyValue(property);
    
                // determine value...
    
                spv.PropertyValue = value;
                result.Add(spv);
            }
    
            return result;
        }
    }
    
    基类中有一些可重写的方法,但中的大部分工作是在GetPropertyValues方法中完成的。collection参数标识需要初始化的属性,为每个属性查找值,或提供一些默认值以避免NullReferenceException

    我们正在使用此技术进行授权设置。我们在应用程序中有几个模块,每个模块都有一组不同的授权设置,但都绑定到同一个CustomSettingsProvider

    用于读取设置的数据库代码与CustomSettingsProvider类分开,但CustomSettingsProvider.GetPropertyValues方法可通过单例接口访问。我们在CustomSettingsProvider上添加了一个公共静态方法来引发UpdateCustomSettings事件(由于事件和方法是静态的,因此没有事件“sender”,但无论如何都不使用事件参数)


    当数据库查询完成并读入设置时,将对CustomSettingsProvider类调用静态方法,这将引发UpdateCustomSettings事件,反过来,settings类将调用其重载方法。默认的重新加载实现调用GetPropertyValues方法,使用数据库中的新数据重新初始化所有设置值。

    我不知道第二个问题的答案,但这是我从数据库查询初始化设置值的方式。我认为这将符合运行时通常期望的生命周期

    首先,为自动生成的设置类创建匹配的分部类实现:

    [SettingsProvider(typeof(CustomSettingsProvider))]
    internal sealed partial class Settings : ApplicationSettingsBase 
    {
        public Settings()
        {
            CustomSettingsProvider.UpdateCustomSettings += delegate(object sender, EventArgs e)
            {
                this.Reload();
            };
        }
    }
    
    SettingsProvider属性标识作为设置源的类

    事件处理程序是可选的。如果值异步更改,则可以通过此方法强制在运行时重新加载设置

    以下是设置Provider的外观:

    public partial class CustomSettingsProvider : System.Configuration.SettingsProvider, System.Configuration.IApplicationSettingsProvider
    {
        // raise this event to cause the settings to be reloaded at runtime after initialization
        public static event EventHandler UpdateCustomSettings;
    
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            SettingsPropertyValueCollection result = new SettingsPropertyValueCollection();
    
            // the collection identifies properties to initialize
            foreach (SettingsProperty property in collection)
            {
                SettingsPropertyValue spv = new SettingsPropertyValue(property);
    
                // determine value...
    
                spv.PropertyValue = value;
                result.Add(spv);
            }
    
            return result;
        }
    }
    
    基类中有一些可重写的方法,但中的大部分工作是在GetPropertyValues方法中完成的。collection参数标识需要初始化的属性,为每个属性查找值,或提供一些默认值以避免NullReferenceException

    我们正在使用此技术进行授权设置。我们在应用程序中有几个模块,每个模块都有一组不同的授权设置,但都绑定到同一个CustomSettingsProvider

    用于读取设置的数据库代码与CustomSettingsProvider类分开,但CustomSettingsProvider.GetPropertyValues方法可通过单例接口访问。我们在CustomSettingsProvider上添加了一个公共静态方法来引发UpdateCustomSettings事件(由于事件和方法是静态的,因此没有事件“sender”,但无论如何都不使用事件参数)


    当数据库查询完成并读入设置时,将对CustomSettingsProvider类调用静态方法,这将引发UpdateCustomSettings事件,反过来,settings类将调用其重载方法。默认的重新加载实现调用GetPropertyValues方法,使用数据库中的新数据重新初始化所有设置值。

    我不知道第二个问题的答案,但这是我从数据库查询初始化设置值的方式。我认为这将符合运行时通常期望的生命周期

    首先,为自动生成的设置类创建匹配的分部类实现:

    [SettingsProvider(typeof(CustomSettingsProvider))]
    internal sealed partial class Settings : ApplicationSettingsBase 
    {
        public Settings()
        {
            CustomSettingsProvider.UpdateCustomSettings += delegate(object sender, EventArgs e)
            {
                this.Reload();
            };
        }
    }
    
    SettingsProvider属性标识作为设置源的类

    事件处理程序是可选的。如果值异步更改,则可以通过此方法强制在运行时重新加载设置

    以下是设置Provider的外观:

    public partial class CustomSettingsProvider : System.Configuration.SettingsProvider, System.Configuration.IApplicationSettingsProvider
    {
        // raise this event to cause the settings to be reloaded at runtime after initialization
        public static event EventHandler UpdateCustomSettings;
    
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            SettingsPropertyValueCollection result = new SettingsPropertyValueCollection();
    
            // the collection identifies properties to initialize
            foreach (SettingsProperty property in collection)
            {
                SettingsPropertyValue spv = new SettingsPropertyValue(property);
    
                // determine value...
    
                spv.PropertyValue = value;
                result.Add(spv);
            }
    
            return result;
        }
    }
    
    基类中有一些可重写的方法,但中的大部分工作是在GetPropertyValues方法中完成的。collection参数标识需要初始化的属性,为每个属性查找值,或提供一些默认值以避免NullReferenceException

    我们正在使用此技术进行授权设置。我们在应用程序中有几个模块,每个模块都有一组不同的授权设置,但都绑定到同一个CustomSettingsProvider

    用于读取设置的数据库代码与CustomSettingsProvider类分开,但CustomSettingsProvider.GetPropertyValues方法可以通过