PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Scripting/Script/Call.cs

http://github.com/polyethene/IronAHK
C# | 49 lines | 42 code | 7 blank | 0 comment | 8 complexity | 6d4e4694edaad93d74325abb36249408 MD5 | raw file
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. namespace IronAHK.Scripting
  5. {
  6. partial class Script
  7. {
  8. public static object Invoke(object del, params object[] parameters)
  9. {
  10. if (!(del is Delegate))
  11. return null;
  12. try
  13. {
  14. return ((Delegate)del).DynamicInvoke(new object[] { parameters });
  15. }
  16. catch (Exception)
  17. {
  18. return null;
  19. }
  20. }
  21. public static object FunctionCall(string name, params object[] parameters)
  22. {
  23. var stack = new StackTrace(false).GetFrames();
  24. MethodInfo method = null;
  25. for (int i = 0; i < 3; i++)
  26. {
  27. var type = stack[i].GetMethod().DeclaringType;
  28. method = FindMethod(name, type.GetMethods(), parameters);
  29. if (method != null)
  30. break;
  31. }
  32. return method == null || !method.IsStatic ? null : method.Invoke(null, new object[] { parameters });
  33. }
  34. static MethodInfo FindMethod(string name, MethodInfo[] list, object[] parameters)
  35. {
  36. for (int i = 0; i < list.Length; i++)
  37. if (list[i].Name.Equals(name, StringComparison.OrdinalIgnoreCase))
  38. return list[i];
  39. return null;
  40. }
  41. }
  42. }