首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Properties.Settings

C# Properties.Settings
EN

Stack Overflow用户
提问于 2014-07-02 10:20:10
回答 3查看 3.2K关注 0票数 1

我在理解Properties.Settings语法的结构时遇到了问题。我知道这等同于使用完全限定名MyProject.Properties.Settings,并且Settings是一个类。但是什么是Properties呢?另一个名称空间?namespace MyProject和namespace MyProject.Properties之间有什么关系

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

EN

回答 3

Stack Overflow用户

发布于 2014-07-02 10:45:46

首先,为了回答您关于名称空间是什么的问题...来自MSDN

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

来自MSDN上另一个有用的页面

....NET框架使用名称空间来组织它的许多类,如下所示:

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

System是一个命名空间,Console是该命名空间中一个类。

尽管名称空间可能有多种用途,但简而言之,声明名称空间为您提供了一种组织类的方法。他们自己并没有真正的“东西”。

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

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

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

代码语言:javascript
复制
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,因此您也可以获得该类内容的一个实例。

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

代码语言:javascript
复制
/// <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,它将属性存储在名为"PropertyValues“的SettingsPropertyValueCollection中。

我不能进一步深入,但是SettingsPropertyValueCollection包含一个Hashtable和一个ArrayList (用于存储键及其关联值)。

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

代码语言:javascript
复制
 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];
     }
 } 
票数 6
EN

Stack Overflow用户

发布于 2014-07-02 10:40:53

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

您可以很容易地在另一个名称空间中编写您自己的类,该名称空间做同样的事情。但这会破坏让VS自动为你生成它的意义。:)

只是一点小插曲。System.Configuration.ApplicationSettingsBase类确实为您免费添加了一些有价值的东西,比如INotifyPropertyChanged的实现。

票数 4
EN

Stack Overflow用户

发布于 2014-07-02 10:33:29

如果我没理解错你的问题,

MyProject.Properties就像你的项目的容器,用来管理你所有的静态引用(语言文件/图像/数据连接等)。你的项目的独立需求。它类似于为每个项目预定义名称空间,以便您可以轻松地知道在何处查找静态资源。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24521914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档