Suppose I have the following class:
public class GenericClass<T>
{
public T Find()
{
//return T if found otherwise null or Nullable<T>
}
}
我希望能够用
class
或struct
来专门化我的类。但我遇到了这个问题: 如果T
类型没有限制为struct
,那么我不能返回Nullable<T>
。
我想提供一个实现我的Find
方法,使其在T
被专门化为class
或struct
时都能工作。
如果Find
失败,我希望如果T
是class
则返回null
,否则返回Nullable<T>
。
这是否可以在不使用反射的情况下实现?如果可以,怎么实现?