|
using System;
namespace ProxyExample
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("--- ProxyExample started ---\n");
Proxy proxy = new Proxy(new BusinessObject());
BusinessObject bizObject = (BusinessObject)proxy.GetTransparentProxy();
bizObject.A = 17;
bizObject.B = 23;
bizObject.Multiply();
Console.WriteLine("Press any key to quit");
Console.ReadLine();
}
}
}
using System;
namespace ProxyExample
{
public class BusinessObject : MarshalByRefObject
{
private int a = 0;
private int b = 0;
public BusinessObject()
{
}
public int Multiply()
{
return a * b;
}
public int A
{
get { return this.a; }
set { this.a = value; }
}
public int B
{
get { return this.b; }
set { this.b = value; }
}
}
}
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
namespace ProxyExample
{
public class Proxy : RealProxy
{
private object target;
public Proxy(MarshalByRefObject target) : base(target.GetType())
{
this.target = target;
}
public override IMessage Invoke(IMessage msg)
{
IMethodCallMessage call = msg as IMethodCallMessage;
preprocess();
Console.WriteLine("method called: " + call.MethodName);
IMessage returnMsg = RemotingServices.ExecuteMessage(
(MarshalByRefObject)this.target, call);
postprocess();
return returnMsg;
}
private void preprocess()
{
Console.WriteLine("before method call...");
}
private void postprocess()
{
Console.WriteLine("after method call...\n");
}
public object Target
{
get { return this.target; }
set { this.target = value; }
}
}
}
|
|