有没有办法获得以下函数声明?
public bool Foo<T>() where T : interface;
Run Code Online (Sandbox Code Playgroud)
即.其中T是接口类型(类似于where T : class,和struct).
目前我已经满足于:
public bool Foo<T>() where T : IBase;
Run Code Online (Sandbox Code Playgroud)
其中IBase被定义为一个空接口,由我的所有自定义接口继承...不理想,但它应该工作...为什么你不能定义泛型类型必须是一个接口?
对于它的价值,我想要这个,因为Foo它正在做需要接口类型的反射......我可以将它作为普通参数传递并在函数本身中进行必要的检查,但这看起来更加类型安全(而且我假设性能更高一些,因为所有检查都是在编译时完成的).