AdaptHelper.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 
  2. using System;
  3. using ILRuntime.CLR.Method;
  4. using ILRuntime.CLR.TypeSystem;
  5. using ILRuntime.Runtime.Enviorment;
  6. using ILRuntime.Runtime.Intepreter;
  7. using AppDomain = ILRuntime.Runtime.Enviorment.AppDomain;
  8. public static class AdaptHelper
  9. {
  10. public class AdaptMethod
  11. {
  12. public string Name;
  13. public int ParamCount;
  14. public IMethod Method;
  15. }
  16. public static IMethod GetMethod(this ILType type, AdaptMethod m)
  17. {
  18. if (m.Method != null)
  19. return m.Method;
  20. m.Method = type.GetMethod(m.Name, m.ParamCount);
  21. if (m.Method == null)
  22. {
  23. string baseClass = "";
  24. if (type.FirstCLRBaseType != null)
  25. {
  26. baseClass = type.FirstCLRBaseType.FullName;
  27. }
  28. else if (type.FirstCLRInterface != null)
  29. {
  30. baseClass = type.FirstCLRInterface.FullName;
  31. }
  32. throw new Exception(string.Format("can't find the method: {0}.{1}:{2}, paramCount={3}", type.FullName, m.Name, baseClass, m.ParamCount));
  33. }
  34. return m.Method;
  35. }
  36. }
  37. public abstract class MyAdaptor : CrossBindingAdaptorType
  38. {
  39. protected AppDomain AppDomain { get; set; }
  40. protected ILTypeInstance _instance;
  41. private AdaptHelper.AdaptMethod[] _methods;
  42. protected abstract AdaptHelper.AdaptMethod[] GetAdaptMethods();
  43. public ILTypeInstance ILInstance
  44. {
  45. get { return _instance; }
  46. set { _instance = value; }
  47. }
  48. protected object Invoke(int index, params object[] p)
  49. {
  50. if (_methods == null)
  51. _methods = GetAdaptMethods();
  52. var m = _instance.Type.GetMethod(_methods[index]);
  53. return AppDomain.Invoke(m, _instance, p);
  54. }
  55. protected MyAdaptor(AppDomain appdomain, ILTypeInstance instance)
  56. {
  57. AppDomain = appdomain;
  58. _instance = instance;
  59. }
  60. }