SimpleProxyTool.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace Azylee.Core.ProxyUtils.SimpleProxyUtils
  7. {
  8. public class SimpleProxyTool<T>
  9. {
  10. T Object;
  11. /// <summary>
  12. /// 记录方法前置操作和后置操作
  13. /// </summary>
  14. List<Tuple<int, RunMode, string, Action>> Operation = new List<Tuple<int, RunMode, string, Action>>();
  15. public SimpleProxyTool(T obj)
  16. {
  17. Object = obj;
  18. }
  19. /// <summary>
  20. /// 添加操作
  21. /// </summary>
  22. /// <param name="type">执行类型</param>
  23. /// <param name="method">方法名</param>
  24. /// <param name="action">动作</param>
  25. public void Add(RunMode type, string method, Action action)
  26. {
  27. Operation.Add(new Tuple<int, RunMode, string, Action>(Operation.Count, type, method, action));
  28. }
  29. /// <summary>
  30. /// 执行方法
  31. /// </summary>
  32. /// <typeparam name="R">返回值</typeparam>
  33. /// <param name="methodName">方法名</param>
  34. /// <param name="objs">参数</param>
  35. /// <returns></returns>
  36. public R Invoke<R>(string methodName, object[] objs)
  37. {
  38. //执行全局前置操作
  39. List<Tuple<int, RunMode, string, Action>> allBefore = Operation.Where(x => x.Item2 == RunMode.AllBefore).ToList();
  40. if (allBefore != null) allBefore.ForEach(b => { b.Item4?.Invoke(); });
  41. //执行方法前置操作
  42. List<Tuple<int, RunMode, string, Action>> methodBefore = Operation.Where(x => x.Item3 == methodName && x.Item2 == RunMode.MethodBefore).ToList();
  43. if (methodBefore != null) methodBefore.ForEach(b => { b.Item4?.Invoke(); });
  44. MethodInfo method = Object.GetType().GetMethod(methodName);
  45. object rs = method.Invoke(Object, objs);
  46. //执行方法后置操作
  47. List<Tuple<int, RunMode, string, Action>> methodAfter = Operation.Where(x => x.Item3 == methodName && x.Item2 == RunMode.MethodAfter).ToList();
  48. if (methodAfter != null) methodAfter.ForEach(b => { b.Item4?.Invoke(); });
  49. //执行全局后置操作
  50. List<Tuple<int, RunMode, string, Action>> allAfter = Operation.Where(x => x.Item2 == RunMode.AllAfter).ToList();
  51. if (allAfter != null) allAfter.ForEach(b => { b.Item4?.Invoke(); });
  52. return (R)rs;
  53. }
  54. }
  55. public class Dog
  56. {
  57. public string Jump(string name) { return name + " Jump"; }
  58. public string Play(string name) { return name + " Play"; }
  59. }
  60. class Test
  61. {
  62. private void Main()
  63. {
  64. //新建对象
  65. Dog dog = new Dog();
  66. //新建代理
  67. SimpleProxyTool<Dog> proxy = new SimpleProxyTool<Dog>(dog);
  68. //初始化代理前置、后置操作
  69. proxy.Add(RunMode.MethodBefore, "Jump", new Action(() => { Console.WriteLine("跳之前1"); }));
  70. proxy.Add(RunMode.MethodBefore, "Jump", new Action(() => { Console.WriteLine("跳之前2"); }));
  71. proxy.Add(RunMode.MethodAfter, "Jump", new Action(() => { Console.WriteLine("跳之后1"); }));
  72. proxy.Add(RunMode.MethodAfter, "Jump", new Action(() => { Console.WriteLine("跳之后2"); }));
  73. proxy.Add(RunMode.MethodBefore, "Play", new Action(() => { Console.WriteLine("Play之前"); }));
  74. proxy.Add(RunMode.MethodAfter, "Play", new Action(() => { Console.WriteLine("Play之后"); }));
  75. proxy.Add(RunMode.AllBefore, "", new Action(() => { Console.WriteLine("所有方法之前"); }));
  76. //执行目标方法
  77. string rs = proxy.Invoke<string>("Jump", new[] { "Tom" });
  78. }
  79. }
  80. }