我正在尝试序列化和反序列化Linq查询。目前我正在使用Serialize.Linq通过json序列化和反序列化Linq查询。像这样:
public async Task StoreQuery<T>(string queriedTypeName, string queryName, Expression<Func<T, bool>> query, IEnumerable<T> results)
where T : class, IStorable
{
var expressionSerializer = new ExpressionSerializer(new Serialize.Linq.Serializers.JsonSerializer());
var queryJson = expressionSerializer.SerializeText(query);
await storage.AddQuery(queriedTypeName + ".queries", queryJson, ...);
//etc...
}
如果我知道查询表达式的操作类型,我能够成功反序列化查询:
public static bool QueryWouldContain<T>(T storable, string queryJson)
where T : class, IStorable
{
var queryStatement = expressionSerializer.DeserializeText(queryJson);
var expressionType = queryStatement.ToExpressionNode().ToExpression<Func<T,bool>>().Compile();
var objectBelongsInQueryResults = expressionType.Invoke(obj)
return objectBelongsInQueryResults;
}
但是,我希望能够在运行时检测该类型,而不是在这种情况下编译时间:
public static async bool QueriesWouldContain<T>(IEnumerable<T> storables, List<string> queryStrings)
where T : class, IStorable
{
foreach (var querystring in queryStrings)
{
var expressionSerializer = new ExpressionSerializer(new JsonSerializer());
var queryStatement = expressionSerializer.DeserializeText(querystring);
var expression = queryStatement.ToExpressionNode().ToExpression<Func<?, bool>>().Compile();
foreach (var storable in storables)
{
if (isOfTypeMatchingQuery(storable, expression))
{
var result = expression.Invoke(storable);
if (result == false)
{
return false;
}
}
}
return true;
}
有没有办法从表达式中获取正在操作的类型?如果是这样,有没有办法将该表达式转换为Func?
答案 0 :(得分:4)
调用ToExpression
时,您不必指定通用类型。只需测试,如果它返回LambdaExpression
。
public static async bool QueriesWouldContain<T>(IEnumerable<T> storables, List<string> queryStrings)
where T : class, IStorable
{
foreach (var querystring in queryStrings)
{
var expressionSerializer = new ExpressionSerializer(new JsonSerializer());
var queryStatement = expressionSerializer.DeserializeText(querystring);
var expression = queryStatement.ToExpressionNode().ToExpression();
if (!(expression is LambdaExpression lambdaExpression))
continue; // TODO: or throw
var d = lambdaExpression.Compile();
foreach (var storable in storables)
{
if (isOfTypeMatchingQuery(storable, d))
{
var result = (bool)d.Invoke(storable);
if (result == false)
{
return false;
}
}
}
return true;
}
}
答案 1 :(得分:2)
在@esskar发布他的回答之前,我想出了这个丑陋的话:
...
var expressionNode = queryStatement.ToExpressionNode();
var funcType = typeof(Func<,>).MakeGenericType(new Type[] { type, typeof(bool) });
var method = typeof(ExpressionNode).GetMethods().First(meth => meth.GetGenericArguments().Any() && meth.Name == "ToExpression" && meth.GetParameters().Count() == 1);
var methodWithGenerics = method.MakeGenericMethod(funcType);
dynamic expressionUncompiled = methodWithGenerics.Invoke(expressionNode, new object[] { null });
var expression = expressionUncompiled.Compile();
不用说,他的回答更为可取。