1、把Json转换为object,可以用开源的Json.Net。具体可以调用JToken.Parse(string)。
2、判定QueryCondition,可以在QueryCondition类中,加一个Eval判定函数:
public class QueryCondition
{
//逻辑运算符
public string Op;
//参数数组
public Parameter[] Parms; public object Eval(object context)
{
object[] parameters = Parms.Select(p => p.Eval(context)).ToArray(); object obj1 = parameters[0], obj2 = parameters.Length > 1 ? parameters[1] : null;
CoerceTypes(ref obj1, ref obj2);
switch (Op)
{
case "<":
return System.Collections.Comparer.Default.Compare(obj1, obj2) < 0;
case "=":
return object.Equals(obj1, obj2);
case "and":
return parameters.All(p => (p as bool?) == true);
}
throw new NotImplementedException(Op + " operator not implemented");
} private static void CoerceTypes(ref object obj1, ref object obj2)
{
if (object.ReferenceEquals(obj1, null) || object.ReferenceEquals(obj2, null)) return;
TypeCode typecode1 = Type.GetTypeCode(obj1.GetType());
TypeCode typecode2 = Type.GetTypeCode(obj2.GetType());
if (typecode1 != TypeCode.Object && obj2 is IConvertible) obj2 = Convert.ChangeType(obj2, typecode1);
if (typecode2 != TypeCode.Object && obj1 is IConvertible) obj1 = Convert.ChangeType(obj1, typecode2);
}
}
3、Parameter转换也可以在Parameter类中,添加一个Eval函数:
public class Parameter
{
//...
public object Eval(object context)
{
switch(ParmType)
{
case ParameterType.Boolean:
return (bool) Parm;
case ParameterType.DateTime:
return (DateTime) Parm;
case ParameterType.LogicExpr:
if (Parm is bool || Parm is bool?) return Parm;
return (Parm as QueryCondition).Eval(context);
case ParameterType.Number:
return Convert.ToDouble(Parm);
case ParameterType.Property:
if (object.ReferenceEquals(context, null)) throw new ArgumentNullException("context");
var indexer = context.GetType().GetProperty("Item", new Type[] { typeof(string) });
if (indexer != null) return indexer.GetValue(context, new object[] { Parm });
return context.GetType().GetProperty(Parm as string).GetValue(context);
case ParameterType.Str:
return (string)Parm;
}
throw new NotImplementedException(ParmType + " parameter type not implemented");
}
}这样,就可以对QueryCondition进行判定:
static void Main(string[] args)
{ QueryCondition condition = new QueryCondition();
QueryCondition condition1 = new QueryCondition();
QueryCondition condition2 = new QueryCondition(); condition.Op = "and";
condition.Parms = new Parameter[]
{
new Parameter(ParameterType.LogicExpr, condition1),
new Parameter(ParameterType.LogicExpr, condition2),
}; condition1.Op = "<";
condition1.Parms = new Parameter[]
{
new Parameter(ParameterType.Property, "age"),
new Parameter(ParameterType.Number, 30),
}; condition2.Op = "=";
condition2.Parms = new Parameter[]
{
new Parameter(ParameterType.Property, "grade"),
new Parameter(ParameterType.Number, 3),
}; string json = "{'name':'John','age':10,'grade':3}";
bool result = (bool)condition.Eval(JToken.Parse(json)); // result = true
}
2、判定QueryCondition,可以在QueryCondition类中,加一个Eval判定函数:
public class QueryCondition
{
//逻辑运算符
public string Op;
//参数数组
public Parameter[] Parms; public object Eval(object context)
{
object[] parameters = Parms.Select(p => p.Eval(context)).ToArray(); object obj1 = parameters[0], obj2 = parameters.Length > 1 ? parameters[1] : null;
CoerceTypes(ref obj1, ref obj2);
switch (Op)
{
case "<":
return System.Collections.Comparer.Default.Compare(obj1, obj2) < 0;
case "=":
return object.Equals(obj1, obj2);
case "and":
return parameters.All(p => (p as bool?) == true);
}
throw new NotImplementedException(Op + " operator not implemented");
} private static void CoerceTypes(ref object obj1, ref object obj2)
{
if (object.ReferenceEquals(obj1, null) || object.ReferenceEquals(obj2, null)) return;
TypeCode typecode1 = Type.GetTypeCode(obj1.GetType());
TypeCode typecode2 = Type.GetTypeCode(obj2.GetType());
if (typecode1 != TypeCode.Object && obj2 is IConvertible) obj2 = Convert.ChangeType(obj2, typecode1);
if (typecode2 != TypeCode.Object && obj1 is IConvertible) obj1 = Convert.ChangeType(obj1, typecode2);
}
}
3、Parameter转换也可以在Parameter类中,添加一个Eval函数:
public class Parameter
{
//...
public object Eval(object context)
{
switch(ParmType)
{
case ParameterType.Boolean:
return (bool) Parm;
case ParameterType.DateTime:
return (DateTime) Parm;
case ParameterType.LogicExpr:
if (Parm is bool || Parm is bool?) return Parm;
return (Parm as QueryCondition).Eval(context);
case ParameterType.Number:
return Convert.ToDouble(Parm);
case ParameterType.Property:
if (object.ReferenceEquals(context, null)) throw new ArgumentNullException("context");
var indexer = context.GetType().GetProperty("Item", new Type[] { typeof(string) });
if (indexer != null) return indexer.GetValue(context, new object[] { Parm });
return context.GetType().GetProperty(Parm as string).GetValue(context);
case ParameterType.Str:
return (string)Parm;
}
throw new NotImplementedException(ParmType + " parameter type not implemented");
}
}这样,就可以对QueryCondition进行判定:
static void Main(string[] args)
{ QueryCondition condition = new QueryCondition();
QueryCondition condition1 = new QueryCondition();
QueryCondition condition2 = new QueryCondition(); condition.Op = "and";
condition.Parms = new Parameter[]
{
new Parameter(ParameterType.LogicExpr, condition1),
new Parameter(ParameterType.LogicExpr, condition2),
}; condition1.Op = "<";
condition1.Parms = new Parameter[]
{
new Parameter(ParameterType.Property, "age"),
new Parameter(ParameterType.Number, 30),
}; condition2.Op = "=";
condition2.Parms = new Parameter[]
{
new Parameter(ParameterType.Property, "grade"),
new Parameter(ParameterType.Number, 3),
}; string json = "{'name':'John','age':10,'grade':3}";
bool result = (bool)condition.Eval(JToken.Parse(json)); // result = true
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货