IAsyncStateMachineAdaptor.cs 2.0 KB

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