I have a generic method, with the following signature
private static void ProcessItem<T>(int id) where T : BaseItem
And it gets called in the following way
ProcessItem<RedItem>(id: 2)
ProcessItem<BlueItem>(id: 7)
My issue is that I get the type of item on application start up via reflection as a Type
object.
So now I am looking for a way that I can construct an expression tree that will result in a lambda that calls this method with the appropriate type.
So I can do something like lambda.Invoke(Type type, int id)
. And this will call something along the lines of id => ProcessItem<type>(id)
I found a similliar issue answered here: https://stackoverflow.com/a/2850366/5493420
However, the answer there shows how to instantiate a generic param of IEnumerable<>
whereas I simply need to call my method, transforming the Type
object into an actual <Type>
parameter that my method can accept.
I am not exactly sure if this is even possible (converting a Type object into an actual type parameter), but would love to hear any suggestions on the topic.
PS: I am also willing to take a different approach (some reflection tricks maybe), as long as it doesn't affect performance by more than 10-20%