Operator as and generic classes
Posted
by abatishchev
on Stack Overflow
See other posts from Stack Overflow
or by abatishchev
Published on 2009-03-28T20:27:06Z
Indexed on
2010/04/07
13:43 UTC
Read the original article
Hit count: 176
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?
© Stack Overflow or respective owner