Interpreter.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Reflection;
  8. using System.Collections.Specialized;
  9. using System.Linq.Expressions;
  10. namespace System.Linq.jvm
  11. {
  12. public class Interpreter
  13. {
  14. private class InternalVoidSubstitute
  15. {
  16. }
  17. private static readonly Type VOID_SUBSTITUTE =
  18. typeof(InternalVoidSubstitute);
  19. LambdaExpression _expression;
  20. static MethodInfo [] _delegateMap = null;
  21. private const int MapSize = 5;
  22. static Interpreter()
  23. {
  24. InitDelegateMap();
  25. }
  26. private static void InitDelegateMap()
  27. {
  28. MethodInfo[] mia =
  29. typeof(Interpreter).GetMethods(
  30. BindingFlags.Instance |
  31. BindingFlags.Public);
  32. _delegateMap = new MethodInfo[MapSize];
  33. foreach (MethodInfo m in mia)
  34. {
  35. if (m.Name == "GetDelegate")
  36. {
  37. _delegateMap[m.GetGenericArguments().Length - 1] = m;
  38. }
  39. }
  40. }
  41. public LambdaExpression Expression
  42. {
  43. get { return _expression; }
  44. }
  45. public Interpreter(LambdaExpression expression)
  46. {
  47. _expression = expression;
  48. }
  49. public Delegate CreateDelegate()
  50. {
  51. Type[] arr = ExtractGenerecParameters();
  52. MethodInfo mi = _delegateMap[arr.Length - 1];
  53. MethodInfo mgi = mi.MakeGenericMethod(arr);
  54. return (Delegate)mgi.Invoke(this, new object[0]);
  55. }
  56. public void Validate()
  57. {
  58. ExpressionValidator validator = new ExpressionValidator(this.Expression);
  59. validator.Validate();
  60. }
  61. private Type[] ExtractGenerecParameters()
  62. {
  63. Type[] arr = new Type[Expression.Parameters.Count + 1];
  64. Type rt = Expression.GetReturnType();
  65. if (rt == typeof(void))
  66. {
  67. rt = VOID_SUBSTITUTE;
  68. }
  69. arr[Expression.Parameters.Count] = rt;
  70. for (int i = 0; i < Expression.Parameters.Count; i++)
  71. {
  72. arr[i] = Expression.Parameters[i].Type;
  73. }
  74. return arr;
  75. }
  76. private object Run(object[] arg)
  77. {
  78. ExpressionInterpreter inter = new ExpressionInterpreter(arg);
  79. inter.Run((LambdaExpression)_expression);
  80. return inter.Value;
  81. }
  82. public Delegate GetDelegate<TResult>()
  83. {
  84. if (typeof(TResult) == VOID_SUBSTITUTE)
  85. {
  86. return new Action(this.ActionAccessor);
  87. }
  88. return new Func<TResult>(this.FuncAccessor<TResult>);
  89. }
  90. public TResult FuncAccessor<TResult>()
  91. {
  92. return (TResult) Run(new object[0]);
  93. }
  94. public void ActionAccessor()
  95. {
  96. Run(new object[0]);
  97. }
  98. public Delegate GetDelegate<T, TResult>()
  99. {
  100. if (typeof(TResult) == VOID_SUBSTITUTE)
  101. {
  102. return new Action<T>(this.ActionAccessor<T>);
  103. }
  104. return new Func<T, TResult>(this.FuncAccessor<T, TResult>);
  105. }
  106. public TResult FuncAccessor<T, TResult>(T arg)
  107. {
  108. return (TResult)Run(new object[] { arg });
  109. }
  110. public void ActionAccessor<T>(T arg)
  111. {
  112. Run(new object[] { arg });
  113. }
  114. public Delegate GetDelegate<T1, T2, TResult>()
  115. {
  116. if (typeof(TResult) == VOID_SUBSTITUTE)
  117. {
  118. return new Action<T1, T2>(this.ActionAccessor<T1, T2>);
  119. }
  120. return new Func<T1, T2, TResult>(this.FuncAccessor<T1, T2, TResult>);
  121. }
  122. public TResult FuncAccessor<T1, T2, TResult>(T1 arg1, T2 arg2)
  123. {
  124. return (TResult)Run(new object[] { arg1, arg2 });
  125. }
  126. public void ActionAccessor<T1, T2>(T1 arg1, T2 arg2)
  127. {
  128. Run(new object[] { arg1, arg2 });
  129. }
  130. public Delegate GetDelegate<T1, T2, T3, TResult>()
  131. {
  132. if (typeof(TResult) == VOID_SUBSTITUTE)
  133. {
  134. return new Action<T1, T2, T3>(this.ActionAccessor<T1, T2, T3>);
  135. }
  136. return new Func<T1, T2, T3, TResult>(this.FuncAccessor<T1, T2, T3, TResult>);
  137. }
  138. public TResult FuncAccessor<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3)
  139. {
  140. return (TResult)Run(new object[] { arg1, arg2, arg3 });
  141. }
  142. public void ActionAccessor<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)
  143. {
  144. Run(new object[] { arg1, arg2, arg3 });
  145. }
  146. public Delegate GetDelegate<T1, T2, T3, T4, TResult>()
  147. {
  148. if (typeof(TResult) == VOID_SUBSTITUTE)
  149. {
  150. return new Action<T1, T2, T3, T4>(this.ActionAccessor<T1, T2, T3, T4>);
  151. }
  152. return new Func<T1, T2, T3, T4, TResult>(this.FuncAccessor<T1, T2, T3, T4, TResult>);
  153. }
  154. public TResult FuncAccessor<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  155. {
  156. return (TResult)Run(new object[] { arg1, arg2, arg3, arg4});
  157. }
  158. public void ActionAccessor<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
  159. {
  160. Run(new object[] { arg1, arg2, arg3, arg4 });
  161. }
  162. }
  163. }