• .net 动态代理实现


    1、先新建一个要被代理的类,必须实现  MarshalByRefObject

    1. public class User: MarshalByRefObject
    2. {
    3. public string SayHello()
    4. {
    5. Console.WriteLine("I am SayHello");
    6. return "I am SuperMan";
    7. }
    8. }

    2、创建类 用于代理类执行方法 

    1. ///
    2. /// 动态代理要执行的方法
    3. ///
    4. public class DynamicAction
    5. {
    6. ///
    7. /// 执行目标方法前执行
    8. ///
    9. public Action BeforeAction { get; set; }
    10. ///
    11. /// 执行目标方法后执行
    12. ///
    13. public Action AfterAction { get; set; }
    14. }

    3、创建代理类  必须继承 RealProxy类

    1. public class DynamicProxy<T> : RealProxy
    2. {
    3. private readonly T _targetInstance = default(T);
    4. public Dictionary<string, DynamicAction> ProxyMethods { get; set; }
    5. public DynamicProxy(T targetInstance) : base(typeof(T))
    6. {
    7. _targetInstance = targetInstance;
    8. }
    9. public override IMessage Invoke(IMessage msg)
    10. {
    11. var reqMsg = msg as IMethodCallMessage;
    12. if (reqMsg == null)
    13. {
    14. return new ReturnMessage(new Exception("调用失败!"), null);
    15. }
    16. var target = _targetInstance as MarshalByRefObject;
    17. if (target == null)
    18. {
    19. return new ReturnMessage(new Exception("调用失败!请把目标对象 继承自 System.MarshalByRefObject"), reqMsg);
    20. }
    21. var methodName = reqMsg.MethodName;
    22. DynamicAction actions = null;
    23. if (ProxyMethods != null && ProxyMethods.ContainsKey(methodName))
    24. {
    25. actions = ProxyMethods[methodName];
    26. }
    27. if (actions != null && actions.BeforeAction != null)
    28. {
    29. actions.BeforeAction();
    30. }
    31. IMethodCallMessage callMessage = (IMethodCallMessage)msg;
    32. var result = RemotingServices.ExecuteMessage(target, reqMsg);
    33. if (actions != null && actions.AfterAction != null)
    34. {
    35. actions.AfterAction();
    36. }
    37. return result;
    38. }
    39. }

    4、创建代理工厂类

    1. ///
    2. /// 代理工厂
    3. ///
    4. ///
    5. public class ProxyFactory<T>
    6. {
    7. public static T Create(T obj, Dictionary<string, DynamicAction> proxyMethods = null)
    8. {
    9. var proxy = new DynamicProxy(obj) { ProxyMethods = proxyMethods };
    10. return (T)proxy.GetTransparentProxy();
    11. }
    12. }

    5、测试

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. var proxyMotheds = new Dictionary<string, DynamicAction>();
    6. proxyMotheds.Add("SayHello", new DynamicAction()
    7. {
    8. BeforeAction = new Action(() => Console.WriteLine("Before Doing....")),
    9. AfterAction = new Action(() => Console.WriteLine("After Doing...."))
    10. });
    11. var user = new User();
    12. var t = ProxyFactory.Create(user, proxyMotheds);
    13. t.SayHello();
    14. }
    15. }

  • 相关阅读:
    基于SSM框架的ACG动漫周边交易平台设计与实现
    ISIS的基本概念
    vue项目打包成H5apk中使用语音播放
    为什么从 MVC 到 DDD,架构的本质是什么?
    金仓数据库KingbaseES安全指南--6.1. 强身份验证简介
    【ARK UI】HarmonyOS ETS 资源管理基本使用
    【MySql进阶】索引详解(三):索引的使用和创建原则、索引失效、索引优化
    Android 12.0 Launcher3 去掉Hotseat功能
    光流法optical flow
    文件包含漏洞复习总结
  • 原文地址:https://blog.csdn.net/zhang8593/article/details/126494014