public static object GetExpressionValue(this DbExpression exp) { if (exp.NodeType == DbExpressionType.Constant) { return(((DbConstantExpression)exp).Value); } if (exp.NodeType == DbExpressionType.MemberAccess) { DbMemberExpression m = (DbMemberExpression)exp; object instance = null; if (m.Expression != null) { instance = DbExpressionExtensions.GetExpressionValue(m.Expression); /* 非静态成员,需要检查是否为空引用。Nullable<T>.HasValue 的情况比较特俗,暂不考虑 */ Type declaringType = m.Member.DeclaringType; if (declaringType.IsClass || declaringType.IsInterface) { if (instance == null) { throw new NullReferenceException(string.Format("There is an object reference not set to an instance of an object in expression tree.The type of null object is '{0}'.", declaringType.FullName)); } } } return(GetMemberAccessExpressionValue(m, instance)); } throw new NotSupportedException(); }
static void Method_Contains(DbMethodCallExpression exp, SqlGenerator generator) { MethodInfo method = exp.Method; if (method.DeclaringType == UtilConstants.TypeOfString) { Method_String_Contains(exp, generator); return; } List <DbExpression> exps = new List <DbExpression>(); IEnumerable values = null; DbExpression arg = null; var declaringType = method.DeclaringType; if (typeof(IList).IsAssignableFrom(declaringType) || (declaringType.GetTypeInfo().IsGenericType&& typeof(ICollection <>).MakeGenericType(declaringType.GetGenericArguments()).IsAssignableFrom(declaringType))) { DbMemberExpression memberExp = exp.Object as DbMemberExpression; if (memberExp == null || !memberExp.CanEvaluate()) { throw new NotSupportedException(exp.ToString()); } values = DbExpressionExtensions.GetExpressionValue(memberExp) as IEnumerable; //Enumerable arg = exp.Arguments.First(); goto constructInState; } if (method.IsStatic && declaringType == typeof(Enumerable) && exp.Arguments.Count == 2) { DbMemberExpression memberExp = exp.Arguments.First() as DbMemberExpression; if (memberExp == null || !memberExp.CanEvaluate()) { throw new NotSupportedException(exp.ToString()); } values = DbExpressionExtensions.GetExpressionValue(memberExp) as IEnumerable; arg = exp.Arguments.Skip(1).First(); goto constructInState; } throw UtilExceptions.NotSupportedMethod(exp.Method); constructInState: foreach (object value in values) { if (value == null) { exps.Add(DbExpression.Constant(null, arg.Type)); } else { exps.Add(DbExpression.Parameter(value)); } } In(generator, exps, arg); }
/// <summary> /// 对 memberExpression 进行求值 /// </summary> /// <param name="exp"></param> /// <returns>返回 DbParameterExpression</returns> public static DbParameterExpression ParseToParameterExpression(this DbMemberExpression memberExpression) { DbParameterExpression ret = null; //求值 object val = DbExpressionExtensions.GetExpressionValue(memberExpression); ret = DbExpression.Parameter(val, memberExpression.Type); return(ret); }
public static object GetExpressionValue(this DbExpression exp) { if (exp.NodeType == DbExpressionType.Constant) { return(((DbConstantExpression)exp).Value); } if (exp.NodeType == DbExpressionType.MemberAccess) { DbMemberExpression m = (DbMemberExpression)exp; object instance = null; if (m.Expression != null) { instance = DbExpressionExtensions.GetExpressionValue(m.Expression); } return(GetMemberAccessExpressionValue(m, instance)); } throw new NotSupportedException(); }