5 // Jb Evain (jbevain@novell.com)
6 // Miguel de Icaza (miguel@novell.com)
8 // (C) 2008 Novell, Inc. (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.ObjectModel;
34 using System.Globalization;
36 using System.Reflection;
37 using System.Reflection.Emit;
39 namespace System.Linq.Expressions {
41 public abstract class Expression {
43 ExpressionType node_type;
46 internal const BindingFlags PublicInstance = BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
47 internal const BindingFlags NonPublicInstance = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
48 internal const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
49 internal const BindingFlags AllInstance = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
50 internal const BindingFlags AllStatic = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy;
51 internal const BindingFlags All = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
53 public ExpressionType NodeType {
54 get { return node_type; }
61 protected Expression (ExpressionType node_type, Type type)
63 this.node_type = node_type;
67 public override string ToString ()
69 return ExpressionPrinter.ToString (this);
72 #region Binary Expressions
74 static MethodInfo GetUnaryOperator (string oper_name, Type declaring, Type param)
76 return GetUnaryOperator (oper_name, declaring, param, null);
79 static MethodInfo GetUnaryOperator (string oper_name, Type declaring, Type param, Type ret)
81 var methods = declaring.GetNotNullableType ().GetMethods (PublicStatic);
83 foreach (var method in methods) {
84 if (method.Name != oper_name)
87 var parameters = method.GetParameters ();
88 if (parameters.Length != 1)
91 if (method.IsGenericMethod)
94 if (!IsAssignableToParameterType (param.GetNotNullableType (), parameters [0]))
97 if (ret != null && method.ReturnType != ret.GetNotNullableType ())
106 internal static MethodInfo GetTrueOperator (Type self)
108 return GetBooleanOperator ("op_True", self);
111 internal static MethodInfo GetFalseOperator (Type self)
113 return GetBooleanOperator ("op_False", self);
116 static MethodInfo GetBooleanOperator (string op, Type self)
118 return GetUnaryOperator (op, self, self, typeof (bool));
121 static bool IsAssignableToParameterType (Type type, ParameterInfo param)
123 var ptype = param.ParameterType;
125 ptype = ptype.GetElementType ();
127 return type.GetNotNullableType ().IsAssignableTo (ptype);
130 static MethodInfo CheckUnaryMethod (MethodInfo method, Type param)
132 if (method.ReturnType == typeof (void))
133 throw new ArgumentException ("Specified method must return a value", "method");
135 if (!method.IsStatic)
136 throw new ArgumentException ("Method must be static", "method");
138 var parameters = method.GetParameters ();
140 if (parameters.Length != 1)
141 throw new ArgumentException ("Must have only one parameters", "method");
143 if (!IsAssignableToParameterType (param.GetNotNullableType (), parameters [0]))
144 throw new InvalidOperationException ("left-side argument type does not match expression type");
149 static MethodInfo UnaryCoreCheck (string oper_name, Expression expression, MethodInfo method, Func<Type, bool> validator)
151 if (expression == null)
152 throw new ArgumentNullException ("expression");
155 return CheckUnaryMethod (method, expression.Type);
157 var type = expression.Type.GetNotNullableType ();
159 if (validator (type))
162 if (oper_name != null) {
163 method = GetUnaryOperator (oper_name, type, expression.Type);
168 throw new InvalidOperationException (
169 string.Format ("Operation {0} not defined for {1}", oper_name != null ? oper_name.Substring (3) : "is", expression.Type));
172 static MethodInfo GetBinaryOperator (string oper_name, Type on_type, Expression left, Expression right)
174 MethodInfo [] methods = on_type.GetMethods (PublicStatic);
176 foreach (var method in methods) {
177 if (method.Name != oper_name)
180 var parameters = method.GetParameters ();
181 if (parameters.Length != 2)
184 if (method.IsGenericMethod)
187 if (!IsAssignableToParameterType (left.Type, parameters [0]))
190 if (!IsAssignableToParameterType (right.Type, parameters [1]))
193 // Method has papers in order.
201 // Performs basic checks on the incoming expressions for binary expressions
202 // and any provided MethodInfo.
204 static MethodInfo BinaryCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
207 throw new ArgumentNullException ("left");
209 throw new ArgumentNullException ("right");
212 if (method.ReturnType == typeof (void))
213 throw new ArgumentException ("Specified method must return a value", "method");
215 if (!method.IsStatic)
216 throw new ArgumentException ("Method must be static", "method");
218 var parameters = method.GetParameters ();
220 if (parameters.Length != 2)
221 throw new ArgumentException ("Must have only two parameters", "method");
223 if (!IsAssignableToParameterType (left.Type, parameters [0]))
224 throw new InvalidOperationException ("left-side argument type does not match left expression type");
226 if (!IsAssignableToParameterType (right.Type, parameters [1]))
227 throw new InvalidOperationException ("right-side argument type does not match right expression type");
231 Type ltype = left.Type;
232 Type rtype = right.Type;
233 Type ultype = ltype.GetNotNullableType ();
234 Type urtype = rtype.GetNotNullableType ();
236 if (oper_name == "op_BitwiseOr" || oper_name == "op_BitwiseAnd") {
237 if (ultype == typeof (bool)) {
238 if (ultype == urtype && ltype == rtype)
243 // Use IsNumber to avoid expensive reflection.
244 if (IsNumber (ultype)) {
245 if (ultype == urtype && ltype == rtype && ultype != typeof (decimal))
248 if (oper_name != null){
249 method = GetBinaryOperator (oper_name, urtype, left, right);
255 if (oper_name != null){
256 method = GetBinaryOperator (oper_name, ultype, left, right);
261 if (oper_name == "op_Equality" || oper_name == "op_Inequality") {
263 // == and != allow reference types without operators defined.
265 if (!ltype.IsValueType && !rtype.IsValueType)
268 if (ltype == rtype && ultype == typeof (bool))
272 if (oper_name == "op_LeftShift" || oper_name == "op_RightShift") {
273 if (IsInt (ultype) && urtype == typeof (int))
277 throw new InvalidOperationException (
278 String.Format ("Operation {0} not defined for {1} and {2}", oper_name != null ? oper_name.Substring (3) : "is", ltype, rtype));
283 // This is like BinaryCoreCheck, but if no method is used adds the restriction that
284 // only ints and bools are allowed
286 static MethodInfo BinaryBitwiseCoreCheck (string oper_name, Expression left, Expression right, MethodInfo method)
289 throw new ArgumentNullException ("left");
291 throw new ArgumentNullException ("right");
293 if (method == null) {
294 // avoid reflection shortcut and catches Ints/bools before we check Numbers in general
295 if (left.Type == right.Type && IsIntOrBool (left.Type))
299 method = BinaryCoreCheck (oper_name, left, right, method);
300 if (method == null) {
301 // The check in BinaryCoreCheck allows a bit more than we do
302 // (floats and doubles). Catch this here
303 if (left.Type == typeof (double) || left.Type == typeof (float))
304 throw new InvalidOperationException ("Types not supported");
310 static BinaryExpression MakeSimpleBinary (ExpressionType et, Expression left, Expression right, MethodInfo method)
315 if (method == null) {
316 is_lifted = left.Type.IsNullable ();
319 var parameters = method.GetParameters ();
321 var lp = parameters [0];
322 var rp = parameters [1];
324 if (IsAssignableToOperatorParameter (left, lp) && IsAssignableToOperatorParameter (right, rp)) {
326 type = method.ReturnType;
327 } else if (left.Type.IsNullable ()
328 && right.Type.IsNullable ()
329 && left.Type.GetNotNullableType () == lp.ParameterType
330 && right.Type.GetNotNullableType () == rp.ParameterType
331 && !method.ReturnType.IsNullable ()) {
334 type = method.ReturnType.MakeNullableType ();
336 throw new InvalidOperationException ();
339 return new BinaryExpression (et, type, left, right, is_lifted, is_lifted, method, null);
342 static bool IsAssignableToOperatorParameter (Expression expression, ParameterInfo parameter)
344 if (expression.Type == parameter.ParameterType)
347 if ((!expression.Type.IsNullable () && !parameter.ParameterType.IsNullable ())
348 && IsAssignableToParameterType (expression.Type, parameter))
354 static UnaryExpression MakeSimpleUnary (ExpressionType et, Expression expression, MethodInfo method)
359 if (method == null) {
360 type = expression.Type;
361 is_lifted = type.IsNullable ();
363 var parameter = method.GetParameters () [0];
365 if (IsAssignableToOperatorParameter (expression, parameter)) {
367 type = method.ReturnType;
368 } else if (expression.Type.IsNullable ()
369 && expression.Type.GetNotNullableType () == parameter.ParameterType
370 && !method.ReturnType.IsNullable ()) {
373 type = method.ReturnType.MakeNullableType ();
375 throw new InvalidOperationException ();
378 return new UnaryExpression (et, expression, type, method, is_lifted);
381 static BinaryExpression MakeBoolBinary (ExpressionType et, Expression left, Expression right, bool liftToNull, MethodInfo method)
386 if (method == null) {
387 if (!left.Type.IsNullable () && !right.Type.IsNullable ()) {
390 type = typeof (bool);
391 } else if (left.Type.IsNullable () && right.Type.IsNullable ()) {
393 type = liftToNull ? typeof (bool?) : typeof (bool);
395 throw new InvalidOperationException ();
397 var parameters = method.GetParameters ();
399 var lp = parameters [0];
400 var rp = parameters [1];
402 if (IsAssignableToOperatorParameter (left, lp) && IsAssignableToOperatorParameter (right, rp)) {
405 type = method.ReturnType;
406 } else if (left.Type.IsNullable ()
407 && right.Type.IsNullable ()
408 && left.Type.GetNotNullableType () == lp.ParameterType
409 && right.Type.GetNotNullableType () == rp.ParameterType) {
413 if (method.ReturnType == typeof (bool))
414 type = liftToNull ? typeof (bool?) : typeof (bool);
415 else if (!method.ReturnType.IsNullable ()) {
417 // This behavior is not documented: what
418 // happens if the result is not typeof(bool), but
419 // the parameters are nullable: the result
420 // becomes nullable<returntype>
423 // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=323139
425 type = method.ReturnType.MakeNullableType ();
427 throw new InvalidOperationException ();
429 throw new InvalidOperationException ();
432 return new BinaryExpression (et, type, left, right, liftToNull, is_lifted, method, null);
438 public static BinaryExpression Add (Expression left, Expression right)
440 return Add (left, right, null);
443 public static BinaryExpression Add (Expression left, Expression right, MethodInfo method)
445 method = BinaryCoreCheck ("op_Addition", left, right, method);
447 return MakeSimpleBinary (ExpressionType.Add, left, right, method);
450 public static BinaryExpression AddChecked (Expression left, Expression right)
452 return AddChecked (left, right, null);
455 public static BinaryExpression AddChecked (Expression left, Expression right, MethodInfo method)
457 method = BinaryCoreCheck ("op_Addition", left, right, method);
459 // The check in BinaryCoreCheck allows a bit more than we do
460 // (byte, sbyte). Catch that here
461 if (method == null) {
462 if (left.Type == typeof (byte) || left.Type == typeof (sbyte))
463 throw new InvalidOperationException (String.Format ("AddChecked not defined for {0} and {1}", left.Type, right.Type));
466 return MakeSimpleBinary (ExpressionType.AddChecked, left, right, method);
469 public static BinaryExpression Subtract (Expression left, Expression right)
471 return Subtract (left, right, null);
474 public static BinaryExpression Subtract (Expression left, Expression right, MethodInfo method)
476 method = BinaryCoreCheck ("op_Subtraction", left, right, method);
478 return MakeSimpleBinary (ExpressionType.Subtract, left, right, method);
481 public static BinaryExpression SubtractChecked (Expression left, Expression right)
483 return SubtractChecked (left, right, null);
486 public static BinaryExpression SubtractChecked (Expression left, Expression right, MethodInfo method)
488 method = BinaryCoreCheck ("op_Subtraction", left, right, method);
490 // The check in BinaryCoreCheck allows a bit more than we do
491 // (byte, sbyte). Catch that here
492 if (method == null) {
493 if (left.Type == typeof (byte) || left.Type == typeof (sbyte))
494 throw new InvalidOperationException (String.Format ("SubtractChecked not defined for {0} and {1}", left.Type, right.Type));
497 return MakeSimpleBinary (ExpressionType.SubtractChecked, left, right, method);
500 public static BinaryExpression Modulo (Expression left, Expression right)
502 return Modulo (left, right, null);
505 public static BinaryExpression Modulo (Expression left, Expression right, MethodInfo method)
507 method = BinaryCoreCheck ("op_Modulus", left, right, method);
509 return MakeSimpleBinary (ExpressionType.Modulo, left, right, method);
512 public static BinaryExpression Multiply (Expression left, Expression right)
514 return Multiply (left, right, null);
517 public static BinaryExpression Multiply (Expression left, Expression right, MethodInfo method)
519 method = BinaryCoreCheck ("op_Multiply", left, right, method);
521 return MakeSimpleBinary (ExpressionType.Multiply, left, right, method);
524 public static BinaryExpression MultiplyChecked (Expression left, Expression right)
526 return MultiplyChecked (left, right, null);
529 public static BinaryExpression MultiplyChecked (Expression left, Expression right, MethodInfo method)
531 method = BinaryCoreCheck ("op_Multiply", left, right, method);
533 return MakeSimpleBinary (ExpressionType.MultiplyChecked, left, right, method);
536 public static BinaryExpression Divide (Expression left, Expression right)
538 return Divide (left, right, null);
541 public static BinaryExpression Divide (Expression left, Expression right, MethodInfo method)
543 method = BinaryCoreCheck ("op_Division", left, right, method);
545 return MakeSimpleBinary (ExpressionType.Divide, left, right, method);
548 public static BinaryExpression Power (Expression left, Expression right)
550 return Power (left, right, null);
553 public static BinaryExpression Power (Expression left, Expression right, MethodInfo method)
555 method = BinaryCoreCheck (null, left, right, method);
557 if (left.Type.GetNotNullableType () != typeof (double))
558 throw new InvalidOperationException ("Power only supports double arguments");
560 return MakeSimpleBinary (ExpressionType.Power, left, right, method);
566 public static BinaryExpression And (Expression left, Expression right)
568 return And (left, right, null);
571 public static BinaryExpression And (Expression left, Expression right, MethodInfo method)
573 method = BinaryBitwiseCoreCheck ("op_BitwiseAnd", left, right, method);
575 return MakeSimpleBinary (ExpressionType.And, left, right, method);
578 public static BinaryExpression Or (Expression left, Expression right)
580 return Or (left, right, null);
583 public static BinaryExpression Or (Expression left, Expression right, MethodInfo method)
585 method = BinaryBitwiseCoreCheck ("op_BitwiseOr", left, right, method);
587 return MakeSimpleBinary (ExpressionType.Or, left, right, method);
590 public static BinaryExpression ExclusiveOr (Expression left, Expression right)
592 return ExclusiveOr (left, right, null);
595 public static BinaryExpression ExclusiveOr (Expression left, Expression right, MethodInfo method)
597 method = BinaryBitwiseCoreCheck ("op_ExclusiveOr", left, right, method);
599 return MakeSimpleBinary (ExpressionType.ExclusiveOr, left, right, method);
602 public static BinaryExpression LeftShift (Expression left, Expression right)
604 return LeftShift (left, right, null);
607 public static BinaryExpression LeftShift (Expression left, Expression right, MethodInfo method)
609 method = BinaryBitwiseCoreCheck ("op_LeftShift", left, right, method);
611 return MakeSimpleBinary (ExpressionType.LeftShift, left, right, method);
614 public static BinaryExpression RightShift (Expression left, Expression right)
616 return RightShift (left, right, null);
619 public static BinaryExpression RightShift (Expression left, Expression right, MethodInfo method)
621 method = BinaryCoreCheck ("op_RightShift", left, right, method);
623 return MakeSimpleBinary (ExpressionType.RightShift, left, right, method);
629 public static BinaryExpression AndAlso (Expression left, Expression right)
631 return AndAlso (left, right, null);
634 public static BinaryExpression AndAlso (Expression left, Expression right, MethodInfo method)
636 method = ConditionalBinaryCheck ("op_BitwiseAnd", left, right, method);
638 return MakeBoolBinary (ExpressionType.AndAlso, left, right, true, method);
641 static MethodInfo ConditionalBinaryCheck (string oper, Expression left, Expression right, MethodInfo method)
643 method = BinaryCoreCheck (oper, left, right, method);
645 if (method == null) {
646 if (left.Type.GetNotNullableType () != typeof (bool))
647 throw new InvalidOperationException ("Only booleans are allowed");
649 var type = left.Type.GetNotNullableType ();
651 // The method should have identical parameter and return types.
652 if (left.Type != right.Type || method.ReturnType != type)
653 throw new ArgumentException ("left, right and return type must match");
655 var optrue = GetTrueOperator (type);
656 var opfalse = GetFalseOperator (type);
658 if (optrue == null || opfalse == null)
659 throw new ArgumentException ("Operators true and false are required but not defined");
665 public static BinaryExpression OrElse (Expression left, Expression right)
667 return OrElse (left, right, null);
670 public static BinaryExpression OrElse (Expression left, Expression right, MethodInfo method)
672 method = ConditionalBinaryCheck ("op_BitwiseOr", left, right, method);
674 return MakeBoolBinary (ExpressionType.OrElse, left, right, true, method);
680 public static BinaryExpression Equal (Expression left, Expression right)
682 return Equal (left, right, false, null);
685 public static BinaryExpression Equal (Expression left, Expression right, bool liftToNull, MethodInfo method)
687 method = BinaryCoreCheck ("op_Equality", left, right, method);
689 return MakeBoolBinary (ExpressionType.Equal, left, right, liftToNull, method);
692 public static BinaryExpression NotEqual (Expression left, Expression right)
694 return NotEqual (left, right, false, null);
698 public static BinaryExpression NotEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
700 method = BinaryCoreCheck ("op_Inequality", left, right, method);
702 return MakeBoolBinary (ExpressionType.NotEqual, left, right, liftToNull, method);
705 public static BinaryExpression GreaterThan (Expression left, Expression right)
707 return GreaterThan (left, right, false, null);
710 public static BinaryExpression GreaterThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
712 method = BinaryCoreCheck ("op_GreaterThan", left, right, method);
714 return MakeBoolBinary (ExpressionType.GreaterThan, left, right, liftToNull, method);
717 public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right)
719 return GreaterThanOrEqual (left, right, false, null);
723 public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
725 method = BinaryCoreCheck ("op_GreaterThanOrEqual", left, right, method);
727 return MakeBoolBinary (ExpressionType.GreaterThanOrEqual, left, right, liftToNull, method);
730 public static BinaryExpression LessThan (Expression left, Expression right)
732 return LessThan (left, right, false, null);
735 public static BinaryExpression LessThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
737 method = BinaryCoreCheck ("op_LessThan", left, right, method);
739 return MakeBoolBinary (ExpressionType.LessThan, left, right, liftToNull, method);
742 public static BinaryExpression LessThanOrEqual (Expression left, Expression right)
744 return LessThanOrEqual (left, right, false, null);
747 public static BinaryExpression LessThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
749 method = BinaryCoreCheck ("op_LessThanOrEqual", left, right, method);
751 return MakeBoolBinary (ExpressionType.LessThanOrEqual, left, right, liftToNull, method);
758 static void CheckArray (Expression array)
761 throw new ArgumentNullException ("array");
762 if (!array.Type.IsArray)
763 throw new ArgumentException ("The array argument must be of type array");
766 public static BinaryExpression ArrayIndex (Expression array, Expression index)
771 throw new ArgumentNullException ("index");
772 if (array.Type.GetArrayRank () != 1)
773 throw new ArgumentException ("The array argument must be a single dimensional array");
774 if (index.Type != typeof (int))
775 throw new ArgumentException ("The index must be of type int");
777 return new BinaryExpression (ExpressionType.ArrayIndex, array.Type.GetElementType (), array, index);
780 public static BinaryExpression Coalesce (Expression left, Expression right)
782 return Coalesce (left, right, null);
785 static BinaryExpression MakeCoalesce (Expression left, Expression right)
789 if (left.Type.IsNullable ()) {
790 Type lbase = left.Type.GetNotNullableType ();
792 if (!right.Type.IsNullable () && right.Type.IsAssignableTo (lbase))
796 if (result == null && right.Type.IsAssignableTo (left.Type))
799 if (result == null) {
800 if (left.Type.IsNullable () && left.Type.GetNotNullableType ().IsAssignableTo (right.Type))
805 throw new ArgumentException ("Incompatible argument types");
807 return new BinaryExpression (ExpressionType.Coalesce, result, left, right, false, false, null, null);
810 static BinaryExpression MakeConvertedCoalesce (Expression left, Expression right, LambdaExpression conversion)
812 var invoke = conversion.Type.GetInvokeMethod ();
814 CheckNotVoid (invoke.ReturnType);
816 if (invoke.ReturnType != right.Type)
817 throw new InvalidOperationException ("Conversion return type doesn't march right type");
819 var parameters = invoke.GetParameters ();
821 if (parameters.Length != 1)
822 throw new ArgumentException ("Conversion has wrong number of parameters");
824 if (!IsAssignableToParameterType (left.Type, parameters [0]))
825 throw new InvalidOperationException ("Conversion argument doesn't marcht left type");
827 return new BinaryExpression (ExpressionType.Coalesce, right.Type, left, right, false, false, null, conversion);
830 public static BinaryExpression Coalesce (Expression left, Expression right, LambdaExpression conversion)
833 throw new ArgumentNullException ("left");
835 throw new ArgumentNullException ("right");
838 // First arg must ne nullable (either Nullable<T> or a reference type
840 if (left.Type.IsValueType && !left.Type.IsNullable ())
841 throw new InvalidOperationException ("Left expression can never be null");
843 if (conversion != null)
844 return MakeConvertedCoalesce (left, right, conversion);
846 return MakeCoalesce (left, right);
850 // MakeBinary constructors
852 public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right)
854 return MakeBinary (binaryType, left, right, false, null);
857 public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method)
859 return MakeBinary (binaryType, left, right, liftToNull, method, null);
862 public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method, LambdaExpression conversion)
864 switch (binaryType) {
865 case ExpressionType.Add:
866 return Add (left, right, method);
867 case ExpressionType.AddChecked:
868 return AddChecked (left, right, method);
869 case ExpressionType.AndAlso:
870 return AndAlso (left, right);
871 case ExpressionType.Coalesce:
872 return Coalesce (left, right, conversion);
873 case ExpressionType.Divide:
874 return Divide (left, right, method);
875 case ExpressionType.Equal:
876 return Equal (left, right, liftToNull, method);
877 case ExpressionType.ExclusiveOr:
878 return ExclusiveOr (left, right, method);
879 case ExpressionType.GreaterThan:
880 return GreaterThan (left, right, liftToNull, method);
881 case ExpressionType.GreaterThanOrEqual:
882 return GreaterThanOrEqual (left, right, liftToNull, method);
883 case ExpressionType.LeftShift:
884 return LeftShift (left, right, method);
885 case ExpressionType.LessThan:
886 return LessThan (left, right, liftToNull, method);
887 case ExpressionType.LessThanOrEqual:
888 return LessThanOrEqual (left, right, liftToNull, method);
889 case ExpressionType.Modulo:
890 return Modulo (left, right, method);
891 case ExpressionType.Multiply:
892 return Multiply (left, right, method);
893 case ExpressionType.MultiplyChecked:
894 return MultiplyChecked (left, right, method);
895 case ExpressionType.NotEqual:
896 return NotEqual (left, right, liftToNull, method);
897 case ExpressionType.OrElse:
898 return OrElse (left, right);
899 case ExpressionType.Power:
900 return Power (left, right, method);
901 case ExpressionType.RightShift:
902 return RightShift (left, right, method);
903 case ExpressionType.Subtract:
904 return Subtract (left, right, method);
905 case ExpressionType.SubtractChecked:
906 return SubtractChecked (left, right, method);
907 case ExpressionType.And:
908 return And (left, right, method);
909 case ExpressionType.Or:
910 return Or (left, right, method);
913 throw new ArgumentException ("MakeBinary expect a binary node type");
918 public static MethodCallExpression ArrayIndex (Expression array, params Expression [] indexes)
920 return ArrayIndex (array, indexes as IEnumerable<Expression>);
923 public static MethodCallExpression ArrayIndex (Expression array, IEnumerable<Expression> indexes)
928 throw new ArgumentNullException ("indexes");
930 var args = indexes.ToReadOnlyCollection ();
931 if (array.Type.GetArrayRank () != args.Count)
932 throw new ArgumentException ("The number of arguments doesn't match the rank of the array");
934 foreach (var arg in args)
935 if (arg.Type != typeof (int))
936 throw new ArgumentException ("The index must be of type int");
938 return Call (array, array.Type.GetMethod ("Get", PublicInstance), args);
941 public static UnaryExpression ArrayLength (Expression array)
944 throw new ArgumentNullException ("array");
945 if (!array.Type.IsArray)
946 throw new ArgumentException ("The type of the expression must me Array");
947 if (array.Type.GetArrayRank () != 1)
948 throw new ArgumentException ("The array must be a single dimensional array");
950 return new UnaryExpression (ExpressionType.ArrayLength, array, typeof (int));
953 public static MemberAssignment Bind (MemberInfo member, Expression expression)
956 throw new ArgumentNullException ("member");
957 if (expression == null)
958 throw new ArgumentNullException ("expression");
962 var prop = member as PropertyInfo;
963 if (prop != null && prop.GetSetMethod (true) != null)
964 type = prop.PropertyType;
966 var field = member as FieldInfo;
968 type = field.FieldType;
971 throw new ArgumentException ("member");
973 if (!expression.Type.IsAssignableTo (type))
974 throw new ArgumentException ("member");
976 return new MemberAssignment (member, expression);
979 public static MemberAssignment Bind (MethodInfo propertyAccessor, Expression expression)
981 if (propertyAccessor == null)
982 throw new ArgumentNullException ("propertyAccessor");
983 if (expression == null)
984 throw new ArgumentNullException ("expression");
986 CheckNonGenericMethod (propertyAccessor);
988 var prop = GetAssociatedProperty (propertyAccessor);
990 throw new ArgumentException ("propertyAccessor");
992 var setter = prop.GetSetMethod (true);
994 throw new ArgumentException ("setter");
996 if (!expression.Type.IsAssignableTo (prop.PropertyType))
997 throw new ArgumentException ("member");
999 return new MemberAssignment (prop, expression);
1002 public static MethodCallExpression Call (Expression instance, MethodInfo method)
1004 return Call (instance, method, null as IEnumerable<Expression>);
1007 public static MethodCallExpression Call (MethodInfo method, params Expression [] arguments)
1009 return Call (null, method, arguments as IEnumerable<Expression>);
1012 public static MethodCallExpression Call (Expression instance, MethodInfo method, params Expression [] arguments)
1014 return Call (instance, method, arguments as IEnumerable<Expression>);
1017 public static MethodCallExpression Call (Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
1020 throw new ArgumentNullException ("method");
1021 if (instance == null && !method.IsStatic)
1022 throw new ArgumentNullException ("instance");
1023 if (method.IsStatic && instance != null)
1024 throw new ArgumentException ("instance");
1025 if (!method.IsStatic && !instance.Type.IsAssignableTo (method.DeclaringType))
1026 throw new ArgumentException ("Type is not assignable to the declaring type of the method");
1028 var args = CheckMethodArguments (method, arguments);
1030 return new MethodCallExpression (instance, method, args);
1033 static Type [] CollectTypes (IEnumerable<Expression> expressions)
1035 return (from arg in expressions select arg.Type).ToArray ();
1038 static MethodInfo TryMakeGeneric (MethodInfo method, Type [] args)
1043 if (!method.IsGenericMethod && args == null)
1046 if (args.Length == method.GetGenericArguments ().Length)
1047 return method.MakeGenericMethod (args);
1052 public static MethodCallExpression Call (Expression instance, string methodName, Type [] typeArguments, params Expression [] arguments)
1054 if (instance == null)
1055 throw new ArgumentNullException ("instance");
1056 if (methodName == null)
1057 throw new ArgumentNullException ("methodName");
1059 var method = TryGetMethod (instance.Type, methodName, AllInstance,
1060 CollectTypes (arguments), typeArguments);
1062 var args = CheckMethodArguments (method, arguments);
1064 return new MethodCallExpression (instance, method, args);
1067 static bool MethodMatch (MethodInfo method, string name, Type [] parameterTypes, Type [] argumentTypes)
1069 if (method.Name != name)
1072 var parameters = method.GetParameters ();
1074 if (parameters.Length != parameterTypes.Length)
1077 if (method.IsGenericMethod && method.IsGenericMethodDefinition) {
1078 var closed = TryMakeGeneric (method, argumentTypes);
1082 return MethodMatch (closed, name, parameterTypes, argumentTypes);
1085 for (int i = 0; i < parameters.Length; i++)
1086 if (!IsAssignableToParameterType (parameterTypes [i], parameters [i]))
1092 static MethodInfo TryGetMethod (Type type, string methodName, BindingFlags flags, Type [] parameterTypes, Type [] argumentTypes)
1094 var methods = from meth in type.GetMethods (flags)
1095 where MethodMatch (meth, methodName, parameterTypes, argumentTypes)
1098 if (methods.Count () > 1)
1099 throw new InvalidOperationException ("Too much method candidates");
1101 var method = TryMakeGeneric (methods.FirstOrDefault (), argumentTypes);
1105 throw new InvalidOperationException ("No such method");
1108 public static MethodCallExpression Call (Type type, string methodName, Type [] typeArguments, params Expression [] arguments)
1111 throw new ArgumentNullException ("type");
1112 if (methodName == null)
1113 throw new ArgumentNullException ("methodName");
1115 var method = TryGetMethod (type, methodName, AllStatic,
1116 CollectTypes (arguments), typeArguments);
1118 var args = CheckMethodArguments (method, arguments);
1120 return new MethodCallExpression (method, args);
1123 public static ConditionalExpression Condition (Expression test, Expression ifTrue, Expression ifFalse)
1126 throw new ArgumentNullException ("test");
1128 throw new ArgumentNullException ("ifTrue");
1129 if (ifFalse == null)
1130 throw new ArgumentNullException ("ifFalse");
1131 if (test.Type != typeof (bool))
1132 throw new ArgumentException ("Test expression should be of type bool");
1133 if (ifTrue.Type != ifFalse.Type)
1134 throw new ArgumentException ("The ifTrue and ifFalse type do not match");
1136 return new ConditionalExpression (test, ifTrue, ifFalse);
1139 public static ConstantExpression Constant (object value)
1142 return new ConstantExpression (null, typeof (object));
1144 return Constant (value, value.GetType ());
1147 public static ConstantExpression Constant (object value, Type type)
1150 throw new ArgumentNullException ("type");
1153 // value must be compatible with type, no conversions
1157 if (type.IsValueType && !type.IsNullable ())
1158 throw new ArgumentException ();
1160 if (!(type.IsValueType && type.IsNullable ()) && !value.GetType ().IsAssignableTo (type))
1161 throw new ArgumentException ();
1165 return new ConstantExpression (value, type);
1168 static bool IsConvertiblePrimitive (Type type)
1170 var t = type.GetNotNullableType ();
1172 if (t == typeof (bool))
1178 return t.IsPrimitive;
1181 internal static bool IsPrimitiveConversion (Type type, Type target)
1186 if (type.IsNullable () && target == type.GetNotNullableType ())
1189 if (target.IsNullable () && type == target.GetNotNullableType ())
1192 if (IsConvertiblePrimitive (type) && IsConvertiblePrimitive (target))
1198 internal static bool IsReferenceConversion (Type type, Type target)
1203 if (type == typeof (object) || target == typeof (object))
1206 if (type.IsInterface || target.IsInterface)
1209 if (type.IsValueType || target.IsValueType)
1212 if (type.IsAssignableTo (target) || target.IsAssignableTo (type))
1218 public static UnaryExpression Convert (Expression expression, Type type)
1220 return Convert (expression, type, null);
1223 static MethodInfo GetUserConversionMethod (Type type, Type target)
1225 var method = GetUnaryOperator ("op_Explicit", type, type, target);
1227 method = GetUnaryOperator ("op_Implicit", type, type, target);
1229 method = GetUnaryOperator ("op_Explicit", target, type, target);
1231 method = GetUnaryOperator ("op_Implicit", target, type, target);
1233 throw new InvalidOperationException ();
1238 public static UnaryExpression Convert (Expression expression, Type type, MethodInfo method)
1240 if (expression == null)
1241 throw new ArgumentNullException ("expression");
1243 throw new ArgumentNullException ("type");
1245 var et = expression.Type;
1248 CheckUnaryMethod (method, et);
1249 else if (!IsPrimitiveConversion (et, type) && !IsReferenceConversion (et, type))
1250 method = GetUserConversionMethod (et, type);
1252 return new UnaryExpression (ExpressionType.Convert,
1253 expression, type, method,
1254 IsConvertNodeLifted (method, expression, type));
1257 static bool IsConvertNodeLifted (MethodInfo method, Expression operand, Type target)
1260 return operand.Type.IsNullable () || target.IsNullable ();
1262 if (operand.Type.IsNullable () && !ParameterMatch (method, operand.Type))
1265 if (target.IsNullable () && !ReturnTypeMatch (method, target))
1271 static bool ParameterMatch (MethodInfo method, Type type)
1273 return method.GetParameters () [0].ParameterType == type;
1276 static bool ReturnTypeMatch (MethodInfo method, Type type)
1278 return method.ReturnType == type;
1281 public static UnaryExpression ConvertChecked (Expression expression, Type type)
1283 return ConvertChecked (expression, type, null);
1286 public static UnaryExpression ConvertChecked (Expression expression, Type type, MethodInfo method)
1288 if (expression == null)
1289 throw new ArgumentNullException ("expression");
1291 throw new ArgumentNullException ("type");
1293 var et = expression.Type;
1296 CheckUnaryMethod (method, et);
1297 else if (IsReferenceConversion (et, type))
1298 return Convert (expression, type, method);
1299 else if (!IsPrimitiveConversion (et, type))
1300 method = GetUserConversionMethod (et, type);
1302 return new UnaryExpression (ExpressionType.ConvertChecked,
1303 expression, type, method,
1304 IsConvertNodeLifted (method, expression, type));
1307 public static ElementInit ElementInit (MethodInfo addMethod, params Expression [] arguments)
1309 return ElementInit (addMethod, arguments as IEnumerable<Expression>);
1312 public static ElementInit ElementInit (MethodInfo addMethod, IEnumerable<Expression> arguments)
1314 if (addMethod == null)
1315 throw new ArgumentNullException ("addMethod");
1316 if (arguments == null)
1317 throw new ArgumentNullException ("arguments");
1318 if (addMethod.Name.ToLower (CultureInfo.InvariantCulture) != "add")
1319 throw new ArgumentException ("addMethod");
1320 if (addMethod.IsStatic)
1321 throw new ArgumentException ("addMethod must be an instance method", "addMethod");
1323 var args = CheckMethodArguments (addMethod, arguments);
1325 return new ElementInit (addMethod, args);
1328 public static MemberExpression Field (Expression expression, FieldInfo field)
1331 throw new ArgumentNullException ("field");
1332 if (!field.IsStatic) {
1333 if (expression == null)
1334 throw new ArgumentNullException ("expression");
1335 if (!expression.Type.IsAssignableTo (field.DeclaringType))
1336 throw new ArgumentException ("field");
1337 } else if (expression != null)
1338 throw new ArgumentException ("expression");
1340 return new MemberExpression (expression, field, field.FieldType);
1343 public static MemberExpression Field (Expression expression, string fieldName)
1345 if (expression == null)
1346 throw new ArgumentNullException ("expression");
1348 var field = expression.Type.GetField (fieldName, AllInstance);
1350 throw new ArgumentException (string.Format ("No field named {0} on {1}", fieldName, expression.Type));
1352 return new MemberExpression (expression, field, field.FieldType);
1355 public static Type GetActionType (params Type [] typeArgs)
1357 if (typeArgs == null)
1358 throw new ArgumentNullException ("typeArgs");
1360 if (typeArgs.Length > 4)
1361 throw new ArgumentException ("No Action type of this arity");
1363 if (typeArgs.Length == 0)
1364 return typeof (Action);
1367 switch (typeArgs.Length) {
1369 action = typeof (Action<>);
1372 action = typeof (Action<,>);
1375 action = typeof (Action<,,>);
1378 action = typeof (Action<,,,>);
1382 return action.MakeGenericType (typeArgs);
1385 public static Type GetFuncType (params Type [] typeArgs)
1387 if (typeArgs == null)
1388 throw new ArgumentNullException ("typeArgs");
1390 if (typeArgs.Length < 1 || typeArgs.Length > 5)
1391 throw new ArgumentException ("No Func type of this arity");
1394 switch (typeArgs.Length) {
1396 func = typeof (Func<>);
1399 func = typeof (Func<,>);
1402 func = typeof (Func<,,>);
1405 func = typeof (Func<,,,>);
1408 func = typeof (Func<,,,,>);
1412 return func.MakeGenericType (typeArgs);
1415 public static InvocationExpression Invoke (Expression expression, params Expression [] arguments)
1417 return Invoke (expression, arguments as IEnumerable<Expression>);
1420 static Type GetInvokableType (Type t)
1422 if (t.IsAssignableTo (typeof (Delegate)))
1425 return GetGenericType (t, typeof (Expression<>));
1428 static Type GetGenericType (Type t, Type def)
1433 if (t.IsGenericType && t.GetGenericTypeDefinition () == def)
1436 return GetGenericType (t.BaseType, def);
1439 public static InvocationExpression Invoke (Expression expression, IEnumerable<Expression> arguments)
1441 if (expression == null)
1442 throw new ArgumentNullException ("expression");
1444 var type = GetInvokableType (expression.Type);
1446 throw new ArgumentException ("The type of the expression is not invokable");
1448 var args = arguments.ToReadOnlyCollection ();
1449 CheckForNull (args, "arguments");
1451 var invoke = type.GetInvokeMethod ();
1453 throw new ArgumentException ("expression");
1455 if (invoke.GetParameters ().Length != args.Count)
1456 throw new InvalidOperationException ("Arguments count doesn't match parameters length");
1458 args = CheckMethodArguments (invoke, args);
1460 return new InvocationExpression (expression, invoke.ReturnType, args);
1463 static bool CanAssign (Type target, Type source)
1465 // This catches object and value type mixage, type compatibility is handled later
1466 if (target.IsValueType ^ source.IsValueType)
1469 return source.IsAssignableTo (target);
1472 static Expression CheckLambda (Type delegateType, Expression body, ReadOnlyCollection<ParameterExpression> parameters)
1474 if (!delegateType.IsSubclassOf (typeof (System.Delegate)))
1475 throw new ArgumentException ("delegateType");
1477 var invoke = delegateType.GetInvokeMethod ();
1479 throw new ArgumentException ("delegate must contain an Invoke method", "delegateType");
1481 var invoke_parameters = invoke.GetParameters ();
1482 if (invoke_parameters.Length != parameters.Count)
1483 throw new ArgumentException (string.Format ("Different number of arguments in delegate {0}", delegateType), "delegateType");
1485 for (int i = 0; i < invoke_parameters.Length; i++) {
1486 var parameter = parameters [i];
1487 if (parameter == null)
1488 throw new ArgumentNullException ("parameters");
1490 if (!CanAssign (parameter.Type, invoke_parameters [i].ParameterType))
1491 throw new ArgumentException (String.Format ("Can not assign a {0} to a {1}", invoke_parameters [i].ParameterType, parameter.Type));
1494 if (invoke.ReturnType != typeof (void)) {
1495 if (!CanAssign (invoke.ReturnType, body.Type)) {
1496 if (invoke.ReturnType.IsExpression ())
1497 return Expression.Quote (body);
1499 throw new ArgumentException (String.Format ("body type {0} can not be assigned to {1}", body.Type, invoke.ReturnType));
1505 public static Expression<TDelegate> Lambda<TDelegate> (Expression body, params ParameterExpression [] parameters)
1507 return Lambda<TDelegate> (body, parameters as IEnumerable<ParameterExpression>);
1510 public static Expression<TDelegate> Lambda<TDelegate> (Expression body, IEnumerable<ParameterExpression> parameters)
1513 throw new ArgumentNullException ("body");
1515 var ps = parameters.ToReadOnlyCollection ();
1517 body = CheckLambda (typeof (TDelegate), body, ps);
1519 return new Expression<TDelegate> (body, ps);
1522 public static LambdaExpression Lambda (Expression body, params ParameterExpression [] parameters)
1525 throw new ArgumentNullException ("body");
1526 if (parameters.Length > 4)
1527 throw new ArgumentException ("Too many parameters");
1529 return Lambda (GetDelegateType (body.Type, parameters), body, parameters);
1532 static Type GetDelegateType (Type return_type, ParameterExpression [] parameters)
1534 if (parameters == null)
1535 parameters = new ParameterExpression [0];
1537 if (return_type == typeof (void))
1538 return GetActionType (parameters.Select (p => p.Type).ToArray ());
1540 var types = new Type [parameters.Length + 1];
1541 for (int i = 0; i < types.Length - 1; i++)
1542 types [i] = parameters [i].Type;
1544 types [types.Length - 1] = return_type;
1545 return GetFuncType (types);
1548 public static LambdaExpression Lambda (Type delegateType, Expression body, params ParameterExpression [] parameters)
1550 return Lambda (delegateType, body, parameters as IEnumerable<ParameterExpression>);
1553 static LambdaExpression CreateExpressionOf (Type type, Expression body, ReadOnlyCollection<ParameterExpression> parameters)
1555 return (LambdaExpression) typeof (Expression<>).MakeGenericType (type).GetConstructor (
1556 NonPublicInstance, null, new [] { typeof (Expression), typeof (ReadOnlyCollection<ParameterExpression>) }, null).Invoke (new object [] { body, parameters } );
1559 public static LambdaExpression Lambda (Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
1561 if (delegateType == null)
1562 throw new ArgumentNullException ("delegateType");
1564 throw new ArgumentNullException ("body");
1566 var ps = parameters.ToReadOnlyCollection ();
1568 body = CheckLambda (delegateType, body, ps);
1570 return CreateExpressionOf (delegateType, body, ps);
1573 public static MemberListBinding ListBind (MemberInfo member, params ElementInit [] initializers)
1575 return ListBind (member, initializers as IEnumerable<ElementInit>);
1578 static void CheckIsAssignableToIEnumerable (Type t)
1580 if (!t.IsAssignableTo (typeof (IEnumerable)))
1581 throw new ArgumentException (string.Format ("Type {0} doesn't implemen IEnumerable", t));
1584 public static MemberListBinding ListBind (MemberInfo member, IEnumerable<ElementInit> initializers)
1587 throw new ArgumentNullException ("member");
1588 if (initializers == null)
1589 throw new ArgumentNullException ("initializers");
1591 var inits = initializers.ToReadOnlyCollection ();
1592 CheckForNull (inits, "initializers");
1594 member.OnFieldOrProperty (
1595 field => CheckIsAssignableToIEnumerable (field.FieldType),
1596 prop => CheckIsAssignableToIEnumerable (prop.PropertyType));
1598 return new MemberListBinding (member, inits);
1601 public static MemberListBinding ListBind (MethodInfo propertyAccessor, params ElementInit [] initializers)
1603 return ListBind (propertyAccessor, initializers as IEnumerable<ElementInit>);
1606 static void CheckForNull<T> (ReadOnlyCollection<T> collection, string name) where T : class
1608 foreach (var t in collection)
1610 throw new ArgumentNullException (name);
1613 public static MemberListBinding ListBind (MethodInfo propertyAccessor, IEnumerable<ElementInit> initializers)
1615 if (propertyAccessor == null)
1616 throw new ArgumentNullException ("propertyAccessor");
1617 if (initializers == null)
1618 throw new ArgumentNullException ("initializers");
1620 var inits = initializers.ToReadOnlyCollection ();
1621 CheckForNull (inits, "initializers");
1623 var prop = GetAssociatedProperty (propertyAccessor);
1625 throw new ArgumentException ("propertyAccessor");
1627 CheckIsAssignableToIEnumerable (prop.PropertyType);
1629 return new MemberListBinding (prop, inits);
1632 public static ListInitExpression ListInit (NewExpression newExpression, params ElementInit [] initializers)
1634 return ListInit (newExpression, initializers as IEnumerable<ElementInit>);
1637 public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<ElementInit> initializers)
1639 var inits = CheckListInit (newExpression, initializers);
1641 return new ListInitExpression (newExpression, inits);
1644 public static ListInitExpression ListInit (NewExpression newExpression, params Expression [] initializers)
1646 return ListInit (newExpression, initializers as IEnumerable<Expression>);
1649 public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<Expression> initializers)
1651 var inits = CheckListInit (newExpression, initializers);
1653 var add_method = GetAddMethod (newExpression.Type, inits [0].Type);
1654 if (add_method == null)
1655 throw new InvalidOperationException ("No suitable add method found");
1657 return new ListInitExpression (newExpression, CreateInitializers (add_method, inits));
1660 static ReadOnlyCollection<ElementInit> CreateInitializers (MethodInfo add_method, ReadOnlyCollection<Expression> initializers)
1662 return (from init in initializers select Expression.ElementInit (add_method, init)).ToReadOnlyCollection ();
1665 static MethodInfo GetAddMethod (Type type, Type arg)
1667 return type.GetMethod ("Add", PublicInstance | BindingFlags.IgnoreCase, null, new [] { arg }, null);
1670 public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, params Expression [] initializers)
1672 return ListInit (newExpression, addMethod, initializers as IEnumerable<Expression>);
1675 static ReadOnlyCollection<T> CheckListInit<T> (NewExpression newExpression, IEnumerable<T> initializers) where T : class
1677 if (newExpression == null)
1678 throw new ArgumentNullException ("newExpression");
1679 if (initializers == null)
1680 throw new ArgumentNullException ("initializers");
1681 if (!newExpression.Type.IsAssignableTo (typeof (IEnumerable)))
1682 throw new InvalidOperationException ("The type of the new expression does not implement IEnumerable");
1684 var inits = initializers.ToReadOnlyCollection ();
1685 if (inits.Count == 0)
1686 throw new ArgumentException ("Empty initializers");
1688 CheckForNull (inits, "initializers");
1693 public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, IEnumerable<Expression> initializers)
1695 var inits = CheckListInit (newExpression, initializers);
1697 if (addMethod != null) {
1698 if (addMethod.Name.ToLower (CultureInfo.InvariantCulture) != "add")
1699 throw new ArgumentException ("addMethod");
1701 var parameters = addMethod.GetParameters ();
1702 if (parameters.Length != 1)
1703 throw new ArgumentException ("addMethod");
1705 foreach (var expression in inits)
1706 if (!IsAssignableToParameterType (expression.Type, parameters [0]))
1707 throw new InvalidOperationException ("Initializer not assignable to the add method parameter type");
1710 if (addMethod == null)
1711 addMethod = GetAddMethod (newExpression.Type, inits [0].Type);
1713 if (addMethod == null)
1714 throw new InvalidOperationException ("No suitable add method found");
1716 return new ListInitExpression (newExpression, CreateInitializers (addMethod, inits));
1719 public static MemberExpression MakeMemberAccess (Expression expression, MemberInfo member)
1721 if (expression == null)
1722 throw new ArgumentNullException ("expression");
1724 throw new ArgumentNullException ("member");
1726 var field = member as FieldInfo;
1728 return Field (expression, field);
1730 var property = member as PropertyInfo;
1731 if (property != null)
1732 return Property (expression, property);
1734 throw new ArgumentException ("Member should either be a field or a property");
1737 public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type)
1739 return MakeUnary (unaryType, operand, type, null);
1742 public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type, MethodInfo method)
1744 switch (unaryType) {
1745 case ExpressionType.ArrayLength:
1746 return ArrayLength (operand);
1747 case ExpressionType.Convert:
1748 return Convert (operand, type, method);
1749 case ExpressionType.ConvertChecked:
1750 return ConvertChecked (operand, type, method);
1751 case ExpressionType.Negate:
1752 return Negate (operand, method);
1753 case ExpressionType.NegateChecked:
1754 return NegateChecked (operand, method);
1755 case ExpressionType.Not:
1756 return Not (operand, method);
1757 case ExpressionType.Quote:
1758 return Quote (operand);
1759 case ExpressionType.TypeAs:
1760 return TypeAs (operand, type);
1761 case ExpressionType.UnaryPlus:
1762 return UnaryPlus (operand, method);
1765 throw new ArgumentException ("MakeUnary expect an unary operator");
1768 public static MemberMemberBinding MemberBind (MemberInfo member, params MemberBinding [] bindings)
1770 return MemberBind (member, bindings as IEnumerable<MemberBinding>);
1773 public static MemberMemberBinding MemberBind (MemberInfo member, IEnumerable<MemberBinding> bindings)
1776 throw new ArgumentNullException ("member");
1778 var type = member.OnFieldOrProperty (
1779 field => field.FieldType,
1780 prop => prop.PropertyType);
1782 return new MemberMemberBinding (member, CheckMemberBindings (type, bindings));
1785 public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, params MemberBinding [] bindings)
1787 return MemberBind (propertyAccessor, bindings as IEnumerable<MemberBinding>);
1790 public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, IEnumerable<MemberBinding> bindings)
1792 if (propertyAccessor == null)
1793 throw new ArgumentNullException ("propertyAccessor");
1795 var bds = bindings.ToReadOnlyCollection ();
1796 CheckForNull (bds, "bindings");
1798 var prop = GetAssociatedProperty (propertyAccessor);
1800 throw new ArgumentException ("propertyAccessor");
1802 return new MemberMemberBinding (prop, CheckMemberBindings (prop.PropertyType, bindings));
1805 static ReadOnlyCollection<MemberBinding> CheckMemberBindings (Type type, IEnumerable<MemberBinding> bindings)
1807 if (bindings == null)
1808 throw new ArgumentNullException ("bindings");
1810 var bds = bindings.ToReadOnlyCollection ();
1811 CheckForNull (bds, "bindings");
1813 foreach (var binding in bds)
1814 if (!type.IsAssignableTo (binding.Member.DeclaringType))
1815 throw new ArgumentException ("Type not assignable to member type");
1820 public static MemberInitExpression MemberInit (NewExpression newExpression, params MemberBinding [] bindings)
1822 return MemberInit (newExpression, bindings as IEnumerable<MemberBinding>);
1825 public static MemberInitExpression MemberInit (NewExpression newExpression, IEnumerable<MemberBinding> bindings)
1827 if (newExpression == null)
1828 throw new ArgumentNullException ("newExpression");
1830 return new MemberInitExpression (newExpression, CheckMemberBindings (newExpression.Type, bindings));
1833 public static UnaryExpression Negate (Expression expression)
1835 return Negate (expression, null);
1838 public static UnaryExpression Negate (Expression expression, MethodInfo method)
1840 method = UnaryCoreCheck ("op_UnaryNegation", expression, method, type => IsSignedNumber (type));
1842 return MakeSimpleUnary (ExpressionType.Negate, expression, method);
1845 public static UnaryExpression NegateChecked (Expression expression)
1847 return NegateChecked (expression, null);
1850 public static UnaryExpression NegateChecked (Expression expression, MethodInfo method)
1852 method = UnaryCoreCheck ("op_UnaryNegation", expression, method, type => IsSignedNumber (type));
1854 return MakeSimpleUnary (ExpressionType.NegateChecked, expression, method);
1857 public static NewExpression New (ConstructorInfo constructor)
1859 if (constructor == null)
1860 throw new ArgumentNullException ("constructor");
1862 if (constructor.GetParameters ().Length > 0)
1863 throw new ArgumentException ("Constructor must be parameter less");
1865 return new NewExpression (constructor, (null as IEnumerable<Expression>).ToReadOnlyCollection (), null);
1868 public static NewExpression New (Type type)
1871 throw new ArgumentNullException ("type");
1873 CheckNotVoid (type);
1875 var args = (null as IEnumerable<Expression>).ToReadOnlyCollection ();
1877 if (type.IsValueType)
1878 return new NewExpression (type, args);
1880 var ctor = type.GetConstructor (Type.EmptyTypes);
1882 throw new ArgumentException ("Type doesn't have a parameter less constructor");
1884 return new NewExpression (ctor, args, null);
1887 public static NewExpression New (ConstructorInfo constructor, params Expression [] arguments)
1889 return New (constructor, arguments as IEnumerable<Expression>);
1892 public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments)
1894 if (constructor == null)
1895 throw new ArgumentNullException ("constructor");
1897 var args = CheckMethodArguments (constructor, arguments);
1899 return new NewExpression (constructor, args, null);
1902 static IList<Expression> CreateArgumentList (IEnumerable<Expression> arguments)
1904 if (arguments == null)
1905 return arguments.ToReadOnlyCollection ();
1907 return arguments.ToList ();
1910 static void CheckNonGenericMethod (MethodBase method)
1912 if (method.IsGenericMethodDefinition || method.ContainsGenericParameters)
1913 throw new ArgumentException ("Can not used open generic methods");
1916 static ReadOnlyCollection<Expression> CheckMethodArguments (MethodBase method, IEnumerable<Expression> args)
1918 CheckNonGenericMethod (method);
1920 var arguments = CreateArgumentList (args);
1921 var parameters = method.GetParameters ();
1923 if (arguments.Count != parameters.Length)
1924 throw new ArgumentException ("The number of arguments doesn't match the number of parameters");
1926 for (int i = 0; i < parameters.Length; i++) {
1927 if (arguments [i] == null)
1928 throw new ArgumentNullException ("arguments");
1930 if (!IsAssignableToParameterType (arguments [i].Type, parameters [i])) {
1931 if (!parameters [i].ParameterType.IsExpression ())
1932 throw new ArgumentException ("arguments");
1934 arguments [i] = Expression.Quote (arguments [i]);
1938 return arguments.ToReadOnlyCollection ();
1941 public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, params MemberInfo [] members)
1943 return New (constructor, arguments, members as IEnumerable<MemberInfo>);
1946 public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, IEnumerable<MemberInfo> members)
1948 if (constructor == null)
1949 throw new ArgumentNullException ("constructor");
1951 var args = arguments.ToReadOnlyCollection ();
1952 var mmbs = members.ToReadOnlyCollection ();
1954 CheckForNull (args, "arguments");
1955 CheckForNull (mmbs, "members");
1957 args = CheckMethodArguments (constructor, arguments);
1959 if (args.Count != mmbs.Count)
1960 throw new ArgumentException ("Arguments count does not match members count");
1962 for (int i = 0; i < mmbs.Count; i++) {
1963 var member = mmbs [i];
1965 switch (member.MemberType) {
1966 case MemberTypes.Field:
1967 type = (member as FieldInfo).FieldType;
1969 case MemberTypes.Method:
1970 type = (member as MethodInfo).ReturnType;
1972 case MemberTypes.Property:
1973 var prop = member as PropertyInfo;
1974 if (prop.GetGetMethod (true) == null)
1975 throw new ArgumentException ("Property must have a getter");
1977 type = (member as PropertyInfo).PropertyType;
1980 throw new ArgumentException ("Member type not allowed");
1983 if (!args [i].Type.IsAssignableTo (type))
1984 throw new ArgumentException ("Argument type not assignable to member type");
1987 return new NewExpression (constructor, args, mmbs);
1990 public static NewArrayExpression NewArrayBounds (Type type, params Expression [] bounds)
1992 return NewArrayBounds (type, bounds as IEnumerable<Expression>);
1995 public static NewArrayExpression NewArrayBounds (Type type, IEnumerable<Expression> bounds)
1998 throw new ArgumentNullException ("type");
2000 throw new ArgumentNullException ("bounds");
2002 CheckNotVoid (type);
2004 var array_bounds = bounds.ToReadOnlyCollection ();
2005 foreach (var expression in array_bounds)
2006 if (!IsInt (expression.Type))
2007 throw new ArgumentException ("The bounds collection can only contain expression of integers types");
2009 return new NewArrayExpression (ExpressionType.NewArrayBounds, type.MakeArrayType (array_bounds.Count), array_bounds);
2012 public static NewArrayExpression NewArrayInit (Type type, params Expression [] initializers)
2014 return NewArrayInit (type, initializers as IEnumerable<Expression>);
2017 public static NewArrayExpression NewArrayInit (Type type, IEnumerable<Expression> initializers)
2020 throw new ArgumentNullException ("type");
2021 if (initializers == null)
2022 throw new ArgumentNullException ("initializers");
2024 CheckNotVoid (type);
2026 var inits = initializers.ToReadOnlyCollection ();
2028 foreach (var expression in inits) {
2029 if (expression == null)
2030 throw new ArgumentNullException ("initializers");
2032 if (!expression.Type.IsAssignableTo (type))
2033 throw new InvalidOperationException (
2034 string.Format ("{0} IsAssignableTo {1}, expression [ {2} ] : {3}", expression.Type, type, expression.NodeType, expression));
2036 // TODO: Quote elements if type == typeof (Expression)
2039 return new NewArrayExpression (ExpressionType.NewArrayInit, type.MakeArrayType (), inits);
2042 public static UnaryExpression Not (Expression expression)
2044 return Not (expression, null);
2047 public static UnaryExpression Not (Expression expression, MethodInfo method)
2049 Func<Type, bool> validator = type => IsIntOrBool (type);
2051 method = UnaryCoreCheck ("op_LogicalNot", expression, method, validator);
2054 method = UnaryCoreCheck ("op_OnesComplement", expression, method, validator);
2056 return MakeSimpleUnary (ExpressionType.Not, expression, method);
2059 static void CheckNotVoid (Type type)
2061 if (type == typeof (void))
2062 throw new ArgumentException ("Type can't be void");
2065 public static ParameterExpression Parameter (Type type, string name)
2068 throw new ArgumentNullException ("type");
2070 CheckNotVoid (type);
2072 return new ParameterExpression (type, name);
2075 public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
2077 if (propertyAccessor == null)
2078 throw new ArgumentNullException ("propertyAccessor");
2080 CheckNonGenericMethod (propertyAccessor);
2082 if (!propertyAccessor.IsStatic) {
2083 if (expression == null)
2084 throw new ArgumentNullException ("expression");
2085 if (!expression.Type.IsAssignableTo (propertyAccessor.DeclaringType))
2086 throw new ArgumentException ("expression");
2087 } else if (expression != null)
2088 throw new ArgumentException ("expression");
2090 var prop = GetAssociatedProperty (propertyAccessor);
2092 throw new ArgumentException (string.Format ("Method {0} has no associated property", propertyAccessor));
2094 return new MemberExpression (expression, prop, prop.PropertyType);
2097 static PropertyInfo GetAssociatedProperty (MethodInfo method)
2102 foreach (var prop in method.DeclaringType.GetProperties (All)) {
2103 if (method.Equals (prop.GetGetMethod (true)))
2105 if (method.Equals (prop.GetSetMethod (true)))
2112 public static MemberExpression Property (Expression expression, PropertyInfo property)
2114 if (property == null)
2115 throw new ArgumentNullException ("property");
2117 var getter = property.GetGetMethod (true);
2119 throw new ArgumentException ("getter");
2121 if (!getter.IsStatic) {
2122 if (expression == null)
2123 throw new ArgumentNullException ("expression");
2124 if (!expression.Type.IsAssignableTo (property.DeclaringType))
2125 throw new ArgumentException ("expression");
2126 } else if (expression != null)
2127 throw new ArgumentException ("expression");
2129 return new MemberExpression (expression, property, property.PropertyType);
2132 public static MemberExpression Property (Expression expression, string propertyName)
2134 if (expression == null)
2135 throw new ArgumentNullException ("expression");
2137 var prop = expression.Type.GetProperty (propertyName, AllInstance);
2139 throw new ArgumentException (string.Format ("No property named {0} on {1}", propertyName, expression.Type));
2141 return new MemberExpression (expression, prop, prop.PropertyType);
2144 public static MemberExpression PropertyOrField (Expression expression, string propertyOrFieldName)
2146 if (expression == null)
2147 throw new ArgumentNullException ("expression");
2148 if (propertyOrFieldName == null)
2149 throw new ArgumentNullException ("propertyOrFieldName");
2151 var prop = expression.Type.GetProperty (propertyOrFieldName, AllInstance);
2153 return new MemberExpression (expression, prop, prop.PropertyType);
2155 var field = expression.Type.GetField (propertyOrFieldName, AllInstance);
2157 return new MemberExpression (expression, field, field.FieldType);
2159 throw new ArgumentException (string.Format ("No field or property named {0} on {1}", propertyOrFieldName, expression.Type));
2162 public static UnaryExpression Quote (Expression expression)
2164 if (expression == null)
2165 throw new ArgumentNullException ("expression");
2167 return new UnaryExpression (ExpressionType.Quote, expression, expression.GetType ());
2170 public static UnaryExpression TypeAs (Expression expression, Type type)
2172 if (expression == null)
2173 throw new ArgumentNullException ("expression");
2175 throw new ArgumentNullException ("type");
2176 if (type.IsValueType && !type.IsNullable ())
2177 throw new ArgumentException ("TypeAs expect a reference or a nullable type");
2179 return new UnaryExpression (ExpressionType.TypeAs, expression, type);
2182 public static TypeBinaryExpression TypeIs (Expression expression, Type type)
2184 if (expression == null)
2185 throw new ArgumentNullException ("expression");
2187 throw new ArgumentNullException ("type");
2189 CheckNotVoid (type);
2191 return new TypeBinaryExpression (ExpressionType.TypeIs, expression, type, typeof (bool));
2194 public static UnaryExpression UnaryPlus (Expression expression)
2196 return UnaryPlus (expression, null);
2199 public static UnaryExpression UnaryPlus (Expression expression, MethodInfo method)
2201 method = UnaryCoreCheck ("op_UnaryPlus", expression, method, type => IsNumber (type));
2203 return MakeSimpleUnary (ExpressionType.UnaryPlus, expression, method);
2206 static bool IsInt (Type t)
2208 return t == typeof (byte) || t == typeof (sbyte) ||
2209 t == typeof (short) || t == typeof (ushort) ||
2210 t == typeof (int) || t == typeof (uint) ||
2211 t == typeof (long) || t == typeof (ulong);
2214 static bool IsIntOrBool (Type t)
2216 return IsInt (t) || t == typeof (bool);
2219 static bool IsNumber (Type t)
2224 return t == typeof (float) || t == typeof (double) || t == typeof (decimal);
2227 static bool IsSignedNumber (Type t)
2229 return IsNumber (t) && !IsUnsigned (t);
2232 internal static bool IsUnsigned (Type t)
2236 return IsUnsigned (t.GetElementType ());
2239 return t == typeof (ushort) ||
2240 t == typeof (uint) ||
2241 t == typeof (ulong) ||
2246 // This method must be overwritten by derived classes to
2247 // compile the expression
2249 internal virtual void Emit (EmitContext ec)
2251 throw new NotImplementedException (String.Format ("Emit method is not implemented in expression type {0}", GetType ()));