I am attempting to Serialize and Deserialize Linq queries. Currently I am using Serialize.Linq to serialize and deserialize Linq queries via json. Like so:
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...
}
I am able to successfully deserialize the query if I know the type upon which the query expression is to operate:
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;
}
However I would like to be able to detect that type at runtime rather than compile time in cases like this:
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;
}
Is there any way to get from an expression what type is being operated on? and If so, Is there a way to convert that expression into a Func?