C# С;分类和提示不起作用
我在我的应用程序上有一个属性网格,我为它创建了以下类:C# С;分类和提示不起作用,c#,winforms,propertygrid,C#,Winforms,Propertygrid,我在我的应用程序上有一个属性网格,我为它创建了以下类: class NginXConfig { [Browsable(true)] [ReadOnly(true)] [Category("General")] [Description("Defines the number of worker processes. Windows only supports one.")] int _worker_processes
class NginXConfig
{
[Browsable(true)]
[ReadOnly(true)]
[Category("General")]
[Description("Defines the number of worker processes. Windows only supports one.")]
int _worker_processes = 1;
public int worker_processes
{
get { return _worker_processes; }
set { _worker_processes = value; }
}
[Browsable(true)]
[ReadOnly(false)]
[Category("General")]
[Description("Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes.")]
int _worker_rlimit_nofile = 100000;
public int worker_rlimit_nofile
{
get { return _worker_rlimit_nofile; }
set { _worker_rlimit_nofile = value; }
}
[Browsable(true)]
[ReadOnly(false)]
[Category("General")]
[Description("Defines the error logging level. Availble options: debug | info | notice | warn | error | crit | alert | emerg")]
string _error_log_level = "crit";
public string error_log_level
{
get { return _error_log_level; }
set { _error_log_level = value; }
}
}
在属性网格上,当我设置SelectedObject
时,它呈现如下:
说明
未显示
class NginXConfig
{
[Browsable(true), ReadOnly(true), Category("General"), DefaultValueAttribute(1), Description("Defines the number of worker processes. Windows operating system supports only one.")]
public int worker_processes { get ; set; }
[Browsable(true), ReadOnly(false), Category("General"), DefaultValueAttribute(10000), Description("Changes the limit on the maximum number of open files (RLIMIT_NOFILE) for worker processes.")]
public int worker_rlimit_nofile { get; set; }
[Browsable(true), ReadOnly(false), Category("General"), DefaultValueAttribute("crit"), Description("Defines the error logging level. Availble options: debug | info | notice | warn | error | crit | alert | emerg")]
public string error_log_level { get; set; }
public NginXConfig()
{
worker_processes = 1;
worker_rlimit_nofile = 10000;
error_log_level = "crit";
}
}
以这种方式定义:
int _worker_processes = 1;
[Browsable(true),ReadOnly(true),Category("General"),Description("Defines the number of worker processes. Windows only supports one.")]
public int worker_processes
{
get { return _worker_processes; }
set { _worker_processes = value; }
}
属性用于属性,而不是专用变量。为什么不
{get;set;}
而不int\u worker\u processes=1
如果我使用{get;set;}
如何设置默认值?@Latheesan,在C#6.0中很容易:public int-worker_-process{get;set;}=…
,如果没有它,在构造函数中。@Sinatr我正在使用C#4.5-有解决方案吗?因为,目前我正在设置如下默认值:NginXConfig myNginXConfig=new NginXConfig();myNginXConfig.worker_进程=1代码>等。。。在设置SelectedObject
@Latheesan之前,请按F5并查看我之前的注释。@Latheesan:在C#6之前的版本中,您可以在构造函数中设置自动属性的默认值。提示:所有值都以粗体显示,因为您没有设置DefaultValueAttribute
@Sinatr,所以可以通过类定义这些字段在我的属性网格上显示的顺序吗?顺序是字母表,最简单的是给类别/属性指定合适的名称(例如“1.类别”、“2.类别”…)