IAsyncStateMachineAdaptor.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using ILRuntime.CLR.Method;
  4. using ILRuntime.Runtime.Enviorment;
  5. using ILRuntime.Runtime.Intepreter;
  6. namespace Model
  7. {
  8. /// <summary>
  9. /// 用于async await适配
  10. /// </summary>
  11. public class IAsyncStateMachineClassInheritanceAdaptor : CrossBindingAdaptor
  12. {
  13. public override Type BaseCLRType
  14. {
  15. get
  16. {
  17. return typeof (IAsyncStateMachine);
  18. }
  19. }
  20. public override Type AdaptorType
  21. {
  22. get
  23. {
  24. return typeof (IAsyncStateMachineAdaptor);
  25. }
  26. }
  27. public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
  28. {
  29. return new IAsyncStateMachineAdaptor(appdomain, instance);
  30. }
  31. public class IAsyncStateMachineAdaptor: IAsyncStateMachine, CrossBindingAdaptorType
  32. {
  33. private ILTypeInstance instance;
  34. private ILRuntime.Runtime.Enviorment.AppDomain appDomain;
  35. private IMethod mMoveNext;
  36. private IMethod mSetStateMachine;
  37. private readonly object[] param1 = new object[1];
  38. public IAsyncStateMachineAdaptor()
  39. {
  40. }
  41. public IAsyncStateMachineAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appDomain, ILTypeInstance instance)
  42. {
  43. this.appDomain = appDomain;
  44. this.instance = instance;
  45. }
  46. public ILTypeInstance ILInstance
  47. {
  48. get
  49. {
  50. return instance;
  51. }
  52. }
  53. public void MoveNext()
  54. {
  55. if (this.mMoveNext == null)
  56. {
  57. mMoveNext = instance.Type.GetMethod("MoveNext", 0);
  58. }
  59. this.appDomain.Invoke(mMoveNext, instance, null);
  60. }
  61. public void SetStateMachine(IAsyncStateMachine stateMachine)
  62. {
  63. if (this.mSetStateMachine == null)
  64. {
  65. mSetStateMachine = instance.Type.GetMethod("SetStateMachine");
  66. }
  67. this.appDomain.Invoke(mSetStateMachine, instance, stateMachine);
  68. }
  69. public override string ToString()
  70. {
  71. IMethod m = this.appDomain.ObjectType.GetMethod("ToString", 0);
  72. m = instance.Type.GetVirtualMethod(m);
  73. if (m == null || m is ILMethod)
  74. {
  75. return instance.ToString();
  76. }
  77. return instance.Type.FullName;
  78. }
  79. }
  80. }
  81. }