PageRenderTime 49ms CodeModel.GetById 15ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

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

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