From http://www.cnblogs.com/thinhunan/archive/2007/07/17/460370.html
Realproxy is the simplest method. The disadvantage is that you cannot add multiple proxies at the same time; otherwise, an exception is thrown.
Steps:
1. Implement the inheritance class of realproxy
2. Implement the proxyattribute of the realproxy class implemented above
3. Apply the attribute of the preceding implementation to the contextboundobject instance.
// MyproxyCodeUsing system;
Using System. runtime. remoting;
Using System. runtime. remoting. messaging;
Using System. runtime. remoting. proxies;
Using System. runtime. remoting. activation;
Using System. runtime. remoting. Services;
Using System. diagnostics;
Namespace Dotnetaop. usingrealproxy
... {
Class Myproxy: realproxy
... {
Marshalbyrefobject _ target = Null ;
Public Myproxy (type, externalbyrefobject target ): Base (Type)
... {
This. _ Target=Target;
}
// Overwrite invoke to process various messages intercepted by realproxy,
// This method is the simplest, but it cannot intercept the activation of remote objects. Fortunately, we do not really need to remoting
Public Override IMessage invoke (iMessage MSG)
... {
Imethodcallmessage call = (Imethodcallmessage) MSG;
Iconstructioncallmessage CTR = Call As Iconstructioncallmessage;
Imethodreturnmessage back = Null ;
// Contextboundobject (inherit from jsonalbyrefobject) object can intercept the constructor.
If (CTR ! = Null )
... {
Console. writeline ( " Call " + Ctr. activationtype. Name + " Type constructor " );
Realproxy defaultproxy = Remotingservices. getrealproxy (_ target );
// If the following step is not performed, _ target is still a transparent proxy without direct instantiation of the proxy object,
// In this case, the object is not directly constructed.
Defaultproxy. initializeserverobject (CTR );
// This class is a realproxy, which can be transparent through the gettransparentproxy Function
Back = Enterpriseserviceshelper. createconstructionreturnmessage (CTR, (marshalbyrefobject) gettransparentproxy ());
}
// The delealbyrefobject object can intercept common call messages,
// The externalbyrefobject object tells the compiler that it cannot optimize simple internal member functions into Inline code,
// In this way, function calls can be intercepted.
Else
... {
Console. Write ( " Call the member function: " + Call. methodname );
Back = Remotingservices. executemessage (_ target, call );
Console. writeline ( " , The returned result is: " + Back. returnvalue. tostring ());
}
Return Back;
}
}
}
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. runtime. remoting. proxies;
Namespace Dotnetaop. usingrealproxy
{
// Inherit from proxyattribute to automatically implement realproxy Implantation
[Attributeusage (attributetargets. Class)]
Class Myproxyattribute: proxyattribute
{
// Override the createinstance function and return the self-built proxy.
Public Override Marshalbyrefobject createinstance (type servertype)
{< br> export albyrefobject OBJ = base . createinstance (servertype);
myproxy proxy = New myproxy (servertype, OBJ);
return (financialbyrefobject) proxy. gettransparentproxy ();
}
}
}
// Mycbo code Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Dotnetaop. usingrealproxy
{
[Myproxy]
Class Mycbo: contextboundobject
{
Public Int Add ( Int A, Int B)
{
ReturnA+B;
}
Public Int Divide ( Int A, Int B)
{
ReturnA/B;
}
}
}
// Test code Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Dotnetaop
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Usingrealproxy. mycbo CBO= NewDotnetaop. usingrealproxy. mycbo ();
CBO. Add (1,2);
Console. Readline ();
}
}
}
I tried to implement multiple realproxy nested proxy objects, but it finally ended in failure. However, both of the following implementations can be intercepted by multiple applications.
Download: simple implementation of the nested realproxy framework this framework ended in failure because an object cannot have multiple proxies. The strange thing is that remotingproxy can be used again and remains to be studied.
If you need to implement the preceding single realproxy, leave an email.