sunkaixuan
8 years ago
26 changed files with 228 additions and 178 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,160 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Reflection; |
|||
using System.Text; |
|||
namespace SqlSugar |
|||
{ |
|||
public class ExpressionTool |
|||
{ |
|||
public static string GetOperator(ExpressionType expressiontype) |
|||
{ |
|||
switch (expressiontype) |
|||
{ |
|||
case ExpressionType.And: |
|||
case ExpressionType.AndAlso: |
|||
return " AND "; |
|||
case ExpressionType.Equal: |
|||
return " ="; |
|||
case ExpressionType.GreaterThan: |
|||
return " >"; |
|||
case ExpressionType.GreaterThanOrEqual: |
|||
return ">="; |
|||
case ExpressionType.LessThan: |
|||
return "<"; |
|||
case ExpressionType.LessThanOrEqual: |
|||
return "<="; |
|||
case ExpressionType.NotEqual: |
|||
return "<>"; |
|||
case ExpressionType.Or: |
|||
case ExpressionType.OrElse: |
|||
return " OR "; |
|||
case ExpressionType.Add: |
|||
case ExpressionType.AddChecked: |
|||
return "+"; |
|||
case ExpressionType.Subtract: |
|||
case ExpressionType.SubtractChecked: |
|||
return "-"; |
|||
case ExpressionType.Divide: |
|||
return "/"; |
|||
case ExpressionType.Multiply: |
|||
case ExpressionType.MultiplyChecked: |
|||
return "*"; |
|||
default: |
|||
throw new NotSupportedException(string.Format(ExpressionErrorMessage.OperatorError, expressiontype.ToString())); |
|||
} |
|||
} |
|||
|
|||
public static object GetValue(MemberInfo member, Expression expression) |
|||
{ |
|||
var memberInfos = new Stack<MemberInfo>(); |
|||
var fieldInfo = member as System.Reflection.FieldInfo; |
|||
object reval = null; |
|||
// "descend" toward's the root object reference:
|
|||
while (expression is MemberExpression) |
|||
{ |
|||
var memberExpr = expression as MemberExpression; |
|||
memberInfos.Push(memberExpr.Member); |
|||
if (memberExpr.Expression == null) |
|||
{ |
|||
var isProperty = memberExpr.Member.MemberType == MemberTypes.Property; |
|||
var isField = memberExpr.Member.MemberType == MemberTypes.Field; |
|||
if (isProperty) |
|||
{ |
|||
reval = GetPropertyValue(memberExpr); |
|||
} |
|||
else if (isField) |
|||
{ |
|||
reval = GetFiledValue(memberExpr); |
|||
} |
|||
} |
|||
if (memberExpr.Expression == null) |
|||
{ |
|||
|
|||
} |
|||
expression = memberExpr.Expression; |
|||
} |
|||
// fetch the root object reference:
|
|||
var constExpr = expression as ConstantExpression; |
|||
object objReference = constExpr.Value; |
|||
// "ascend" back whence we came from and resolve object references along the way:
|
|||
while (memberInfos.Count > 0) // or some other break condition
|
|||
{ |
|||
var mi = memberInfos.Pop(); |
|||
if (mi.MemberType == MemberTypes.Property) |
|||
{ |
|||
var objProp = objReference.GetType().GetProperty(mi.Name); |
|||
if (objProp == null) |
|||
{ |
|||
|
|||
} |
|||
objReference = objProp.GetValue(objReference, null); |
|||
} |
|||
else if (mi.MemberType == MemberTypes.Field) |
|||
{ |
|||
var objField = objReference.GetType().GetField(mi.Name); |
|||
if (objField == null) |
|||
{ |
|||
|
|||
} |
|||
objReference = objField.GetValue(objReference); |
|||
} |
|||
} |
|||
reval = objReference; |
|||
return reval; |
|||
} |
|||
|
|||
public static object GetFiledValue(MemberExpression memberExpr) |
|||
{ |
|||
object reval = null; |
|||
FieldInfo field = (FieldInfo)memberExpr.Member; |
|||
reval = field.GetValue(memberExpr.Member); |
|||
if (reval != null && reval.GetType().IsClass && reval.GetType() != ExpressionConst.StringType) |
|||
{ |
|||
var fieldName = memberExpr.Member.Name; |
|||
var proInfo = reval.GetType().GetProperty(fieldName); |
|||
if (proInfo != null) |
|||
{ |
|||
reval = proInfo.GetValue(reval, null); |
|||
} |
|||
var fieInfo = reval.GetType().GetField(fieldName); |
|||
if (fieInfo != null) |
|||
{ |
|||
reval = fieInfo.GetValue(reval); |
|||
} |
|||
if (fieInfo == null && proInfo == null) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
return reval; |
|||
} |
|||
|
|||
public static object GetPropertyValue(MemberExpression memberExpr) |
|||
{ |
|||
object reval = null; |
|||
PropertyInfo pro = (PropertyInfo)memberExpr.Member; |
|||
reval = pro.GetValue(memberExpr.Member, null); |
|||
if (reval != null && reval.GetType().IsClass && reval.GetType() != ExpressionConst.StringType) |
|||
{ |
|||
var fieldName = memberExpr.Member.Name; |
|||
var proInfo = reval.GetType().GetProperty(fieldName); |
|||
if (proInfo != null) |
|||
{ |
|||
reval = proInfo.GetValue(reval, null); |
|||
} |
|||
var fieInfo = reval.GetType().GetField(fieldName); |
|||
if (fieInfo != null) |
|||
{ |
|||
reval = fieInfo.GetValue(reval); |
|||
} |
|||
if (fieInfo == null && proInfo == null) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
return reval; |
|||
} |
|||
} |
|||
} |
@ -1,48 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Text; |
|||
namespace SqlSugar |
|||
{ |
|||
public class ExpressionTool |
|||
{ |
|||
public static string GetOperator(ExpressionType expressiontype) |
|||
{ |
|||
switch (expressiontype) |
|||
{ |
|||
case ExpressionType.And: |
|||
case ExpressionType.AndAlso: |
|||
return " AND "; |
|||
case ExpressionType.Equal: |
|||
return " ="; |
|||
case ExpressionType.GreaterThan: |
|||
return " >"; |
|||
case ExpressionType.GreaterThanOrEqual: |
|||
return ">="; |
|||
case ExpressionType.LessThan: |
|||
return "<"; |
|||
case ExpressionType.LessThanOrEqual: |
|||
return "<="; |
|||
case ExpressionType.NotEqual: |
|||
return "<>"; |
|||
case ExpressionType.Or: |
|||
case ExpressionType.OrElse: |
|||
return " OR "; |
|||
case ExpressionType.Add: |
|||
case ExpressionType.AddChecked: |
|||
return "+"; |
|||
case ExpressionType.Subtract: |
|||
case ExpressionType.SubtractChecked: |
|||
return "-"; |
|||
case ExpressionType.Divide: |
|||
return "/"; |
|||
case ExpressionType.Multiply: |
|||
case ExpressionType.MultiplyChecked: |
|||
return "*"; |
|||
default: |
|||
throw new NotSupportedException(string.Format(ExpressionErrorMessage.OperatorError, expressiontype.ToString())); |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,45 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Linq.Expressions; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace SqlSugar |
|||
{ |
|||
public class MemberNoExpressionResolve : BaseResolve |
|||
{ |
|||
public MemberNoExpressionResolve(ExpressionParameter parameter) : base(parameter) |
|||
{ |
|||
var expression = base.Expression as MemberExpression; |
|||
var isLeft = parameter.IsLeft; |
|||
var isSingle = parameter.Context.IsSingle; |
|||
object value = null; |
|||
var fieldInfo = expression.Member as System.Reflection.FieldInfo; |
|||
var propertyInfo = expression.Member as System.Reflection.PropertyInfo; |
|||
if (fieldInfo != null) |
|||
{ |
|||
value = ExpressionTool.GetFiledValue(expression); |
|||
} |
|||
else if (propertyInfo != null) |
|||
{ |
|||
value = ExpressionTool.GetPropertyValue(expression); |
|||
} |
|||
if (parameter.BaseParameter.BinaryExpressionInfoList != null) |
|||
{ |
|||
parameter.BaseParameter.BinaryExpressionInfoList.Add(new KeyValuePair<string, BinaryExpressionInfo>(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo() |
|||
{ |
|||
IsLeft = Convert.ToBoolean(isLeft), |
|||
Value = value, |
|||
ExpressionType = ExpressionConst.ConstantExpressionType |
|||
})); |
|||
} |
|||
if (isLeft == null && base.Context.SqlWhere == null) |
|||
{ |
|||
base.Context.SqlWhere = new StringBuilder(); |
|||
base.Context.SqlWhere.Append(value); |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue