Yield.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace CLRSharp
  6. {
  7. public class CrossBind_IEnumerable : ICrossBind
  8. {
  9. public Type Type
  10. {
  11. get { return typeof(IEnumerable); }
  12. }
  13. public object CreateBind(CLRSharp_Instance inst)
  14. {
  15. return new Base_IEnumerable(inst);
  16. }
  17. class Base_IEnumerable : IEnumerable
  18. {
  19. CLRSharp_Instance inst;
  20. public Base_IEnumerable(CLRSharp_Instance inst)
  21. {
  22. this.inst = inst;
  23. }
  24. public IEnumerator GetEnumerator()
  25. {
  26. var context = ThreadContext.activeContext;
  27. var _type = context.environment.GetType(typeof(IEnumerable));
  28. var _method = this.inst.type.GetMethod(_type.FullName+"."+"GetEnumerator", MethodParamList.constEmpty());
  29. var obj = _method.Invoke(context, inst, null) as CLRSharp_Instance;
  30. return context.environment.GetCrossBind(typeof(IEnumerator)).CreateBind(obj) as IEnumerator;
  31. }
  32. }
  33. }
  34. public class CrossBind_IEnumerator : ICrossBind
  35. {
  36. public Type Type
  37. {
  38. get { return typeof(IEnumerator); }
  39. }
  40. public object CreateBind(CLRSharp_Instance inst)
  41. {
  42. return new Base_IEnumerator(inst);
  43. }
  44. class Base_IEnumerator : IEnumerator
  45. {
  46. CLRSharp_Instance inst;
  47. public Base_IEnumerator(CLRSharp_Instance inst)
  48. {
  49. var context = ThreadContext.activeContext;
  50. this.inst = inst;
  51. var ms = this.inst.type.GetMethodNames();
  52. foreach(string name in ms)
  53. {
  54. if(name.Contains("MoveNext"))
  55. _MoveNext = this.inst.type.GetMethod(name, MethodParamList.constEmpty());
  56. if (name.Contains(".get_Current"))
  57. _get_Current = this.inst.type.GetMethod(name, MethodParamList.constEmpty());
  58. if (name.Contains(".Reset"))
  59. _Reset = this.inst.type.GetMethod(name, MethodParamList.constEmpty());
  60. }
  61. }
  62. IMethod _MoveNext;
  63. IMethod _get_Current;
  64. IMethod _Reset;
  65. public object Current
  66. {
  67. get
  68. {
  69. var context = ThreadContext.activeContext;
  70. var obj = _get_Current.Invoke(context, inst, null);
  71. return obj;
  72. }
  73. }
  74. public bool MoveNext()
  75. {
  76. var context = ThreadContext.activeContext;
  77. var obj = _MoveNext.Invoke(context, inst, null) as VBox;
  78. return obj.ToBool();
  79. }
  80. public void Reset()
  81. {
  82. var context = ThreadContext.activeContext;
  83. var obj = _Reset.Invoke(context, inst, null);
  84. }
  85. }
  86. }
  87. }