I'm writing .NET On-the-Fly compiler for CLR scripting and want execution method make generic acceptable:
object Execute()
{
return type.InvokeMember(..);
}
T Execute<T>()
{
return Execute() as T; /* doesn't work:
The type parameter 'T' cannot be used with the 'as' operator because
it does not have a class type constraint nor a 'class' constraint */
// also neither typeof(T) not T.GetType(), so on are possible
return (T) Execute(); // ok
}
But I think operator as
will be very useful: if result type isn't T
method will return null
, instead of an exception! Is it possible to do?