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.