Manually add and retrieve data for objects using extended attributes (continued)

Source: Internet
Author: User

InPreviousArticle...Understand the principle and structure of extended attributes. In fact, both its internal structure and philosophy are related toDependency attributeBasically the same, you can also learn about the principles of dependency attributes, which is also helpful for understanding and using dependency attributes, "extended attributes" are only partially extended for specific scenarios (for example, the extended attributes of common objects are supported), but in principle all attributes are stored externally, in this way, the problem is that the object attributes cannot be recycled and released in a timely manner, and the objects must be released manually (I don't know if there is any good solution here ).

Next, I will continue to discuss the dynamic nature of extended attributes.

Remember how the dynamic interfaces of extended attributes were used in the previous article?

 
1:VaR user1 =NewUserinfo1 () {age = 21, name ="Maxzhang1"};
2:Dynamic dyuser = user1.todynamicattachobject ();
 
3:RRR = dyuser. Info;
 
4:Dyuser. info = 1;
 
5:IntD = dyuser. Info;

For dynamic types, we recommend two articles: 1 and 2.. Next, let's take a look at how dynamic attributes are defined in the extended attributes:

 
1: Public ClassExtenddynamicobject: dynamicobject

 
2:{

 
3:PrivateExtendobject;

 
4:

 
5:PublicExtenddynamicobject (extendobject exobject)

 
6:{

 
7:Extendobject = exobject;

8:}

 
9:

 
10:Public Override BoolTrysetmember (setmemberbinder binder,Object Value)

 
11:{

 
12:StringPropertyname = binder. Name;

13:

 
14:If(This. Extendobject. isextendproperty (propertyname ))

 
15:This. Extendobject. setvalue (Binder. Name,Value);

 
16:Else

 
17:{

18:ObjectOwner =This. Extendobject. getowner ();

 
19:Type ownertype = owner. GetType ();

 
20:VaR propertyinfo = ownertype. getproperty (propertyname );

 
21:Propertyinfo. setvalue (owner,Value,Null);

 
22:}

23:Return True;

 
24:}

 
25:

 
26:Public Override BoolTrygetmember (getmemberbinder binder,Out ObjectResult)

 
27:{

28:Result =Null;

 
29:

 
30:StringPropertyname = binder. Name;

 
31:

 
32:If(This. Extendobject. isextendproperty (propertyname ))

33:Result =This. Extendobject. getvalue (Binder. Name );

 
34:Else

 
35:{

 
36:ObjectOwner =This. Extendobject. getowner ();

 
37:Type ownertype = owner. GetType ();

38:VaR propertyinfo = ownertype. getproperty (propertyname );

 
39:Propertyinfo. getvalue (owner,Null);

 
40:}

 
41:Return True;

 
42:}

 
43:

44:}

It is actually injectingExtendobjectType object and dynamically associate the property name with the extended property.GetownerMethod To get an object instance inside the extension object. If it is an extension object, it inheritsExtendobjectOfGetownerMethod is an extendobject object,AttachobjectType(Remember this type. It inherits fromExtendobjectOf)ObjectWordsGetownerThe method may obtain any object, and then we can get the value of the extended attribute through the object instance. For common attributes, we can access the object through reflection.

For the above dynamic interface, the userinfo1 type is a common type and does not inherit any other types. The Info attribute in user1 is an extended attribute of user1.Extendproperty. RegisterpropertyIf the method has been registered, the system generates an extended property of type object by default. However, registering extended attributes in the system can bring some benefits, suchExtended attributesAddDefault Value, verification event, and attribute value change event.

 
1:Private StaticExtendproperty infoproperty =
 
2:Extendproperty. registerproperty ("Info",Typeof(String),Typeof(Userinfo ),"You win");

3:Infoproperty. valuechanging + =NewEventhandler <extendpropertyvaluechangingargs>

(Infoproperty_valuechanging );

 
4:Infoproperty. valuechanged + =NewEventhandler <extendpropertyvaluechangedargs>
 
(Infoproperty_valuechanged );

 

Next, let's look at an interesting thing: property inheritance:

What is property inheritance? Simply put, an attribute of type A also has attributes with the same name in type B, and the attribute in B must have some features of the attribute in. Let's take a look at how to implement property inheritance:

1:Private StaticExtendproperty infoproperty =
 
2:Extendproperty. registerproperty ("Info",Typeof(String),Typeof(Userinfo ),"You win");
 
3:Infoproperty. addowner (Typeof(Userinfo1 ),"Ha ha");

In the extended attributes, the userinfo1 type has an additional info attribute and inherits the info attribute of the userinfo type. The declared results here indicate that the two types have different default values, but they currently share two events (verification events and attribute value change events ).AddownerIf the default value of userinfo1 is not added to the method, the string "you win" is obtained when the object instance of userinfo1 accesses the extended property "info" for the first time, this inherits the attributes of the userinfo type.AddownerAdding the default value is equivalent to rewriting info in userinfo.

To achieve this goalIn fact, only when registering a new property (AddownerMethodUserinfo1 type + property name to be inheritedGenerate a new key,In addition, it points to the original extended attributes (essentially two objects share one attribute ).

 

In fact, this concept andThe additional attributes in WPF are different.

 

I hope my article can extend your thinking and understand the principles of dependency attributes and additional attributes. Thank you.

Maxzhang1985@gmail.com hope you can exchange more.

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.