How to make generic function that return int or date or string
I need to realize a function which return an Expression<Func<MyObject, T>>
, where T could be an integer, a string or a datetime.
The caller won't know the type of T.
What I want to achieve is a function which create a predicate that can be used in a Linq OrderBy function.
The should be something like:
class FilterCreator {
...
Expression<Func<MyObject, T>> getOrderExpression()
{
...
}
}
class Consumer{
void mymethod()
{
var orderedL开发者_JAVA技巧ist = MyList.OrderBy(filterCreator.getOrderExpression());
}
}
Generics don't work that way. You would have to either make the caller know the type of T or replace T with Object.
精彩评论