C# 属性设置

本文关键字:设置 属性 | 更新日期: 2023-09-27 18:11:14

我在理解Properties.Settings语法的结构时遇到了麻烦。我知道这等效于使用完全限定名MyProject.Properties.Settings并且Settings是一个类。但是Properties是什么?另一个命名空间?命名空间MyProject和命名空间MyProject.Properties之间的关系是什么?

顺便说一下,我已经对此进行了详尽的研究,并且非常熟悉Properties.Settings的所有组件。我已经阅读了数十篇文章,但我还没有遇到这个基本问题的答案。

C# 属性设置

首先,回答你关于命名空间是什么的问题......来自 MSDN:

命名空间关键字用于声明包含一组相关对象的作用域。可以使用命名空间来组织代码元素并创建全局唯一类型。

从 MSDN 上的另一个有用页面:

。.NET Framework 使用命名空间来组织其许多类,如下所示:

System.Console.WriteLine("Hello World!");

系统是一个命名空间,控制台是该命名空间中的一个类。

虽然命名空间可能有多种用途,但简而言之,声明命名空间提供了一种组织类的方法。本身并没有真正的"东西"。

如果使用上面的示例,Console 类是 System 命名空间中的唯一内容,并且您从项目中删除了 Console 类(假设您创建了该类(,则 System 命名空间将不复存在,因为它不再有意义。

<小时 />

Settings是一个类,位于 MyProject.Properties 命名空间中。我必须假设这只是他们选择给它的命名空间名称 - 该命名空间中似乎没有任何其他类,它扩展的类驻留在System.Configuration命名空间中。

Settings类如下所示。基本上,它只是在您第一次使用静态Settings.Default访问它时创建一个自己的新实例。

namespace SampleWinforms.Properties
{
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 
    {
        private static Settings defaultInstance =
            ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
        
        public static Settings Default
        {
            get { return defaultInstance; }
        }
    }
}

但是里面没有非静态的东西,那么你在创建实例是什么?它扩展了ApplicationSettingsBase因此您也将获得该类内容的实例。

里面有很多东西,但其中一件事是允许您存储和检索值的索引器(假设键已经存在;我没有看到在运行时添加新设置的方法,但如果在编译时定义了设置,那么您可以在运行时读取该值并覆盖该值(。

/// <summary>
/// Gets or sets the value of the specified application settings property.
/// </summary>
/// 
/// <returns>
/// If found, the value of the named settings property; otherwise, null.
/// </returns>
/// <param name="propertyName">A <see cref="T:System.String"/> containing the name of the property to access.</param><exception cref="T:System.Configuration.SettingsPropertyNotFoundException">There are no properties associated with the current wrapper or the specified property could not be found.</exception><exception cref="T:System.Configuration.SettingsPropertyIsReadOnlyException">An attempt was made to set a read-only property.</exception><exception cref="T:System.Configuration.SettingsPropertyWrongTypeException">The value supplied is of a type incompatible with the settings property, during a set operation.</exception><exception cref="T:System.Configuration.ConfigurationErrorsException">The configuration file could not be parsed.</exception><filterpriority>2</filterpriority>
public override object this[string propertyName]
{
  get
  {
    if (!this.IsSynchronized)
      return this.GetPropertyValue(propertyName);
    lock (this)
      return this.GetPropertyValue(propertyName);
  }
  set
  {
    SettingChangingEventArgs e = new SettingChangingEventArgs(propertyName, this.GetType().FullName, this.SettingsKey, value, false);
    this.OnSettingChanging((object) this, e);
    if (e.Cancel)
      return;
    base[propertyName] = value;
    this.OnPropertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
  }
}

这个类反过来扩展了SettingsBase,这是属性存储在一个名为"属性值"的SettingsPropertyValueCollection中的地方。

我无法进一步深入,但SettingsPropertyValueCollection包含HashtableArrayList(用于存储密钥及其关联值(。

它还有一个用于检索值的索引器,它基本上只是查找你请求的值。

 public SettingsPropertyValue this[string name]
 { 
     get
     {
         object pos = _Indices[name];
         if (pos == null || !(pos is int))
             return null; 
         int ipos = (int)pos;
         if (ipos >= _Values.Count)
             return null;
         return (SettingsPropertyValue)_Values[ipos];
     }
 } 

严格来说,MyProject.Properties是一个自动生成的命名空间。里面的类的默认名称是 MyProject.Properties.Settings ,它继承了System.Configuration.ApplicationSettingsBase 。通过检查解决方案资源管理器顶部的Show All Files按钮,然后在 MyProject->属性->设置.设置->设置.Designer.cs中打开生成的文件,可以轻松找到所有这些。这个命名空间和类没有什么特别之处,除了 VS 自动生成它们。

您可以轻松地在另一个命名空间中编写自己的类,该命名空间执行相同的操作。但这会破坏让VS自动生成它的意义。:)

只是有点题外话。System.Configuration.ApplicationSettingsBase类确实免费为您添加了一些有价值的东西,例如INotifyPropertyChanged的实现。

如果我正确理解你的问题,

MyProject.Properties 就像一个容器,用于管理项目独立需要的所有静态引用(语言文件/图像/数据连接等(。 它就像每个项目的预定义命名空间,以便您可以轻松地知道在哪里查找静态资源。