| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- //
- // Authors:
- // Antonello Provenzano <[email protected]>
- //
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Reflection;
- using System.Text;
- namespace System.Linq.Expressions
- {
- public abstract class Expression
- {
- #region .ctor
- protected Expression(ExpressionType nodeType, Type type)
- {
- this.nodeType = nodeType;
- this.type = type;
- }
- #endregion
- #region Fields
- private Type type;
- private ExpressionType nodeType;
- #endregion
- #region Properties
- public Type Type
- {
- get { return type; }
- }
- public ExpressionType NodeType
- {
- get { return nodeType; }
- }
- #endregion
- #region Private Static Methods
- private static void CheckLeftRight(Expression left, Expression right)
- {
- if (left == null)
- throw new ArgumentNullException("left");
- if (right == null)
- throw new ArgumentNullException("right");
- }
- #endregion
- #region Internal Methods
- internal virtual void BuildString(StringBuilder builder)
- {
- //TODO:
- }
- #endregion
- #region Public Methods
- public override string ToString()
- {
- //TODO: check this...
- StringBuilder builder = new StringBuilder();
- BuildString(builder);
- return builder.ToString();
- }
- #endregion
- #region Internal Static Methos
- internal static Type GetNonNullableType(Type type)
- {
- //TODO:
- return type;
- }
- internal static bool IsNullableType(Type type)
- {
- if (type == null)
- throw new ArgumentNullException("type");
- if (type.IsGenericType)
- {
- //TODO: check this...
- Type genType = type.GetGenericTypeDefinition();
- return typeof(Nullable<>).IsAssignableFrom(genType);
- }
- return false;
- }
- #endregion
- #region Public Static Methods
- public static BinaryExpression Add(Expression left, Expression right, MethodInfo method)
- {
- CheckLeftRight(left, right);
- // sine both the expressions define the same numeric type we don't have
- // to look for the "op_Addition" method...
- if (left.type == right.type &&
- ExpressionUtil.IsNumber (left.type))
- return new BinaryExpression(ExpressionType.Add, left, right, left.type);
- if (method == null)
- {
- method = ExpressionUtil.GetMethod("op_Addition", new Type[] { left.type, right.type });
- }
- if (method != null)
- {
- if (method.ReturnType == null) // it returns a void
- throw new ArgumentException();
- if (!method.IsStatic)
- throw new ArgumentException();
- ParameterInfo[] pars = method.GetParameters();
- if (pars.Length != 2)
- throw new ArgumentException();
- }
- return new BinaryExpression(ExpressionType.Add, left, right, method, method.ReturnType);
- }
- public static BinaryExpression Add(Expression left, Expression right)
- {
- return Add(left, right, null);
- }
- public static BinaryExpression AddChecked(Expression left, Expression right, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression AddChecked(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression And(Expression left, Expression right, MethodInfo method)
- {
- CheckLeftRight(left, right);
- throw new NotImplementedException();
- }
- public static BinaryExpression And(Expression left, Expression right)
- {
- return And(left, right, null);
- }
- public static BinaryExpression AndAlso(Expression left, Expression right, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression AndAlso(Expression left, Expression right)
- {
- return AndAlso(left, right, null);
- }
- public static MethodCallExpression ArrayIndex(Expression array, IEnumerable<Expression> indexes)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression ArrayIndex(Expression array, Expression index)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression ArrayIndex(Expression array, params Expression[] indexes)
- {
- throw new NotImplementedException();
- }
- public static UnaryExpression ArrayLength(Expression array)
- {
- throw new NotImplementedException();
- }
- public static MemberAssignment Bind(MemberInfo member, Expression expression)
- {
- throw new NotImplementedException();
- }
- public static MemberAssignment Bind(MethodInfo propertyAccessor, Expression expression)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression Call(Expression instance, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression Call(MethodInfo method, params Expression[] arguments)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression Call(Expression instance, MethodInfo method, params Expression[] arguments)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression Call(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression Call(Expression instance, string methodName, Type[] typeArguments, params Expression[] arguments)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression Call(Type type, string methodName, Type[] typeArguments, params Expression[] arguments)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression CallVirtual(Expression instance, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression CallVirtual(Expression instance, MethodInfo method, params Expression[] arguments)
- {
- throw new NotImplementedException();
- }
- public static MethodCallExpression CallVirtual(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
- {
- throw new NotImplementedException();
- }
- public static UnaryExpression Cast(Expression expression, Type type)
- {
- throw new NotImplementedException();
- }
- public static UnaryExpression Cast(Expression expression, Type type, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression Coalesce(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression Coalesce(Expression left, Expression right, MethodInfo conversion)
- {
- throw new NotImplementedException();
- }
- public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)
- {
- throw new NotImplementedException();
- }
- public static ConstantExpression Constant(object value)
- {
- Type valueType = null;
- if (value != null)
- valueType = value.GetType();
- return Constant(value, valueType);
- }
- public static ConstantExpression Constant(object value, Type type)
- {
- if (type == null)
- throw new ArgumentNullException("type");
- if (value == null && !IsNullableType(type))
- throw new ArgumentException();
- return new ConstantExpression(value, type);
- }
- public static UnaryExpression Convert(Expression expression, Type type)
- {
- throw new NotImplementedException();
- }
- public static UnaryExpression Convert(Expression expression, Type type, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static UnaryExpression ConvertChecked(Expression expression, Type type)
- {
- throw new NotImplementedException();
- }
- public static UnaryExpression ConvertChecked(Expression expression, Type type, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression Divide(Expression left, Expression right)
- {
- return Divide(left, right);
- }
- public static BinaryExpression Divide(Expression left, Expression right, MethodInfo method)
- {
- CheckLeftRight(left, right);
- throw new NotImplementedException();
- }
- public static BinaryExpression Equal(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression Equal(Expression left, Expression right, bool liftToNull, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression ExclusiveOr(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression ExclusiveOr(Expression left, Expression right, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static MemberExpression Field(Expression expression, FieldInfo field)
- {
- if (field == null)
- throw new ArgumentNullException("field");
- return new MemberExpression(expression, field, field.FieldType);
- }
- public static MemberExpression Field(Expression expression, string fieldName)
- {
- if (expression == null)
- throw new ArgumentNullException("expression");
- FieldInfo field = expression.Type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- if (field == null)
- throw new ArgumentException();
- return Field(expression, field);
- }
- public static FuncletExpression Funclet(Funclet funclet, Type type)
- {
- if (funclet == null)
- throw new ArgumentNullException("funclet");
- if (type == null)
- throw new ArgumentNullException("type");
- return new FuncletExpression(funclet, type);
- }
- public static Type GetFuncType(params Type[] typeArgs)
- {
- if (typeArgs == null)
- throw new ArgumentNullException("typeArgs");
- if (typeArgs.Length > 5)
- throw new ArgumentException();
- return typeof(Func<,,,,>).MakeGenericType(typeArgs);
- }
- public static BinaryExpression GreaterThan(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression GreaterThan(Expression left, Expression right, bool liftToNull, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression GreaterThanOrEqual(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression GreaterThanOrEqual(Expression left, Expression right, bool liftToNull, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static InvocationExpression Invoke(Expression expression, IEnumerable<Expression> arguments)
- {
- throw new NotImplementedException();
- }
- public static InvocationExpression Invoke(Expression expression, params Expression[] arguments)
- {
- throw new NotImplementedException();
- }
- public static Expression<TDelegate> Lambda<TDelegate>(Expression body, params ParameterExpression[] parameters)
- {
- throw new NotImplementedException();
- }
- public static Expression<TDelegate> Lambda<TDelegate>(Expression body, IEnumerable<ParameterExpression> parameters)
- {
- throw new NotImplementedException();
- }
- public static LambdaExpression Lambda(Expression body, params ParameterExpression[] parameters)
- {
- throw new NotImplementedException();
- }
- public static LambdaExpression Lambda(Type delegateType, Expression body, params ParameterExpression[] parameters)
- {
- throw new NotImplementedException();
- }
- public static LambdaExpression Lambda(Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression LeftShift(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression LeftShift(Expression left, Expression right, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression LessThan(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression LessThan(Expression left, Expression right, bool liftToNull, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression LessThanOrEqual(Expression left, Expression right)
- {
- throw new NotImplementedException();
- }
- public static BinaryExpression LessThanOrEqual(Expression left, Expression right, bool liftToNull, MethodInfo method)
- {
- throw new NotImplementedException();
- }
- /*
- public static LiftExpression Lift(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
- {
- throw new NotImplementedException();
- }
- public static LiftExpression Lift(Expression expression, ParameterExpression parameter, Expression argument)
- {
- throw new NotImplementedException();
- }
- public static LiftExpression LiftEqual(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
- {
- throw new NotImplementedException();
- }
- public static LiftExpression LiftFalse(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
- {
- throw new NotImplementedException();
- }
- public static LiftExpression LiftFalse(Expression expression, ParameterExpression parameter, Expression argument)
- {
- throw new NotImplementedException();
- }
- public static LiftExpression LiftNotEqual(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
- {
- throw new NotImplementedException();
- }
- public static LiftExpression LiftTrue(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
- {
- throw new NotImplementedException();
- }
- public static LiftExpression LiftTrue(Expression expression, ParameterExpression parameter, Expression argument)
- {
- throw new NotImplementedException();
- }
- */
- public static MemberListBinding ListBind(MemberInfo member, params Expression[] initializers)
- {
- throw new NotImplementedException();
- }
- public static MemberListBinding ListBind(MemberInfo member, IEnumerable<Expression> initializers)
- {
- throw new NotImplementedException();
- }
- public static MemberListBinding ListBind(MethodInfo propertyAccessor, IEnumerable<Expression> initializers)
- {
- throw new NotImplementedException();
- }
- public static ListInitExpression ListInit(NewExpression newExpression, params Expression[] initializers)
- {
- if (initializers == null)
- throw new ArgumentNullException("inizializers");
- return ListInit(newExpression, ExpressionUtil.GetReadOnlyCollection (initializers));
- }
- public static ListInitExpression ListInit(NewExpression newExpression, IEnumerable<Expression> initializers)
- {
- if (newExpression == null)
- throw new ArgumentNullException("newExpression");
- if (initializers == null)
- throw new ArgumentNullException("inizializers");
- return new ListInitExpression(newExpression, ExpressionUtil.GetReadOnlyCollection(initializers));
- }
- public static MemberInitExpression MemberInit(NewExpression newExpression, IEnumerable<MemberBinding> bindings)
- {
- if (newExpression == null)
- throw new ArgumentNullException("newExpression");
- if (bindings == null)
- throw new ArgumentNullException("bindings");
- return new MemberInitExpression(newExpression, ExpressionUtil.GetReadOnlyCollection(bindings));
- }
- public static MemberExpression Property(Expression expression, PropertyInfo property)
- {
- if (property == null)
- throw new ArgumentNullException("property");
- MethodInfo getMethod = property.GetGetMethod(true);
- if (getMethod == null)
- throw new ArgumentException(); // to access the property we need to have
- // a get method...
- return new MemberExpression(expression, property, property.PropertyType);
- }
- public static MemberExpression Property(Expression expression, string propertyName)
- {
- if (expression == null)
- throw new ArgumentNullException("expression");
- PropertyInfo property = expression.Type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- if (property == null)
- throw new ArgumentException();
- return Property(expression, property);
- }
- public static MemberExpression PropertyOrField(Expression expression, string propertyOrFieldName)
- {
- if (expression == null)
- throw new ArgumentNullException("expression");
- PropertyInfo property = expression.Type.GetProperty(propertyOrFieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- if (property != null)
- return Property(expression, property);
- FieldInfo field = expression.Type.GetField(propertyOrFieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
- if (field != null)
- return Field(expression, field);
-
- //TODO: should we return <null> here?
- // the name is not defined in the Type of the expression given...
- throw new ArgumentException();
- }
- public static UnaryExpression Quote(Expression expression)
- {
- if (expression == null)
- throw new ArgumentNullException("expression");
- //TODO: is this right?
- return new UnaryExpression(ExpressionType.Quote, expression, expression.GetType());
- }
- public static TypeBinaryExpression TypeIs(Expression expression, Type type)
- {
- if (expression == null)
- throw new ArgumentNullException("expression");
- if (type == null)
- throw new ArgumentNullException("type");
-
- return new TypeBinaryExpression(ExpressionType.TypeIs, expression, type, typeof(bool));
- }
- public static UnaryExpression TypeAs(Expression expression, Type type)
- {
- if (expression == null)
- throw new ArgumentNullException("expression");
- if (type == null)
- throw new ArgumentNullException("type");
- return new UnaryExpression(ExpressionType.TypeAs, expression, type);
- }
- #endregion
- }
- }
|