C # create a message Interceptor (intercept) 1

Source: Internet
Author: User

First, we need to create a custom attribute so that it can have the context reading function. Therefore, this attribute class must inherit both attribute and icontextattribute.

There are two methods to implement the icontextattribute interface.

1. bool iscontextok (context CTX, iconstructioncallmessage MSG );

2. Void getpropertiesfornewcontext (iconstructioncallmessage MSG );

The two methods are described as follows:

1. The iscontextok method is used to check whether there is a problem with the current context. If there is no problem, true is returned. If there is a problem, false is returned. Then the class will call getpropertiesfornewcontext.

2. getpropertiesfornewcontext is a new context automatically created by the system, and then let's do what the new environment should do.

    /// <summary>    /// Some class if you want to intercept,you must mark this attribute.    /// </summary>    public class InterceptAttribute : Attribute, IContextAttribute    {        public InterceptAttribute()        {            Console.WriteLine(" Call 'InterceptAttribute' - 'Constructor'  ");        }        public void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)        {            ctorMsg.ContextProperties.Add(new InterceptProperty());        }        public bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg)        {            InterceptProperty interceptObject = ctx.GetProperty("Intercept") as InterceptProperty;                        return interceptObject != null;        }    }
OK. This is the implementation of this class. Several points should be explained:

1. The interceptattribute class inherits attribute, which is used for [attribute] marking.

2. The interceptattribute class inherits the icontextattribute and is used to grant context permissions to the marked class. Then, two methods to implement this interface are required.

3. The iscontextok method is used to determine whether the "intercept" attribute exists currently. Because we only need this attribute, we only need to check whether the current context of this attribute is available. If yes, true is returned, otherwise, the getpropertiesfornewcontext function is called.

(Here we only use the interceptor function, so we only add the intercept custom attribute. Of course, if necessary, we can add multiple attributes and then perform the corresponding check in this function)

4. If you call the getpropertiesfornewcontext function, it will let us customize the new context. Here I am doing an operation: add an attribute in the current context, this attribute is intercept.

5. In the next chapter, I will implement the interceptproperty class. In fact, this class is a context property.

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.