lundi 31 août 2015

Dynamic Linq on method called with generic type via reflection

I have a Query Extender, which has a CustomExpression that I want to use to do some filtering on my datasource.

This is in a DynamicData website so I don't know the object type(current entity) at compile time. Say I knew the object type at compile time, then I could do something like this:

protected void GameFiltering(object sender, CustomExpressionEventArgs e)
{
        e.Query = e.Query.Cast<Resource>().Where(x => x.GameId == GameId);
}

I can get the type I need from e.Query.ElementType. Now I just to send the ElementType as a generic parameter to the Cast method and then call the linq method Where.

I'm gonna assume that every Type is going to have a GameId property which I'll want to filter by.

MethodInfo method = e.Query.GetType().GetMethod("Cast").MakeGenericMethod(new Type[] { e.Query.ElementType });
var castedQuery = method.Invoke(e.Query, null);

This is how I call the cast method with reflection, but I don't know how I can call the linq method on the resulting object.





Aucun commentaire:

Enregistrer un commentaire