views:

57

answers:

1

I am using reflection along with linq.Dynamic, and I am having a small problem with creating a query that needs to get IQueryable<T> from an IList<T> or ISet<T> when I currently have an object.

At first I thought I could write a little helper method:

object Helper<T>( IList<T> list, string query, param object[] values )
{
... do query and return result.
}

Unfortunately the compiler needs to infer the T argument which it cannot do when passed an object.
Is there a simple way to get around this? I am trying to avoid dynamically invoking this helper function as well.

EDIT: I have a Domain object that currently as several IList collections I am trying to query against this class to find the proper object to display on the screen. Basically I have a screen guid 10 and a xpathish string "FruitBasket/Fruit[Ripe == true]/color" So I know Fruit is an IList in my reflection code I inspect the property to find out if it implements IList now I am at the point where i need to query this collection to pick out the Fruit.Ripe == true objects. For this I need a cast to IQuerable

+4  A: 

If you've only got object, how do you expect the compiler to know what T should be?

You haven't shown how you're trying to call this method... do you know (at compile-time) what the type of T should be (even in terms of another type parameter to the calling method)? If not you will have to call it dynamically.

Is this object actually an IList<T> for some T? What would expect to happen if it implemented IList<T> for two different types?

I suggest you take a step back and work out what information is genuinely known at compile time, and what's only known at execution time. Calling a generic method statically will require that you know the type arguments at compile time.

Jon Skeet