static string GetExpressionValue(ISqlDialect dialect, Expression rootExpression, Expression e, IDictionary<string, object> parameters, Func<string> getParameterName) { var de = (dynamic)e; if (de.NodeType == ExpressionType.MemberAccess) { object val = null; var t = (Type)de.Type; var fieldName = de.Member.Name; if (t == typeof(DateTime)) { if (fieldName == "Now") { val = DateTime.Now; } else if (fieldName == "UtcNow") { val = DateTime.UtcNow; } } if (val != null) { var id = getParameterName(); parameters.Add(id, dialect.ConvertParameterValue(val)); return id; } } var ce = (e is ConstantExpression) ? e : de.Expression; if (ce.NodeType == ExpressionType.Constant) { var val = ce.Value; if (!(e is ConstantExpression)) { var t = (Type)ce.Type; var fieldName = de.Member.Name; var fieldInfo = t.GetField(fieldName) ?? t.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); if (fieldInfo != null) { val = fieldInfo.GetValue(val); } else { PropertyInfo propInfo = t.GetProperty(fieldName) ?? t.GetProperty(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); if (propInfo != null) { val = propInfo.GetValue(val, null); } } //var mt = (System.Reflection.MemberTypes)(de.Member).MemberType; //if (mt == System.Reflection.MemberTypes.Field) //{ // var fieldInfo = t.GetField(fieldName) ?? t.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); // val = fieldInfo.GetValue(val); //} //else if (mt == System.Reflection.MemberTypes.Property) //{ // PropertyInfo propInfo = t.GetProperty(fieldName) ?? t.GetProperty(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); // val = propInfo.GetValue(val, null); //} } if (val == null) { return _NULL; } else if ((val as string) == _Space) { return "' '"; } else { var id = getParameterName(); parameters.Add(id, dialect.ConvertParameterValue(val)); return id; } } else if (ce.NodeType == ExpressionType.MemberAccess) { var val = GetMemberAccessValue(rootExpression, e); if (val == null) { return _NULL; } else if ((val as string) == _Space) { return "' '"; } else { var id = getParameterName(); parameters.Add(id, dialect.ConvertParameterValue(val)); return id; } } return null; }
static string GetExpressionValue(ISqlDialect dialect, Expression rootExpression, Expression e, IDictionary <string, object> parameters, Func <string> getParameterName) { var de = (dynamic)e; if (de.NodeType == ExpressionType.MemberAccess) { object val = null; var t = (Type)de.Type; var fieldName = de.Member.Name; if (t == typeof(DateTime)) { if (fieldName == "Now") { val = DateTime.Now; } else if (fieldName == "UtcNow") { val = DateTime.UtcNow; } } if (val != null) { var id = getParameterName(); parameters.Add(id, dialect.ConvertParameterValue(val)); return(id); } } var ce = (e is ConstantExpression) ? e : de.Expression; if (ce.NodeType == ExpressionType.Constant) { var val = ce.Value; if (!(e is ConstantExpression)) { var t = (Type)ce.Type; var fieldName = de.Member.Name; var fieldInfo = t.GetField(fieldName) ?? t.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); if (fieldInfo != null) { val = fieldInfo.GetValue(val); } else { PropertyInfo propInfo = t.GetProperty(fieldName) ?? t.GetProperty(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); if (propInfo != null) { val = propInfo.GetValue(val, null); } } //var mt = (System.Reflection.MemberTypes)(de.Member).MemberType; //if (mt == System.Reflection.MemberTypes.Field) //{ // var fieldInfo = t.GetField(fieldName) ?? t.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); // val = fieldInfo.GetValue(val); //} //else if (mt == System.Reflection.MemberTypes.Property) //{ // PropertyInfo propInfo = t.GetProperty(fieldName) ?? t.GetProperty(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); // val = propInfo.GetValue(val, null); //} } if (val == null) { return(_NULL); } else if ((val as string) == _Space) { return("' '"); } else { var id = getParameterName(); parameters.Add(id, dialect.ConvertParameterValue(val)); return(id); } } else if (ce.NodeType == ExpressionType.MemberAccess) { var val = GetMemberAccessValue(rootExpression, e); if (val == null) { return(_NULL); } else if ((val as string) == _Space) { return("' '"); } else { var id = getParameterName(); parameters.Add(id, dialect.ConvertParameterValue(val)); return(id); } } return(null); }