You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Shentun.Utilities
  8. {
  9. public class ReflectionHelper
  10. {
  11. public static async Task<T> InvokeAsync<T>(string assemblyName, string className, string classConstructorArg, string methodName, object[] args = null)
  12. {
  13. Assembly assembly = Assembly.Load(assemblyName);
  14. Type type = assembly.GetType(className);
  15. // 创建类的实例
  16. object instance = Activator.CreateInstance(type, classConstructorArg);
  17. // 获取方法信息
  18. MethodInfo method = type.GetMethod(methodName);
  19. // 调用方法,如果方法需要参数,可以传入对应的参数数组,例如: new object[] { arg1, arg2 }
  20. T returnValue;
  21. var isAsync = (method.ReturnType == typeof(Task) ||
  22. (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)));
  23. if (isAsync)
  24. {
  25. // 使用反射调用方法
  26. //object returnValue = method.Invoke(instance, args);
  27. returnValue = await (Task<T>)method.Invoke(instance, args);
  28. }
  29. else
  30. {
  31. returnValue = (T)method.Invoke(instance, args);
  32. }
  33. return returnValue;
  34. }
  35. public static async Task InvokeAsync(string assemblyName, string className, string classConstructorArg, string methodName, object[] args = null)
  36. {
  37. Assembly assembly = Assembly.Load(assemblyName);
  38. Type type = assembly.GetType(className);
  39. // 创建类的实例
  40. object instance = Activator.CreateInstance(type, classConstructorArg);
  41. // 获取方法信息
  42. MethodInfo method = type.GetMethod(methodName);
  43. // 调用方法,如果方法需要参数,可以传入对应的参数数组,例如: new object[] { arg1, arg2 }
  44. var isAsync = (method.ReturnType == typeof(Task) ||
  45. (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)));
  46. if (isAsync)
  47. {
  48. // 使用反射调用方法
  49. //object returnValue = method.Invoke(instance, args);
  50. await (Task)method.Invoke(instance, args);
  51. }
  52. else
  53. {
  54. method.Invoke(instance, args);
  55. }
  56. return ;
  57. }
  58. public static void Invoke(string assemblyName, string className, string classConstructorArg, string methodName, object[] args = null)
  59. {
  60. Assembly assembly = Assembly.Load(assemblyName);
  61. Type type = assembly.GetType(className);
  62. // 创建类的实例
  63. object instance = Activator.CreateInstance(type, classConstructorArg);
  64. // 获取方法信息
  65. MethodInfo method = type.GetMethod(methodName);
  66. // 调用方法,如果方法需要参数,可以传入对应的参数数组,例如: new object[] { arg1, arg2 }
  67. //var isAsync = (method.ReturnType == typeof(Task) ||
  68. // (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)));
  69. //if (isAsync)
  70. //{
  71. // throw new Exception("该方法不支持异步");
  72. //}
  73. method.Invoke(instance, args);
  74. return;
  75. }
  76. }
  77. }