private Expression Evaluate(Expression e) { Type type = e.Type; if (e.NodeType == ExpressionType.Convert && AtkTypeHelper.GetNonNullableType(((UnaryExpression)e).Operand.Type) == AtkTypeHelper.GetNonNullableType(type)) { e = ((UnaryExpression)e).Operand; } if (e.NodeType == ExpressionType.Constant) { ConstantExpression constantExpression = (ConstantExpression)e; if (e.Type != type && AtkTypeHelper.GetNonNullableType(e.Type) == AtkTypeHelper.GetNonNullableType(type)) { e = Expression.Constant(constantExpression.Value, type); } return(e); } MemberExpression memberExpression = e as MemberExpression; if (memberExpression != null) { ConstantExpression expression = memberExpression.Expression as ConstantExpression; if (expression != null) { return(Expression.Constant(memberExpression.Member.GetValue(expression.Value), type)); } } if (type.IsValueType) { e = Expression.Convert(e, typeof(object)); } return(Expression.Constant(Expression.Lambda <Func <object> >(e, Array.Empty <ParameterExpression>()).Compile()(), type)); }
private Expression Evaluate(System.Linq.Expressions.Expression e) { Type type = e.Type; // check for nullable converts & strip them if (e.NodeType == ExpressionType.Convert) { var u = (UnaryExpression)e; if (AtkTypeHelper.GetNonNullableType(u.Operand.Type) == AtkTypeHelper.GetNonNullableType(type)) { e = ((UnaryExpression)e).Operand; } } // if we now just have a constant, return it if (e.NodeType == ExpressionType.Constant) { var ce = (ConstantExpression)e; // if we've lost our nullable typeness add it back if (e.Type != type && AtkTypeHelper.GetNonNullableType(e.Type) == AtkTypeHelper.GetNonNullableType(type)) { e = ce = System.Linq.Expressions.Expression.Constant(ce.Value, type); } return(e); } var me = e as MemberExpression; if (me != null) { // member accesses off of constant's are common, and yet since these partial evals // are never re-used, using reflection to access the member is faster than compiling // and invoking a lambda var ce = me.Expression as ConstantExpression; if (ce != null) { return(System.Linq.Expressions.Expression.Constant(Atk.AtkExpression.AtkReflectionExtensions.GetValue(me.Member, ce.Value), type)); } } if (type.IsValueType) { e = System.Linq.Expressions.Expression.Convert(e, typeof(object)); } Expression <Func <object> > lambda = System.Linq.Expressions.Expression.Lambda <Func <object> >(e); #if NOREFEMIT Func <object> fn = ExpressionEvaluator.CreateDelegate(lambda); #else Func <object> fn = lambda.Compile(); #endif return(System.Linq.Expressions.Expression.Constant(fn(), type)); }
public static bool IsInteger(Type type) { AtkTypeHelper.GetNonNullableType(type); return((uint)(Type.GetTypeCode(type) - 5) <= 7U); }