复杂的属性类型 [英] Complex property types
问题描述
我有一个名为TerminalConnectionParameters的类,它包含一些不同类型的字段,如Hostname,IPAddress,UserName,Password等。
我想用这个类键入Dummy类的属性,例如:
public class Dummy
{
private TerminalConnectionParameters _parameters;
public TerminalConnectionParameters参数
{
get
{
// 在这里做一些花哨的计算
返回 _parameters;
}
set
{
_parameters = value ;
// 在这里做一些花哨的计算
}
}
}
这确保我可以获取或设置Dummy对象的整个Parameters类。
到目前为止一切都那么好,但是,如果我想使用这个属性如下:
objectDummy.Parameters.Hostname = < span class =code-string> dummyRouter;
当然,这个赋值语句不会设置dummyObject的内部_parameters字段的值。那么呢?什么是实现这样的属性编写器的正确方法,而不必独特地创建TerminalConnectionParameters的每个字段的属性访问器?
我知道如下的简单方法,但没有更好的解决方案?
TerminalConnectionParameters temp = objectDummy.Parameters;
temp.HostName = dummyRouter;
objectDummy.Parameters = temp;
这个问题背后的原因是我想用PropertyGrid来处理表格的内部_parameters对象。
我尝试过:
如上例所示。
实际上,如果您的_parameters成员已经初始化,那么您的第一个赋值语句很好 - 您可以在声明中执行此操作
私人 TerminalConnectionParameters _parameters = new TerminalConnectionParameters();
或在getter中
private TerminalConnectionParameters _parameters = null ;
public TerminalConnectionParameters参数
{
get
{
如果(_parameters - null )
_parameters = new TerminalConnectionParameters();
// 在这里做一些花哨的计算
返回 _parameters;
现在,一旦你是确保初始化,您需要做的就是用以下属性装饰参数
属性,设计师将神奇地处理其余的事情:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TerminalConnectionParameters参数
{
您可以序列化虚拟对象中的参数,并在宿主对象中反序列化。
谢谢Midi_Mick,就是这样。不知怎的,我觉得它必须简单......: - )
I have a class named TerminalConnectionParameters which contains a few differently typed fields, like Hostname, IPAddress, UserName, Password, etc.
I want to used this class type as a property of a Dummy class, like :
public class Dummy
{
private TerminalConnectionParameters _parameters;
public TerminalConnectionParameters Parameters
{
get
{
// do some fancy calculation here
return _parameters;
}
set
{
_parameters = value;
// do some fancy calculation here
}
}
}
This ensures that I can get or set the whole Parameters class of a Dummy object.
So far so good, but what, if I want to use this property like :
objectDummy.Parameters.Hostname= "dummyRouter";
For sure, this assignment statement will not set the value of the dummyObject's internal _parameters field. But what then ? And what is the correct way to implenment such property writer without having to create a property accessor of each field of the TerminalConnectionParameters uniquely ?
I am aware of the trivial way as below, but is there no better solution ?
TerminalConnectionParameters temp = objectDummy.Parameters;
temp.HostName = "dummyRouter";
objectDummy.Parameters = temp;
And the reason behind this question is that I want to use PropertyGrid to deal with the internal _parameters object of a Form.
What I have tried:
As outlined in the example above.
Actually, your first assignment statement is good providing your _parameters member has been initialised - you can either do that in the declaration
private TerminalConnectionParameters _parameters = new TerminalConnectionParameters();
or in the getter
private TerminalConnectionParameters _parameters = null; public TerminalConnectionParameters Parameters { get { if (_parameters - null) _parameters = new TerminalConnectionParameters(); // do some fancy calculation here return _parameters;
Now, once you are ensured of initialisation, all you need to do is decorate theParameters
property with the following attribute, and the designer will magically take care of the rest:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public TerminalConnectionParameters Parameters {
You could serialize the parameters in the dummy object, and deserialize it in the host object.
Thank you Midi_Mick, that was it. Somehow I felt it must be simple...:-)
这篇关于复杂的属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!