As described in the previous section, the regkeyattribute constructor is:
Public regkeyattribute (reghives hive, string valuename)
Based on the prototype of this property constructor, we attach this property to a field:
[Regkey (reghives. HKEY_CURRENT_USER, "foo")]
Public int Foo;
In fact, it can make programming easier. If the parameters remain unchanged in most cases, you can use positional parameter and named parameter to set the default value.
the location parameter is required. In the preceding example, both Hive and valuename are the location parameters.
the named parameters are not defined in the property constructor. On the contrary, they are non-static fields and features (property, this book translates this word into a feature, probably to distinguish it from the attribute currently mentioned, but I always feel awkward ). Therefore, the named parameters enable the customer to set property fields and features when the property is instantiated, without the need to set fields and features for the customer, instead of creating a constructor for each combination of fields and features that the customer may set.
after specifying the location parameter, you can use the fieldorpropertyname = Value Syntax to reference a field or feature. Modify the regkeyattribute attribute to demonstrate this process.
// [regkey (reghives. HKEY_CURRENT_USER, "foo")]
[regkey ("foo")]
Public int Foo;
In this modification, valuename is a unique location parameter. Hive becomes an optional name parameter.
"how to define the named parameters ?" Because only the location parameter is included in the definition of the constructor (so it is required), you only need to delete this parameter from the constructor. (I understand that "this parameter" refers to hive ). In this way, you can reference a field or feature as a named parameter, provided that these fields are not readonly, static, and Const. These features must contain the setting method and are not static.
now, you can attach this attribute in either of the two ways.
// either
[regkey ("foo")]
Public int Foo;
the output is:
Foo will be saved in hkey_class_root \ Foo
another method is:
// or
[regkey ("foo", hive = reghives. HKEY_LOCAL_MACHINE)]
Public int Foo;
the output is:
Foo will be saved in HKEY_LOCAL_MACHINE \ Foo
In this way, the default value is provided for the field, or the default value can be overwritten as needed.
But where does the default value of regkeyattribute. Hive come from?
In fact, regkeyattribute. hive is an enumeration value. The underlying type is int and a value type. According to the definition, the compiler initializes it to 0!
However, if you do not want to use 0 as the default value for some reason, modifyCodeThe value 0 is invalid.
Public Enum reghives
{
Hkey_class_root = 1,
HKEY_CURRENT_USER,
HKEY_LOCAL_MACHINE,
Hkey_current_config
}
(After my experiment, the output at this time is
Foo will be saved in 0 \ foo
)
Now we know that the only reason regkeyattribute. Hive becomes 0 is that the compiler initializes it to 0, and the user does not overwrite this value through the name parameter. You can use the following code to initialize the value:
Public regkeyattribute (string valuename)
{
If (this. Hive = 0)
This. Hive = reghives. HKEY_CURRENT_USER;
This. valuename = valuename;
}
PS:
Although most of the original articles are written in the book, I try to refine them with my own understanding. Of course, my own words are still in brackets. Although I am a little tired, I can understand this while playing, the results are quite good, huh, because I was dizzy when I read this section yesterday. Today I wrote a blog much clearer.