I have this static function
public static object Create(Type t)
{
//unimportant
}
I don't have control on the above function above, so I cant alter it. The problem is it's not generic, so I have to cast the returned object to some type. This type is provided by the constraint of another generic class from where I call the Create
method.
This is where I have reached:
public static class Creator<T>
{
public static void Create()
{
var m = typeof(SomeClass).GetMethod("Create");
var p = Expression.Parameter(typeof(Type));
var e = Expression.Call(m, p);
//at this stage I want to create delegate for calling the 'Create' method,
//then pass typeof(T) as parameter, get returned object,
//and finally cast it to 'T'.
//for eg, I can do it like this:
var f = Expression.Lambda<Func<Type, object>>(e, p).Compile();
Func<T> mainThing = () => (T)f(typeof(T));
//is there a way I can achieve in one step?
}
}
In my above approach I'm not compiling the final delegate but one step previously. How do I incorporate the cast too before compilation and get Func<T>
back?