Example of Using proxy interception method call

Source: Internet
Author: User

Example of Using proxy to intercept method calls copied from chapter 7 of essential. net. Priorityproxy is used to increase the priority of the thread and reply to the thread before and after the method call.

Code Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. runtime. remoting. proxies;
Using System. Threading;
Using System. runtime. remoting. messaging;
Using System. runtime. remoting;
Using System. runtime. remoting. activation;
Using System. runtime. remoting. Services;

Namespace Leleapplication1
{
 
Class Program
{
Public   Static   Void Main ()
{
// Use the factory method to create a transparent proxy for objects. The object type must inherit from alalbyrefobject.
Mycalc1 C = Mycalc1.create (threadpriority. Normal );
VaR = C. Add ( 1 , 3 );
Console. writeline ();

// intercept the transparent proxy of the object created during object creation. The target type must be from contextboundobject
mycalc2 C3 = New mycalc2 ();
var B = c3.multiply ( 2 , 3 );
console. writeline (B);

Console. readkey ();
}

}
Public   Class Mycalc1: marshalbyrefobject
{
Public   Static Mycalc1 create (threadpriority level)
{
Mycalc1 target =   New Mycalc1 ();
Priorityproxy RP =   New Priorityproxy (target, Typeof (Mycalc1), level );
Return (Mycalc1) RP. gettransparentproxy ();
}
Private Mycalc1 (){}
Public   Double Add ( Double X, Double Y ){ Return X + Y ;}
Public   Double Multiply ( Double X, Double Y ){ Return X * Y ;}
}
[Priorityproxy (threadpriority. Highest)]
Public   Class Mycalc2: contextboundobject
{
Public   Double Add ( Double X, Double Y ){ Return X + Y ;}
Public   Double Multiply ( Double X, Double Y ){ Return X * Y ;}
}

[attributeusage (attributetargets. class)]
Public class priorityproxyattribute: proxyattribute
{< br> threadpriority level;
Public priorityproxyattribute (threadpriority level)
{< br> This . level = level;
}

Public   Override Marshalbyrefobject createinstance (type T)
{
// Note that we delegate to our base to get
// Uninitialized instance!
Marshalbyrefobject target =   Base . Createinstance (t );
Realproxy PP =   New Priorityproxy (target, T, level );
Return (Externalbyrefobject) pp. gettransparentproxy ();
}
}

Public   Class Priorityproxy: realproxy
{
Readonly Marshalbyrefobject target;
Readonly Threadpriority level;
Public Priorityproxy (export albyrefobject target, type, threadpriority level ): Base (Type)
{
This . Target = Target;
This . Level = Level;
}
Public   Override IMessage invoke (iMessage request)
{
// Step 1: Adjust priority
Thread here = Thread. currentthread;
Threadpriority old = Here. priority;
Here. Priority = Level;

// Step 2: Forward call
IMessage response =   Null ;
Imethodcallmessage call = (Imethodcallmessage) request;
Iconstructioncallmessage ctor = Call As Iconstructioncallmessage;

If (Ctor ! =   Null ) // Constructor Interception
{
Console. writeline ( " Ctor before " );
// We are holding a TP, so grab its RP
Realproxy defaultproxy = Remotingservices. getrealproxy (target );
// Ask intermediate RP to invoke Constructor
Defaultproxy. initializeserverobject (ctor );
// Get our TP
Marshalbyrefobject TP = (Externalbyrefobject) This . Gettransparentproxy ();
// Return a message containing our TP as the result of
// Constructor call
Response = Enterpriseserviceshelper. createconstructionreturnmessage (ctor, TP );

Console. writeline ( " Ctor after " );
}
Else // Common method Interception
{
Console. writeline ( " Method before " );
Response = Remotingservices. executemessage (target, call );
Console. writeline ( " Method After " );
}

// Step 3: restore old priority
here. priority = old;
// Step 4: return Response Message to TP
return response;

}
}

}

 

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.