using System.Collections.Generic;
using RealProxy = System.Runtime.Remoting.Proxies.RealProxy;
using IMessage = System.Runtime.Remoting.Messaging.IMessage;
using ReturnMessage = System.Runtime.Remoting.Messaging.ReturnMessage;
using IMethodCallMessage = System.Runtime.Remoting.Messaging.IMethodCallMessage;
using TargetInvocationException = System.Reflection.TargetInvocationException;
namespace De.Tutorials.Training
class DynamicProxyExample
public static void Main(string[] args)
IBusinessService businessService
= new SimpleProxy
(typeof(IBusinessService
),
new BusinessServiceImpl
()).
GetTransparentProxy() as IBusinessService;
Console.WriteLine(businessService.BusinessOperation("Proxy"));
class SimpleProxy : RealProxy
public SimpleProxy(Type type, object target)
public override IMessage Invoke(IMessage message)
IMethodCallMessage methodCallMessage = (IMethodCallMessage)message;
Console.WriteLine("Before: " + methodCallMessage.MethodName);
object result = target.GetType().GetMethod(methodCallMessage.MethodName, (Type[])methodCallMessage.MethodSignature).Invoke(target, methodCallMessage.InArgs);
Console.WriteLine("After: " + methodCallMessage.MethodName);
return new ReturnMessage
(result,
null,
0, methodCallMessage.
LogicalCallContext, methodCallMessage
);
catch (TargetInvocationException targetInvocationException)
return new ReturnMessage
(targetInvocationException, methodCallMessage
);
interface IBusinessService
string BusinessOperation(string args);
class BusinessServiceImpl
public string BusinessOperation(string args)
Console.WriteLine("Hallo " + args);