. NET Component Programming context

Source: Internet
Author: User

. NET Component Programming context

In the subsequent length of the remote invocation of the article on the application domain, which is a large-grained control of the logic of the program set, then the object of the control and who will be the boss?
Yes, that's the context. The CLR refines the application domain, adding the concept of context to the application, where the context is a logical existence with a set of constraints and is responsible for managing access to all objects in it.
For example, it's a bit inappropriate, but let's take a little understanding.
For example, the community is the application domain, we live in the residential community is the context of the container, the community's gatekeeper is the context of behavior, doorman + community = context. And we are the object, the community of the door to strangers in and out of the community will ask: you come in to find Which? Looking for who? What's that?
And the real context is to do this, the actual busy much (this is a bit of AOP meaning)

Figure 1

is just a preliminary example structure, and we'll look down.

In the system, we can get to the context of the current object through the static property Defaultcontext of the context class.

Context context = Context.defaultcontext;

Of course, you can also get to the current context through the static property CurrentContext of the thread class

Context context = Thread.currentcontext;

Figure 2

(The left red line starts at the beginning of the thread, and the red end represents the thread description)

In the context system, it is composed of context-bound objects (ContextBoundObject), Context properties (Icontextattrbitute), Context member properties (Icontextproperty), and message sinks.

This space only involves the first three points of knowledge in the system, that is, how the custom component service is implemented?

Before we start talking about context and object types, there is a default context in the application domain, and ordinary objects can be used in any context, such objects are called context Agile objects, and some objects can only be used in the context of an attribute. is an object that inherits from the ContextBoundObject type, which is called a context-oriented object.

How to understand the custom component service?

1. Context member properties (Icontextproperty)

1     /// <summary>2     ///Context member Properties3     /// </summary>4      Public classContextwriterservice:icontextproperty5     {6 7          Public voidFreeze (System.Runtime.Remoting.Contexts.Context newcontext)8         {9             Ten         } One  A          Public BOOLIsnewcontextok (System.Runtime.Remoting.Contexts.Context newCtx) -         { -             return true; the         } -  -          Public stringName -         { +             Get{return "Contextservice"; } -         } +  A         /// <summary> at         ///the services provided -         /// </summary> -         /// <param name= "Meg" ></param> -          Public voidWritermessage (stringMeg) -         { - Console.WriteLine (MEG); in         } -}

Several members of the Icontextproperty interface: a name read-only property, a member property name, as a keyword in the context member property collection, or a keyword and name for a custom service. Freeze: Freezes the context Isnewcontextok: Determines whether the current context satisfies the requirement and provides an opportunity to discard the creation context if the function returns FALSE.

An object of type Icontextproperty exists in the context of contextproperties, not directly added, and when we get to the context object, the context object is already frozen.

2. Context properties (Icontextattrbitute)

1     usingSystem.Runtime.Remoting;2     usingSystem.Runtime.Remoting.Contexts;3     usingSystem.Runtime.Remoting.Activation;4 5 6 [AttributeUsage (AttributeTargets.Class)]7      Public classContextwriterattribute:attribute, Icontextattribute8     {9 Ten          Public voidGetpropertiesfornewcontext (iconstructioncallmessage msg) One         { AMsg. Contextproperties.add (NewContextwriterservice ()); -         } -  the          Public BOOLIscontextok (System.Runtime.Remoting.Contexts.Context ctx, IConstructionCallMessage msg) -         { -Contextwriterservice Contextservice = -CTx. GetProperty ("Contextservice") asContextwriterservice; +              -             if(Contextservice! =NULL) +             { A                 return true; at             } -             return false; -         } -}

Icontextattribute Interface Members:
Iscontextok function: Call this method at run time to determine whether the current context has a required custom property, that is, whether the current type is active in the current context.
Getpropertiesfornewcontext: This method is called after the Iscontextok method has determined that it is not OK, where the IConstructionCallMessage interface type object is passed to the newly created context. And the newly created context will be based on the IConstructionCallMessage type
Contextproperties to add custom services for your own, which is also the only opportunity to add custom services to the context.
I also have a question here, looking to know the great God to give a tip, is how to add a custom service in the current default context?

3. Context-bound object (ContextBoundObject)

1 [Contextwriter ()]2      Public classMycontextobject:contextboundobject3     {4          Public voidCallcurrentcontextwriterservice ()5         {6Context context =Thread.currentcontext;7Contextwriterservice Contextwriterservice =8Context. GetProperty ("Contextservice") asContextwriterservice;9             if(Contextwriterservice! =NULL)Ten             { OneContextwriterservice.writermessage ("Current Context ID:"+context. ContextID +"-Test Information"); A             } -         } -}

It is obvious that mycontextobject can be obtained to the Contextwriterservice type and used.

These are part of the context and, of course, the most important part: The message receiver, this space is not introduced, will be said in a later space.

Jinyuan

Source: http://blog.csdn.net/jinyuan0829

This article is copyrighted by the author and Csdn, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.