Sharpdevelop attribute Structure Analysis

Source: Internet
Author: User
Tags sharpdevelop
The attribute structure is located in core/property. First, let's look at how the attribute system works,

The ixmlcovartable interface provides a method for converting objects and XML nodes. It can be considered another Implementation Method for serialization. According to the author, the implementation of these two methods is much faster than the serialization speed.
Iproperties is the main interface, and other parts of the application work by calling this interface. It provides read/write operations on attributes and a replication method for its own objects.

Defaultproperties is the default implementation method of the iproperties interface. Several of the implementation methods are indeed somewhat interesting.
This class is actually a registration factory. All properties created on demand exist in the hashtable properties.

1. First

1. First, let's talk about getproperty (). This method has several reloads, as shown below: 1 public object getproperty (string key, object defaultvalue)
2 {
3 if (! Properties. containskey (key )){
4 If (defaultvalue! = NULL ){
5 properties [Key] = defaultvalue;
6}
7 return defaultvalue;
8}
9
10 object OBJ = properties [Key];
11
12 if (defaultvalue is ixmlconvertable & obj is xmlelement ){
13 OBJ = properties [Key] = (ixmlconvertable) defaultvalue). fromxmlelement (xmlelement) OBJ). firstchild );
14}
15 return OBJ;
16}

From 2 to 8 rows, the key cannot be found in hashtable: Create a key and set it to defaultvalue, and return defavalue value.
10-15 rows. If hashtable has this key, assign the value to OBJ. If defaultvalue is derived from the ixmlcovertable interface, and obj is an xmlelement -- 13 lines, it will be difficult to understand. It is strange why cool people like to write such a long statement. Take it apart as follows: Xmlnode node = (xmlelement) OBJ). firstchild;
Xmlelement element = (xmlelement) node; // the key value should correspond to the first byte point of obj.

Ixmlconvertable Ixc = (ixmlconvertable) defaultvalue; // unpack defavalue value into ixmlconvertable

Object temp = Ixc. fromxmlelement (element );

OBJ = properties [Key] = temp;

Here we can see that defaultvalue does not play any role here, because the fromxmlelement () method only creates an element-based New defaultproperties object. Therefore, it can be abbreviated

Xmlnode node = (xmlelement) OBJ). firstchild;
Xmlelement element = (xmlelement) node; // here, element is a clone of the OBJ object.

Object temp = fromxmlelement (element );

OBJ = properties [Key] = temp;


This is the meaning of row 13th. Since hashtable has this key value, it is taken out to get its first subnode, and a new object is reconstructed based on this, assign OBJ and return it. -- I don't quite understand here. It takes debugging to runtime to comprehend.

Note: getproperty has an Enum version overload, which is of reference value for writing programs:

Public System. Enum getproperty (string key, system. Enum defaultvalue)
{
Try {
Return (system. Enum) enum. parse (defaultvalue. GetType (), getproperty (Key, (object) defaultvalue). tostring ());
} Catch (exception ){
Return defaultvalue;
}
}

2. setproperty (string key, object Val)
If the new and old attribute values are different, a new attribute is set for the key and the event onpropertychanged is triggered. This is a virtual overwriting event. Different scenarios have different implementations for this event, the custom parameter propertyeventargs of this event inherits eventargs and carries key/oldvalue/newvalue with it.

3. fromxmlelement (): A New defaultproperty object is constructed based on the xmlelement parameter, and the cooperation of the auxiliary function setvaluefromxmlelement () is required:

Public Virtual Object fromxmlelement (xmlelement element)
{
Defaultproperties = new defaultproperties ();
Defaultproperties. setvaluefromxmlelement (element );
Return defaultproperties;
}

Protected void setvaluefromxmlelement (xmlelement element)
{
Xmlnodelist nodes = element. childnodes;
Foreach (xmlelement El in nodes)
{
If (El. Name = "property ")
{
Properties [El. attributes ["key"]. innertext] = El. attributes ["value"]. innertext;
}
Else if (El. Name = "xmlconvertableproperty ")
{
Properties [El. attributes ["key"]. innertext] = El;
}
Else
{
Throw new unknownpropertynodeexception (El. Name );
}
}
}

To read the setvaluefromxmlelement () method, see the data \ options \ sharpdevelopproperties. xml file to understand how a new property object is constructed.

4. toxmlelement (): similar to fromxmlelement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.