PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/IronPython_Main/Runtime/Samples/ExpressionTree/UESamples/ConsoleApplication1/CMultiply.cs

#
C# | 333 lines | 190 code | 46 blank | 97 comment | 18 complexity | 2a4fa2779befe4ce0093b13171981f23 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Scripting.Ast;
  5. namespace Samples {
  6. class CMultiply {
  7. //Multiply(Expression, Expression)
  8. public static void MultiplySample1() {
  9. //<Snippet1>
  10. //This expression multiplies the values of its two arguments.
  11. //Both arguments need to be of the same type.
  12. Expression MyMultiply = Expression.Multiply(
  13. Expression.Constant(2),
  14. Expression.Constant(3)
  15. );
  16. //The end result should be six:
  17. Console.WriteLine(Expression.Lambda<Func<int>>(MyMultiply).Compile().Invoke());
  18. //</Snippet1>
  19. //validate sample.
  20. if (Expression.Lambda<Func<int>>(MyMultiply).Compile().Invoke() != 6) throw new Exception();
  21. }
  22. //Multiply(Expression, Expression, MethodInfo)
  23. //<Snippet2>
  24. public static int Multiply(int arg1, Exception arg2) {
  25. return arg1 * Convert.ToInt32(arg2.Message);
  26. }
  27. //</Snippet2>
  28. public static void MultiplySample2() {
  29. //<Snippet2>
  30. //This expression represents the multiplication of two arguments using a user defined operator.
  31. //The parameters to the multiply should be reference convertible to the MethodInfo's arguments
  32. Expression MyMultiply = Expression.Multiply(
  33. Expression.Constant(6),
  34. Expression.Constant(new Exception("2")),
  35. ((Func<int, Exception, int>)Multiply).Method
  36. );
  37. //The end result should be twelve:
  38. Console.WriteLine(Expression.Lambda<Func<int>>(MyMultiply).Compile().Invoke());
  39. //</Snippet2>
  40. //validate sample.
  41. if (Expression.Lambda<Func<int>>(MyMultiply).Compile().Invoke() != 12) throw new Exception();
  42. }
  43. //MultiplyAssign(Expression, Expression)
  44. public static void MultiplyAssignSample1() {
  45. //<Snippet5>
  46. //MultiplyAssign requires an assignable expression to be used as the left argument.
  47. ParameterExpression Variable = Expression.Variable(typeof(int), "Variable");
  48. //Here we initialize the variable with 6, then use it in an MultiplyAssign expression.
  49. //both the MultiplyAssign expression and the variable will have the value 18 after the
  50. //tree is executed.
  51. Expression MyMultiplyAssign = Expression.Block(
  52. new ParameterExpression[] { Variable },
  53. Expression.Assign(Variable, Expression.Constant(6)),
  54. Expression.MultiplyAssign(
  55. Variable,
  56. Expression.Constant(3)
  57. )
  58. );
  59. //The result should be 18.
  60. Console.WriteLine(Expression.Lambda<Func<int>>(MyMultiplyAssign).Compile().Invoke());
  61. //</Snippet5>
  62. //validate sample.
  63. if (Expression.Lambda<Func<int>>(MyMultiplyAssign).Compile().Invoke() != 18) throw new Exception();
  64. }
  65. //MultiplyAssign(Expression, Expression, MethodInfo)
  66. //<Snippet6>
  67. public static int MultiplyAssign(int arg1, double arg2) {
  68. return arg1 * (int)arg2;
  69. }
  70. //</Snippet6>
  71. public static void MultiplyAssignSample2() {
  72. //<Snippet6>
  73. //MultiplyAssign requires an assignable expression to be used as the left argument.
  74. ParameterExpression Variable = Expression.Variable(typeof(int), "Variable");
  75. //Here we initialize the variable with 6, then use it in an MultiplyAssign expression
  76. //with a user defined method.
  77. Expression MyMultiplyAssign = Expression.Block(
  78. new ParameterExpression[] { Variable },
  79. Expression.Assign(Variable, Expression.Constant(6)),
  80. Expression.MultiplyAssign(
  81. Variable,
  82. Expression.Constant((double)3),
  83. ((Func<int, double, int>)MultiplyAssign).Method
  84. )
  85. );
  86. //The result should be 18.
  87. Console.WriteLine(Expression.Lambda<Func<int>>(MyMultiplyAssign).Compile().Invoke());
  88. //</Snippet6>
  89. //validate sample.
  90. if (Expression.Lambda<Func<int>>(MyMultiplyAssign).Compile().Invoke() != 18) throw new Exception();
  91. }
  92. //MultiplyAssign(Expression, Expression, MethodInfo, LambdaExpression)
  93. //<Snippet7>
  94. public static double MultiplyAssignDouble(int arg1, double arg2) {
  95. return (double)arg1 * arg2;
  96. }
  97. //</Snippet7>
  98. public static void MultiplyAssignSample3() {
  99. //<Snippet7>
  100. //MultiplyAssign requires an assignable expression to be used as the left argument.
  101. ParameterExpression Variable = Expression.Variable(typeof(int), "Variable");
  102. //This overload of MultiplyAssign also requires a conversion lambda. This is the
  103. //Lambda's parameter
  104. ParameterExpression Param1 = Expression.Parameter(typeof(double), "Parameter 1");
  105. //Here we initialize the variable with 6, then use it in an MultiplyAssign expression
  106. //with a user defined method.
  107. //Since the return type of the method is double, we need to provide a conversion
  108. //to the variable's type.
  109. Expression MyMultiplyAssign = Expression.Block(
  110. new ParameterExpression[] { Variable },
  111. Expression.Assign(Variable, Expression.Constant(6)),
  112. Expression.MultiplyAssign(
  113. Variable,
  114. Expression.Constant((double)3),
  115. ((Func<int, double, double>)MultiplyAssignDouble).Method,
  116. Expression.Lambda<Func<double, int>>(
  117. Expression.Convert(Param1, typeof(int)),
  118. new ParameterExpression[] { Param1 }
  119. )
  120. )
  121. );
  122. //The result should be 18.
  123. Console.WriteLine(Expression.Lambda<Func<int>>(MyMultiplyAssign).Compile().Invoke());
  124. //</Snippet7>
  125. //validate sample.
  126. if (Expression.Lambda<Func<int>>(MyMultiplyAssign).Compile().Invoke() != 18) throw new Exception();
  127. }
  128. //MultiplyChecked(Expression, Expression)
  129. static public void MultiplyCheckedSample1() {
  130. //<Snippet8>
  131. //This expression multiplies the values of its two arguments.
  132. //Both arguments need to be of the same type.
  133. //If the result is larger than the type of the operation,
  134. //an OverflowException is thrown.
  135. Expression MyMultiply = Expression.MultiplyChecked(
  136. Expression.Constant(int.MaxValue),
  137. Expression.Constant(2)
  138. );
  139. //An exception should happen:
  140. try {
  141. Expression.Lambda<Func<int>>(MyMultiply).Compile().Invoke();
  142. } catch (OverflowException) {
  143. Console.WriteLine("Expected exception thrown");
  144. }
  145. //</Snippet8>
  146. //validate sample.
  147. try {
  148. Expression.Lambda<Func<int>>(MyMultiply).Compile().Invoke();
  149. throw new Exception("Expected Overflow Exception, no exception thrown.");
  150. } catch (OverflowException) {
  151. }
  152. }
  153. //MultiplyChecked(Expression, Expression, MethodInfo)
  154. //<Snippet9>
  155. public static Int64 MultiplyChecked(int arg1, Exception arg2) {
  156. Int64 res = (Int64)arg1 * Convert.ToInt64(arg2.Message);
  157. if (res > Int64.MaxValue || res < Int64.MinValue) throw new OverflowException();
  158. return res;
  159. }
  160. //</Snippet9>
  161. public static void MultiplyCheckedSample2() {
  162. //<Snippet9>
  163. //This expression represents the multiplication of two arguments using a user defined operator.
  164. //The parameters to the multiplication should be reference convertible to the MethodInfo's arguments
  165. Expression MyMultiply = Expression.MultiplyChecked(
  166. Expression.Constant(Int32.MaxValue),
  167. Expression.Constant(new Exception("2")),
  168. ((Func<int, Exception, Int64>)MultiplyChecked).Method
  169. );
  170. Console.WriteLine(Expression.Lambda<Func<Int64>>(MyMultiply).Compile().Invoke());
  171. //</Snippet9>
  172. //validate sample.
  173. if (Expression.Lambda<Func<Int64>>(MyMultiply).Compile().Invoke() != (Int32.MaxValue * (Int64)2)) throw new Exception();
  174. }
  175. //MultiplyAssignChecked(Expression, Expression)
  176. public static void MultiplyAssignCheckedSample1() {
  177. //<Snippet10>
  178. //MultiplyAssign requires an assignable expression to be used as the left argument.
  179. ParameterExpression Variable = Expression.Variable(typeof(int), "Variable");
  180. //Here we initialize the variable with Int32.MaxValue, then use it in an MultiplyAssign expression.
  181. Expression MyMultiplyAssignChecked = Expression.Block(
  182. new ParameterExpression[] { Variable },
  183. Expression.Assign(Variable, Expression.Constant(Int32.MaxValue)),
  184. Expression.MultiplyAssignChecked(
  185. Variable,
  186. Expression.Constant(2)
  187. )
  188. );
  189. //An exception should happen:
  190. try {
  191. Expression.Lambda<Func<int>>(MyMultiplyAssignChecked).Compile().Invoke();
  192. } catch (OverflowException) {
  193. Console.WriteLine("Expected exception thrown");
  194. }
  195. //</Snippet10>
  196. //validate sample.
  197. try {
  198. Expression.Lambda<Func<int>>(MyMultiplyAssignChecked).Compile().Invoke();
  199. throw new Exception("Expected Overflow Exception, no exception thrown.");
  200. } catch (OverflowException) {
  201. }
  202. }
  203. //MultiplyAssignChecked(Expression, Expression, MethodInfo)
  204. //<Snippet11>
  205. public static int MultiplyAssignChecked(int arg1, double arg2) {
  206. Int64 res = (Int64)arg1 * Convert.ToInt64(arg2);
  207. if (res > Int32.MaxValue || res < Int32.MinValue) throw new OverflowException();
  208. return (int)res;
  209. }
  210. //</Snippet11>
  211. public static void MultiplyAssignCheckedSample2() {
  212. //<Snippet11>
  213. //MultiplyAssign requires an assignable expression to be used as the left argument.
  214. ParameterExpression Variable = Expression.Variable(typeof(int), "Variable");
  215. //Here we initialize the variable with Int32.MaxValue, then use it in an MultiplyAssign expression
  216. //with a user defined method.
  217. Expression MyMultiplyAssignChecked = Expression.Block(
  218. new ParameterExpression[] { Variable },
  219. Expression.Assign(Variable, Expression.Constant(Int32.MaxValue)),
  220. Expression.MultiplyAssignChecked(
  221. Variable,
  222. Expression.Constant((double)2.0),
  223. ((Func<int, double, int>)MultiplyAssignChecked).Method
  224. )
  225. );
  226. //An exception should happen:
  227. try {
  228. Expression.Lambda<Func<int>>(MyMultiplyAssignChecked).Compile().Invoke();
  229. } catch (OverflowException) {
  230. Console.WriteLine("Expected exception thrown");
  231. }
  232. //</Snippet11>
  233. //validate sample.
  234. try {
  235. Expression.Lambda<Func<int>>(MyMultiplyAssignChecked).Compile().Invoke();
  236. throw new Exception("Expected Overflow Exception, no exception thrown.");
  237. } catch (OverflowException) {
  238. }
  239. }
  240. //MultiplyAssign(Expression, Expression, MethodInfo, LambdaExpression)
  241. //<Snippet12>
  242. public static double MultiplyAssignCheckedDouble(int arg1, double arg2) {
  243. Int64 res = (Int64)arg1 * Convert.ToInt64(arg2);
  244. if (res > Int32.MaxValue || res < Int32.MinValue) throw new OverflowException();
  245. return (double)res;
  246. }
  247. //</Snippet12>
  248. public static void MultiplyAssignCheckedSample3() {
  249. //<Snippet10>
  250. //MultiplyAssign requires an assignable expression to be used as the left argument.
  251. ParameterExpression Variable = Expression.Variable(typeof(int), "Variable");
  252. //This overload of MultiplyAssign also requires a conversion lambda. This is the
  253. //Lambda's parameter
  254. ParameterExpression Param1 = Expression.Parameter(typeof(double), "Parameter 1");
  255. //Here we initialize the variable with Int32.MaxValue, then use it in an MultiplyAssign expression
  256. //with a user defined method.
  257. //Since the return type of the method is double, we need to provide a conversion
  258. //to the variable's type.
  259. Expression MyMultiplyAssignChecked = Expression.Block(
  260. new ParameterExpression[] { Variable },
  261. Expression.Assign(Variable, Expression.Constant(Int32.MaxValue)),
  262. Expression.MultiplyAssignChecked(
  263. Variable,
  264. Expression.Constant((double)2.0),
  265. ((Func<int, double, double>)MultiplyAssignCheckedDouble).Method,
  266. Expression.Lambda<Func<double, int>>(
  267. Expression.Convert(Param1, typeof(int)),
  268. new ParameterExpression[] { Param1 }
  269. )
  270. )
  271. );
  272. //An exception should happen:
  273. try {
  274. Expression.Lambda<Func<int>>(MyMultiplyAssignChecked).Compile().Invoke();
  275. } catch (OverflowException) {
  276. Console.WriteLine("Expected exception thrown");
  277. }
  278. //</Snippet11>
  279. //validate sample.
  280. try {
  281. Expression.Lambda<Func<int>>(MyMultiplyAssignChecked).Compile().Invoke();
  282. throw new Exception("Expected Overflow Exception, no exception thrown.");
  283. } catch (OverflowException) {
  284. }
  285. }
  286. }
  287. }