[英]C# Properties.Settings
我在理解Properties.Settings
语法的结构时遇到了麻烦。 我知道这等效于使用完全限定的名称MyProject.Properties.Settings
,并且Settings
是一个类。 但是什么是Properties
? 另一个名称空间? 命名空间MyProject
和命名空间MyProject.Properties
之间是什么关系?
顺便说一下,我已经对此进行了详尽的研究,并且对Properties.Settings
所有组件都非常熟悉。 我已经阅读了数十篇文章,但是没有找到这个基本问题的答案。
首先,从MSDN回答关于名称空间是...的问题:
namespace关键字用于声明包含一组相关对象的范围。 您可以使用名称空间来组织代码元素并创建全局唯一类型。
从MSDN上另一个有用的页面:
... .NET Framework使用名称空间来组织其许多类,如下所示:
System.Console.WriteLine("Hello World!");
System是一个名称空间,而Console是该名称空间中的类。
虽然名称空间可能有多种用途,但总而言之,声明名称空间为您提供了一种组织类的方法。 本身并没有真正的“事物”。
如果在上面的示例中, 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
,在此属性存储在名为“ PropertyValues”的SettingsPropertyValueCollection
。
我无法进一步深入,但是SettingsPropertyValueCollection
包含一个Hashtable
和一个ArrayList
(用于存储键及其关联值)。
它还有一个用于检索值的索引器,该索引器基本上只是查找您请求的值。
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-> Properties-> Settings.settings-> Settings.Designer.cs中打开生成的文件,可以轻松找到所有这些信息。 这个名称空间和类没有什么特别的,除了VS自动生成它们。
您可以轻松地在另一个具有相同功能的名称空间中编写自己的类。 但这会挫败VS自动为您生成它的意义。 :)
放在一边。 System.Configuration.ApplicationSettingsBase
类确实为您免费添加了一些有价值的东西,例如INotifyPropertyChanged
实现。
如果我正确理解您的问题,
MyProject.Properties就像您的项目的容器,用于管理对项目独立需要的所有静态引用(语言文件/图像/数据连接等)。 它就像每个项目的预定义名称空间一样,以便您可以轻松知道要在哪里寻找静态资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.