SimpleReflection.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Y.Utils.IOUtils.FileUtils;
  6. using Y.Utils.IOUtils.LogUtils;
  7. namespace Y.Utils.ReflectionUtils.ReflectionCoreUtils
  8. {
  9. public class SimpleReflection : MarshalByRefObject
  10. {
  11. //public string AssemblyPath { get; set; }
  12. //public SimpleReflection(string assemblyPath)
  13. //{
  14. // AssemblyPath = assemblyPath;
  15. //}
  16. //public Assembly AssemblyResolve(object sender, ResolveEventArgs args)
  17. //{
  18. // return SearchAssembly(AssemblyPath, args.Name);
  19. //}
  20. private Assembly SearchAssembly(string path, string name)
  21. {
  22. try
  23. {
  24. List<string> dlls = FileTool.GetFile(path, "*.dll");
  25. foreach (var dll in dlls)
  26. {
  27. Assembly ass = Assembly.LoadFile(dll);
  28. if (ass.FullName == name)
  29. return ass;
  30. }
  31. }
  32. catch (Exception e)
  33. {
  34. Log.e(e.Message);
  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. }