I want to call a function that generates a function, but I don't know the type parameter until I call the generated function as follows:
ActionResult Foo() {
IEnumerable<MyObject> list = GetList();
var orderBy = OrderBy(list);
switch(Request.QueryString["sortBy"]) {
case "Name":
return orderBy<string>(o => o.Name);
case "TrackingNumber":
return orderBy<int>(o => o.TrackingNumber);
default:
return View(list);
}
}
I will not know the type T until I call the returned function. I imagine something like this to generate a function, it completes the list in close, so I don't need to skip it.
Func<Func<MyObject,T>,ActionResult> OrderBy<T>(IEnumerable<MyObject> list) {
Func<Func<MyObject,T>, ActionResult> f = orderBy => {
return View(Request.QueryString["sortDir"] == "d"
? list.OrderBy<MyObject, T>(orderBy)
: list.OrderByDescending<MyObject, T>(orderBy));
};
return f;
}
Update:
I know there are better ways to do this. I want to know how I can wrap this list in closure and return a function that has a generic type that I don’t know about in advance. For example, since I cannot return a function of a general type without knowing the type, is there any type that I can replace with it?
source
share