Using System;
Using System.Runtime.Remoting;
Using System.Runtime.Remoting.Services;
Using System.Runtime.Remoting.Activation;
Using System.Runtime.Remoting.Proxies;
Using System.Runtime.Remoting.Messaging;
/*
This procedure explains how the interception is carried out, and the relationship between the real agent and the transparent agent and interaction.
*/
Namespace Intercept_example
{
Class Class1
{
[STAThread]
static void Main (string[] args)
{
Example Exa = new Example ("Sky");
Exa.say_hello ();
}
}
Defines a real agent for implementing a specific preprocessing and reprocessing
public class Interceptproxy:realproxy
{
Private ReadOnly MarshalByRefObject Target;
Public Interceptproxy (MarshalByRefObject obj, type type): Base (Type)
{
This.target = obj;
}
If the constructor is triggered, then target construction has not started
IConstructionCallMessage ctor = call as IConstructionCallMessage;
if (ctor!= null)
{
System.Console.WriteLine ("Forwarding constructor call!") ") ;
System.Console.WriteLine ("post-processing ...");
System.Threading.Thread.Sleep (5000);
System.Console.WriteLine ("End of post processing!") ") ;
return result_msg;
}
}
Defines an attribute that can associate the real agent above with the class that uses the attribute
[AttributeUsage (AttributeTargets.Class)]
public class Interceptproxyattribute:proxyattribute
{
Get Transparent Agent
public override MarshalByRefObject CreateInstance (Type servertype)
{
Uninitialized instances
MarshalByRefObject target = base. CreateInstance (ServerType);
Interceptproxy rp = new Interceptproxy (target, servertype);
[Interceptproxy]
The public class example:contextboundobject//is placed in a particular context outside of which the transparent proxy for the object is obtained
{
private string name;
Public Example (String a)
{
THIS.name = A;
}
/*
* (1) when the caller and the location are located in different contexts, the caller obtains the transparent proxy of the object being invoked. The transparent proxy packages the method call into a imessage object and passes to the Invoke method of the actual proxy.
* The default implementation of the Invoke method simply passes the imessage object to a subsequent channel, such as a stack generator, and so on. We can customize our real proxies, rewrite the Invoke method, and add preprocessing and reprocessing.
* (2) associates the real agent with the target class by proxy attributes (such as Interceptproxyattribute) (inherited from ProxyAttribute). This proxy feature overrides the ProxyAttribute CreateInstance
* CreateInstance method, in which the real agent (such as Interceptproxy) is wrapped around the target object, and the transparent proxy for the real agent is returned.
* (3) in a program, the target object's method cannot be invoked directly through a real proxy, because the real agent handles the message object (IMessage), which is based on the message world.
* (4) It is possible to join the interception (pretreatment and reprocessing) only if a reference is held with a transparent proxy.
* (5) derived from the ContextBoundObject class, we can only get its transparent proxy; The object of the class that derives from Mashalbyrefobject, we may get its transparent proxy or direct reference, depending on the adjusted party and the target
* Whether the object is in the same context; The object of a class that is not derived from these two classes, we can only get a direct reference to it.
*/
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