我想做一个方法:
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), nor T.GetType() are possible
return (T) Execute(); // ok
}
但我认为 operatoras
将非常有用:如果结果类型不是T
方法将返回null
,而不是异常!有可能吗?