Implemented Expression.Bind() method.
[mono.git] / mcs / class / System.Core / System.Linq.Expressions / Expression.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 //
19 // Authors:
20 //        Marek Safar (marek.safar@seznam.cz)
21 //        Antonello Provenzano  <antonello@deveel.com>
22 //        Federico Di Gregorio <fog@initd.org>
23
24 using System.Collections.Generic;
25 using System.Collections.ObjectModel;
26 using System.Reflection;
27 using System.Text;
28
29 namespace System.Linq.Expressions
30 {
31     public abstract class Expression
32     {
33         #region .ctor
34         protected Expression (ExpressionType nodeType, Type type)
35         {
36             this.nodeType = nodeType;
37             this.type = type;
38         }
39         #endregion
40         
41         #region Fields
42         private Type type;
43         private ExpressionType nodeType;
44         #endregion
45         
46         #region Properties
47         public Type Type {
48             get { return type; }
49         }
50
51         public ExpressionType NodeType {
52             get { return nodeType; }
53         }
54         #endregion
55
56         #region Internal methods 
57         internal virtual void BuildString (StringBuilder builder)
58         {
59             builder.Append ("[").Append (nodeType).Append ("]");
60         }
61         
62         internal static Type GetNonNullableType(Type type)
63         {
64             // The Nullable<> class takes a single generic type so we can directly return
65             // the first element of the array (if the type is nullable.)
66             
67             if (IsNullableType (type))
68                 return type.GetGenericArguments ()[0];
69             else
70                 return type;
71         }
72
73         internal static bool IsNullableType(Type type)
74         {
75             if (type == null)
76                 throw new ArgumentNullException("type");
77
78             if (type.IsGenericType) {
79                 Type genType = type.GetGenericTypeDefinition();
80                 return typeof(Nullable<>).IsAssignableFrom(genType);
81             }
82
83             return false;
84         }
85         #endregion
86         
87         #region Private support methods
88         private const BindingFlags opBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
89         private const BindingFlags methBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
90         private const BindingFlags propBindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
91
92         private static MethodInfo GetUserDefinedBinaryOperator (Type leftType, Type rightType, string name)
93         {
94             Type[] types = new Type[2] { leftType, rightType };
95                         
96             MethodInfo method = leftType.GetMethod (name, opBindingFlags, null, types, null);
97             if (method != null) return method;
98                 
99             method = rightType.GetMethod (name, opBindingFlags, null, types, null);
100             if (method != null) return method;
101
102             if (method == null && IsNullableType(leftType) && IsNullableType(rightType))
103                 return GetUserDefinedBinaryOperator(GetNonNullableType(leftType), GetNonNullableType(rightType), name);
104         
105             return null;
106         }
107
108         private static BinaryExpression GetUserDefinedBinaryOperatorOrThrow (ExpressionType nodeType, string name,
109                 Expression left, Expression right)
110         {
111             MethodInfo method = GetUserDefinedBinaryOperator(left.type, right.type, name);
112
113             if (method != null)
114                 return new BinaryExpression (nodeType, left, right, method, method.ReturnType);
115             else
116                 throw new InvalidOperationException(String.Format(
117                     "The binary operator Add is not defined for the types '{0}' and '{1}'.", left.type, right.type));
118
119             // Note: here the code in ExpressionUtils has a series of checks to make sure that
120             // the method is static, that its return type is not void and that the number of
121             // parameters is 2 and they are of the right type, but we already know that! Or not?
122         }
123
124         private static PropertyInfo GetProperty (MethodInfo mi)
125         {
126             // If the method has the hidebysig and specialname attributes it can be a property accessor;
127             // if that's the case we try to extract the type of the property and then we use it and the
128             // property name (derived from the method name) to find the right ProprtyInfo.
129             
130             if (mi.IsHideBySig && mi.IsSpecialName) {
131                 Type propertyType = null;
132                 if (mi.Name.StartsWith("set_")) {
133                     ParameterInfo[] parameters = mi.GetParameters();
134                     if (parameters.Length == 1)
135                         propertyType = parameters[0].ParameterType;
136                 }
137                 else if (mi.Name.StartsWith("get_")) {
138                     propertyType = mi.ReturnType;
139                 }
140                 
141                 if (propertyType != null) {
142                     PropertyInfo pi = mi.DeclaringType.GetProperty(
143                         mi.Name.Substring(4), propBindingFlags, null, propertyType, new Type[0], null);
144                     if (pi != null) return pi;
145                 }
146             }
147             
148             throw new ArgumentException(String.Format(
149                 "The method '{0}.{1}' is not a property accessor", mi.DeclaringType.FullName, mi.Name));
150         }
151         
152         private static void ValidateUserDefinedConditionalLogicOperator (ExpressionType nodeType, Type left, Type right, MethodInfo method)
153         {
154             // Conditional logic need the "definitely true" and "definitely false" operators.
155             Type[] types = new Type[1] { left };
156                         
157             MethodInfo opTrue  = left.GetMethod ("op_True", opBindingFlags, null, types, null);
158             MethodInfo opFalse = left.GetMethod ("op_False", opBindingFlags, null, types, null);
159             
160             if (opTrue == null || opFalse == null)
161                 throw new ArgumentException(String.Format(
162                     "The user-defined operator method '{0}' for operator '{1}' must have associated boolean True and False operators.",
163                     method.Name, nodeType));
164         }
165         
166         private static void ValidateSettableFieldOrPropertyMember (MemberInfo member, out Type memberType)
167         {
168             if (member.MemberType == MemberTypes.Field) {
169                 memberType = typeof (FieldInfo);
170             }
171             else if (member.MemberType == MemberTypes.Property) {
172                 PropertyInfo pi = (PropertyInfo)member;
173                 if (!pi.CanWrite)
174                     throw new ArgumentException(String.Format("The property '{0}' has no 'set' accessor", pi));
175                 memberType = typeof(PropertyInfo);
176             }
177             else {
178                 throw new ArgumentException("Argument must be either a FieldInfo or PropertyInfo");   
179             }
180         }
181         #endregion
182                 
183         #region ToString
184         public override string ToString()
185         {
186             StringBuilder builder = new StringBuilder ();
187             BuildString (builder);
188             return builder.ToString ();
189         }
190         #endregion
191
192         #region Add
193         public static BinaryExpression Add(Expression left, Expression right, MethodInfo method)
194         {
195             if (left == null)
196                 throw new ArgumentNullException ("left");
197             if (right == null)
198                 throw new ArgumentNullException ("right");
199
200             if (method != null)
201                 return new BinaryExpression(ExpressionType.Add, left, right, method, method.ReturnType);
202             
203             // Since both the expressions define the same numeric type we don't have
204             // to look for the "op_Addition" method.
205             if (left.type == right.type && ExpressionUtil.IsNumber(left.type))
206                 return new BinaryExpression(ExpressionType.Add, left, right, left.type);
207
208             // Else we try for a user-defined operator.
209             return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.Add, "op_Addition", left, right);
210         }
211
212         public static BinaryExpression Add(Expression left, Expression right)
213         {
214             return Add(left, right, null);
215         }
216         #endregion
217         
218         #region AddChecked
219         public static BinaryExpression AddChecked(Expression left, Expression right, MethodInfo method)
220         {
221             if (left == null)
222                 throw new ArgumentNullException ("left");
223             if (right == null)
224                 throw new ArgumentNullException ("right");
225
226             if (method != null)
227                 return new BinaryExpression(ExpressionType.AddChecked, left, right, method, method.ReturnType);
228
229             // Since both the expressions define the same numeric type we don't have
230             // to look for the "op_Addition" method.
231             if (left.type == right.type && ExpressionUtil.IsNumber(left.type))
232                 return new BinaryExpression(ExpressionType.AddChecked, left, right, left.type);
233
234             method = GetUserDefinedBinaryOperator (left.type, right.type, "op_Addition");
235             if (method == null)
236                 throw new InvalidOperationException(String.Format(
237                     "The binary operator AddChecked is not defined for the types '{0}' and '{1}'.", left.type, right.type));
238             
239             Type retType = method.ReturnType;
240
241             // Note: here the code did some very strange checks for bool (but note that bool does
242             // not define an addition operator) and created nullables for value types (but the new
243             // MS code does not do that). All that has been removed.
244
245             return new BinaryExpression(ExpressionType.AddChecked, left, right, method, retType);
246         }
247
248         public static BinaryExpression AddChecked(Expression left, Expression right)
249         {
250             return AddChecked(left, right, null);
251         }
252         #endregion
253
254         #region And
255         public static BinaryExpression And(Expression left, Expression right, MethodInfo method)
256         {
257             if (left == null)
258                 throw new ArgumentNullException ("left");
259             if (right == null)
260                 throw new ArgumentNullException ("right");
261
262             if (method != null)
263                 return new BinaryExpression(ExpressionType.And, left, right, method, method.ReturnType);
264             
265             // Since both the expressions define the same integer or boolean type we don't have
266             // to look for the "op_BitwiseAnd" method.
267             if (left.type == right.type && (ExpressionUtil.IsInteger(left.type) || left.type == typeof(bool)))
268                 return new BinaryExpression(ExpressionType.And, left, right, left.type);
269
270             // Else we try for a user-defined operator.
271             return GetUserDefinedBinaryOperatorOrThrow (ExpressionType.And, "op_BitwiseAnd", left, right);
272         }
273
274         public static BinaryExpression And(Expression left, Expression right)
275         {
276             return And(left, right, null);
277         }
278         #endregion
279         
280         #region AndAlso
281         public static BinaryExpression AndAlso(Expression left, Expression right, MethodInfo method)
282         {
283             if (left == null)
284                 throw new ArgumentNullException ("left");
285             if (right == null)
286                 throw new ArgumentNullException ("right");
287
288             // Since both the expressions define the same integer or boolean type we don't have
289             // to look for the "op_BitwiseAnd" method.
290             if (left.type == right.type && left.type == typeof(bool))
291                 return new BinaryExpression(ExpressionType.AndAlso, left, right, left.type);
292
293             // Else we must validate the method to make sure it has companion "true" and "false" operators.
294             if (method == null)
295                 method = GetUserDefinedBinaryOperator (left.type, right.type, "op_BitwiseAnd");
296             if (method == null)
297                 throw new InvalidOperationException(String.Format(
298                     "The binary operator AndAlso is not defined for the types '{0}' and '{1}'.", left.type, right.type));
299             ValidateUserDefinedConditionalLogicOperator(ExpressionType.AndAlso, left.type, right.type, method);
300             
301             return new BinaryExpression(ExpressionType.AndAlso, left, right, method, method.ReturnType);
302         }
303
304         public static BinaryExpression AndAlso(Expression left, Expression right)
305         {
306             return AndAlso(left, right, null);
307         }
308         #endregion
309         
310         #region ArrayIndex
311         public static BinaryExpression ArrayIndex(Expression array, Expression index)
312         {
313             if (array == null)
314                 throw new ArgumentNullException ("array");
315             if (index == null)
316                 throw new ArgumentNullException ("index");
317             if (!array.type.IsArray)
318                 throw new ArgumentException ("Argument must be array");
319             if (index.type != typeof(int))
320                 throw new ArgumentException ("Argument for array index must be of type Int32");
321
322             return new BinaryExpression(ExpressionType.ArrayIndex, array, index, array.type.GetElementType());
323         }
324
325         public static MethodCallExpression ArrayIndex(Expression array, params Expression[] indexes)
326         {
327             return ArrayIndex(array, (IEnumerable<Expression>)indexes);
328         }
329
330         public static MethodCallExpression ArrayIndex(Expression array, IEnumerable<Expression> indexes)
331         {
332             if (array == null)
333                 throw new ArgumentNullException ("array");
334             if (indexes == null)
335                 throw new ArgumentNullException ("indexes");
336             if (!array.type.IsArray)
337                 throw new ArgumentException ("Argument must be array");
338
339             // We'll need an array of typeof(Type) elements long as the array's rank later
340             // and also a generic List to hold the indexes (ReadOnlyCollection wants that.)
341             
342             Type[] types = (Type[])Array.CreateInstance(typeof(Type), array.type.GetArrayRank());
343             Expression[] indexesList = new Expression[array.type.GetArrayRank()];
344             
345             int rank = 0;
346             foreach (Expression index in indexes) {
347                 if (index.type != typeof(int))
348                     throw new ArgumentException ("Argument for array index must be of type Int32");
349                 if (rank == array.type.GetArrayRank())
350                     throw new ArgumentException ("Incorrect number of indexes");
351
352                 types[rank] = index.type;
353                 indexesList[rank] = index;
354                 rank += 1;
355             }
356                 
357             // If the array's rank is equalto the number of given indexes we can go on and
358             // look for a Get(Int32, ...) method with "rank" parameters to generate the
359             // MethodCallExpression.
360
361             MethodInfo method = array.type.GetMethod("Get", methBindingFlags, null, types, null);
362
363             // This should not happen, but we check anyway.
364             if (method == null)
365                 throw new InvalidOperationException(String.Format(
366                     "The method Get(...) is not defined for the type '{0}'.", array.type));
367     
368             return new MethodCallExpression(ExpressionType.Call, method, array, new ReadOnlyCollection<Expression>(indexesList));
369         }
370         #endregion
371         
372         #region ArrayLength
373         public static UnaryExpression ArrayLength(Expression array)
374         {
375             if (array == null)
376                 throw new ArgumentNullException ("array");
377             if (!array.type.IsArray)
378                 throw new ArgumentException ("Argument must be array");
379
380             return new UnaryExpression(ExpressionType.ArrayLength, array, typeof(Int32));        
381         }
382         #endregion
383         
384         #region Bind
385         public static MemberAssignment Bind (MemberInfo member, Expression expression)
386         {
387             if (member == null)
388                 throw new ArgumentNullException ("member");
389             if (expression == null)
390                 throw new ArgumentNullException ("expression");
391                 
392             Type memberType;
393             ValidateSettableFieldOrPropertyMember(member, out memberType);            
394         
395             return new MemberAssignment(member, expression);
396         }
397
398         public static MemberAssignment Bind (MethodInfo propertyAccessor, Expression expression)
399         {
400             if (propertyAccessor == null)
401                 throw new ArgumentNullException ("propertyAccessor");
402             if (expression == null)
403                 throw new ArgumentNullException ("expression");
404
405             return new MemberAssignment(GetProperty(propertyAccessor), expression);        
406         }
407         #endregion
408         
409         #region Call
410         public static MethodCallExpression Call(Expression instance, MethodInfo method)
411         {
412             if (method == null)
413                 throw new ArgumentNullException("method");
414             if (instance == null && !method.IsStatic)
415                 throw new ArgumentNullException("instance");
416                 
417             return Call(instance, method, (Expression[])null);
418         }
419
420         public static MethodCallExpression Call(MethodInfo method, params Expression[] arguments)
421         {
422             return Call(null, method, (IEnumerable<Expression>)arguments);
423         }
424
425         public static MethodCallExpression Call(Expression instance, MethodInfo method, params Expression[] arguments)
426         {
427             return Call(instance, method, (IEnumerable<Expression>)arguments);
428         }
429
430         public static MethodCallExpression Call(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
431         {
432             if (method == null)
433                 throw new ArgumentNullException("method");
434             if (arguments == null)
435                 throw new ArgumentNullException("arguments");
436             if (instance == null && !method.IsStatic)
437                 throw new ArgumentNullException("instance");
438                 
439             if (method.IsGenericMethodDefinition)
440                     throw new ArgumentException();
441             if (method.ContainsGenericParameters)
442                     throw new ArgumentException();
443             if (instance != null && !instance.type.IsAssignableFrom(method.DeclaringType))
444                 throw new ArgumentException();
445
446             ReadOnlyCollection<Expression> roArgs = Enumerable.ToReadOnlyCollection<Expression>(arguments);
447
448             ParameterInfo[] pars = method.GetParameters();
449             if (Enumerable.Count<Expression>(arguments) != pars.Length)
450                 throw new ArgumentException();
451
452             if (pars.Length > 0)
453             {
454                 //TODO: validate the parameters against the arguments...
455             }
456
457             return new MethodCallExpression(ExpressionType.Call, method, instance, roArgs);
458         }
459         #endregion
460
461         // NOTE: CallVirtual is not implemented because it is already marked as Obsolete by MS.
462         
463         public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)
464         {
465             if (test == null)
466                 throw new ArgumentNullException("test");
467             if (ifTrue == null)
468                 throw new ArgumentNullException("ifTrue");
469             if (ifFalse == null)
470                 throw new ArgumentNullException("ifFalse");
471             if (test.type != typeof(bool))
472                 throw new ArgumentException();
473             if (ifTrue.type != ifFalse.type)
474                 throw new ArgumentException();
475
476             return new ConditionalExpression(test, ifTrue, ifFalse, ifTrue.type);
477         }
478
479         public static ConstantExpression Constant(object value, Type type)
480         {
481             if (type == null)
482                 throw new ArgumentNullException("type");
483             if (value == null && !IsNullableType(type))
484                 throw new ArgumentException("Argument types do not match");
485
486             return new ConstantExpression(value, type);
487         }
488
489         public static ConstantExpression Constant(object value)
490         {
491             if (value != null)
492                 return new ConstantExpression(value, value.GetType());
493             else
494                 return new ConstantExpression(null, typeof(object));
495         }
496
497         public static BinaryExpression Divide(Expression left, Expression right)
498         {
499             return Divide(left, right, null);
500         }
501
502         public static BinaryExpression Divide(Expression left, Expression right, MethodInfo method)
503         {
504             if (left == null)
505                 throw new ArgumentNullException("left");
506             if (right == null)
507                 throw new ArgumentNullException("right");
508
509             // sine both the expressions define the same numeric type we don't have 
510             // to look for the "op_Division" method...
511             if (left.type == right.type &&
512                 ExpressionUtil.IsNumber(left.type))
513                 return new BinaryExpression(ExpressionType.Divide, left, right, left.type);
514
515             if (method == null)
516                 method = ExpressionUtil.GetOperatorMethod("op_Division", left.type, right.type);
517
518             // ok if even op_Division is not defined we need to throw an exception...
519             if (method == null)
520                 throw new InvalidOperationException();
521
522             return new BinaryExpression(ExpressionType.Divide, left, right, method, method.ReturnType);
523         }
524
525         public static MemberExpression Field(Expression expression, FieldInfo field)
526         {
527             if (field == null)
528                 throw new ArgumentNullException("field");
529
530             return new MemberExpression(expression, field, field.FieldType);
531         }
532
533         public static MemberExpression Field(Expression expression, string fieldName)
534         {
535             if (expression == null)
536                 throw new ArgumentNullException("expression");
537
538             FieldInfo field = expression.Type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
539             if (field == null)
540                 throw new ArgumentException();
541
542             return Field(expression, field);
543         }
544
545         public static FuncletExpression Funclet(Funclet funclet, Type type)
546         {
547             if (funclet == null)
548                 throw new ArgumentNullException("funclet");
549             if (type == null)
550                 throw new ArgumentNullException("type");
551
552             return new FuncletExpression(funclet, type);
553         }
554
555         public static Type GetFuncType(params Type[] typeArgs)
556         {
557             if (typeArgs == null)
558                 throw new ArgumentNullException("typeArgs");
559             if (typeArgs.Length > 5)
560                 throw new ArgumentException();
561
562             return typeof(Func<,,,,>).MakeGenericType(typeArgs);
563         }
564
565         public static BinaryExpression LeftShift(Expression left, Expression right, MethodInfo method)
566         {
567             if (left == null)
568                 throw new ArgumentNullException("left");
569             if (right == null)
570                 throw new ArgumentNullException("right");
571
572             // since the left expression is of an integer type and the right is of
573             // an integer we don't have to look for the "op_LeftShift" method...
574             if (ExpressionUtil.IsInteger(left.type) && right.type == typeof(int))
575                 return new BinaryExpression(ExpressionType.LeftShift, left, right, left.type);
576
577             if (method == null)
578                 method = ExpressionUtil.GetOperatorMethod("op_LeftShift", left.type, right.type);
579
580             // ok if even op_Division is not defined we need to throw an exception...
581             if (method == null)
582                 throw new InvalidOperationException();
583
584             return new BinaryExpression(ExpressionType.LeftShift, left, right, method, method.ReturnType);
585         }
586
587         public static BinaryExpression LeftShift(Expression left, Expression right)
588         {
589             return LeftShift(left, right, null);
590         }
591
592         public static ListInitExpression ListInit(NewExpression newExpression, params Expression[] initializers)
593         {
594             if (initializers == null)
595                 throw new ArgumentNullException("inizializers");
596
597             return ListInit(newExpression, Enumerable.ToReadOnlyCollection<Expression>(initializers));
598         }
599
600         public static ListInitExpression ListInit(NewExpression newExpression, IEnumerable<Expression> initializers)
601         {
602             if (newExpression == null)
603                 throw new ArgumentNullException("newExpression");
604             if (initializers == null)
605                 throw new ArgumentNullException("inizializers");
606
607             return new ListInitExpression(newExpression, Enumerable.ToReadOnlyCollection<Expression>(initializers));
608         }
609
610         public static MemberInitExpression MemberInit(NewExpression newExpression, IEnumerable<MemberBinding> bindings)
611         {
612             if (newExpression == null)
613                 throw new ArgumentNullException("newExpression");
614
615             if (bindings == null)
616                 throw new ArgumentNullException("bindings");
617
618             return new MemberInitExpression(newExpression, Enumerable.ToReadOnlyCollection<MemberBinding>(bindings));
619         }
620
621         public static MemberExpression Property(Expression expression, PropertyInfo property)
622         {
623             if (property == null)
624                 throw new ArgumentNullException("property");
625
626             MethodInfo getMethod = property.GetGetMethod(true);
627             if (getMethod == null)
628                 throw new ArgumentException(); // to access the property we need to have
629                                                // a get method...
630
631             return new MemberExpression(expression, property, property.PropertyType);
632         }
633
634         public static UnaryExpression Quote(Expression expression)
635         {
636             if (expression == null)
637                 throw new ArgumentNullException("expression");
638
639             return new UnaryExpression(ExpressionType.Quote, expression, expression.GetType());
640         }
641
642
643         public static MemberExpression Property(Expression expression, string propertyName)
644         {
645             if (expression == null)
646                 throw new ArgumentNullException("expression");
647
648             PropertyInfo property = expression.Type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
649
650             if (property == null)
651                 throw new ArgumentException();
652
653             return Property(expression, property);
654         }
655
656         public static MemberExpression PropertyOrField(Expression expression, string propertyOrFieldName)
657         {
658             if (expression == null)
659                 throw new ArgumentNullException("expression");
660
661             PropertyInfo property = expression.Type.GetProperty(propertyOrFieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
662             if (property != null)
663                 return Property(expression, property);
664
665             FieldInfo field = expression.Type.GetField(propertyOrFieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
666             if (field != null)
667                 return Field(expression, field);
668                 
669             //TODO: should we return <null> here?
670             // the name is not defined in the Type of the expression given...
671             throw new ArgumentException();
672         }
673
674
675         public static TypeBinaryExpression TypeIs(Expression expression, Type type)
676         {
677             if (expression == null)
678                 throw new ArgumentNullException("expression");
679             if (type == null)
680                 throw new ArgumentNullException("type"); 
681             
682             return new TypeBinaryExpression(ExpressionType.TypeIs, expression, type, typeof(bool));
683         }
684
685         public static UnaryExpression TypeAs(Expression expression, Type type)
686         {
687             if (expression == null)
688                 throw new ArgumentNullException("expression");
689             if (type == null)
690                 throw new ArgumentNullException("type");
691
692             return new UnaryExpression(ExpressionType.TypeAs, expression, type);
693         }
694     }
695 }