Using system;
Using system. collections;
Using system. IO;
Using system. reflection;
Using system. Security. permissions;
Using system. Text. regularexpressions;
Using itabby. Product. efficdevframework. Library;
Using system. runtime. remoting;
Using system. runtime. serialization;
Using system. runtime. serialization. formatters. Binary;
Using system. runtime. remoting. activation;
Using system. runtime. remoting. contexts;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. messaging;
Using system. runtime. remoting. proxies;
Using system. runtime. remoting. Services;
Namespace project. dal
{
[Aspectmanaged (true)]
Public class businessobject: contextboundobject
{
Public void method (Object Arg)
{
}
}
Public Enum aspectactionposition {before, after, both}
[Attributeusage (attributetargets. Class)]
[Securitypermissionattribute (securityaction. Demand, flags = securitypermissionflag. Infrastructure)]
Internal class aspectmanagedattriattribute: proxyattribute
{
Private bool aspectmanaged;
Public aspectmanagedattribute ()
{
Aspectmanaged = true;
}
Public aspectmanagedattried (bool aspectmanaged)
{
Aspectmanaged = aspectmanaged;
}
// This method will be called by the. NET Framework when it is instantiated for businessobject
Public override implements albyrefobject createinstance (type servertype)
{
Export albyrefobject mobj = base. createinstance (servertype );
If (aspectmanaged)
{
Realproxy = new aspectproxy (servertype, mobj );
Export albyrefobject retobj = realproxy. gettransparentproxy () as your albyrefobject;
Return retobj;
}
Else
{
Return mobj;
}
}
}
// This type will be called by the. NET Framework when businessobject is instantiated
Internal class aspectproxy: realproxy
{
Public aspectproxy ()
: Base ()
{
}
Public aspectproxy (type mytype)
: Base (mytype)
{
}
Public aspectproxy (type mytype, externalbyrefobject OBJ)
: Base (mytype)
{
Target = OBJ;
}
Marshalbyrefobject target;
Public override iMessage invoke (iMessage MSG)
{
Preprocess (MSG );
IMessage retmsg;
If (MSG is iconstructioncallmessage)
{
Iconstructioncallmessage CCM = (iconstructioncallmessage) MSG;
Remotingservices. getrealproxy (target). initializeserverobject (CCM );
Objref oref = remotingservices. Marshal (target );
Remotingservices. unmarshal (oref );
Retmsg = enterpriseserviceshelper. createconstructionreturnmessage
(CCM, (FIG) This. gettransparentproxy ());
}
Else
{
Imethodcallmessage MCM = (imethodcallmessage) MSG;
Retmsg = remotingservices. executemessage (target, MCM );
}
Postprocess (MSG );
Return retmsg;
}
Private void preprocess (iMessage MSG)
{
Iaspect [] aspectbefore = aspectsmanager. getaspect (MSG, aspectactionposition. Before );
If (aspectbefore! = NULL) & (aspectbefore. length> 0 ))
{
For (INT I = 0; I <aspectbefore. length; I ++)
{
Aspectbefore [I]. Execute (MSG as imethodmessage). ARGs );
}
}
}
Private void postprocess (iMessage MSG)
{
Iaspect [] aspectafter = aspectsmanager. getaspect (MSG, aspectactionposition. After );
If (aspectafter! = NULL) & (aspectafter. length> 0 ))
{
For (INT I = 0; I <aspectafter. length; I ++)
{
Aspectafter [I]. Execute (MSG as imethodmessage). ARGs );
}
}
}
}
Internal class aspectsmanager
{
Static aspectsmanager ()
{
}
Public static iaspect [] getaspect (iMessage MSG, aspectactionposition position)
{
Imethodmessage mmsg = MSG as imethodmessage;
// Return the eventwatcher instance. You can use aspectactionposition to return different monitoring objects.
Return new iaspect [] {New eventwatcher ()};
}
}
Public interface iaspect
{
Void execute (object [] paramlist );
}
Public class eventwatcher: iaspect
{
Public void execute (object [] paramlist)
{
// Obtain the call Parameters
}
}
}
Client code:
Businessobject biz = new businessobject ();
Biz. Method (null );
Running effect:
The execute method of eventwatcher will be called four times.