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;
}
}
}