SimpleReflection.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using Azylee.Core.IOUtils.FileUtils;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Reflection;
  10. namespace Azylee.Core.ReflectionUtils.ReflectionCoreUtils
  11. {
  12. public class SimpleReflection : MarshalByRefObject
  13. {
  14. //public string AssemblyPath { get; set; }
  15. //public SimpleReflection(string assemblyPath)
  16. //{
  17. // AssemblyPath = assemblyPath;
  18. //}
  19. //public Assembly AssemblyResolve(object sender, ResolveEventArgs args)
  20. //{
  21. // return SearchAssembly(AssemblyPath, args.Name);
  22. //}
  23. private Assembly SearchAssembly(string path, string name)
  24. {
  25. List<string> dlls = FileTool.GetFile(path, "*.dll");
  26. foreach (var dll in dlls)
  27. {
  28. try
  29. {
  30. Assembly ass = Assembly.LoadFile(dll);
  31. if (ass.FullName == name)
  32. return ass;
  33. }
  34. catch { }
  35. }
  36. return null;
  37. }
  38. public T Do<T>(string file, string className, string methodName, object[] args, object[] values)
  39. {
  40. //获取dll中所有类
  41. Type[] types = Assembly.LoadFile(file).GetTypes();
  42. //从列表中获取指定类
  43. Type cls = types.FirstOrDefault(x => x.FullName.Contains(className));
  44. if (cls != null)
  45. {
  46. //创建实例
  47. object instance = Activator.CreateInstance(cls, args);
  48. //根据名称获取方法
  49. MethodInfo method = cls.GetMethod(methodName);
  50. //执行方法
  51. object result = method.Invoke(instance, values);
  52. return (T)result;
  53. }
  54. return default(T);
  55. }
  56. }
  57. }