作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个通用类 Foo<T> where T: SomeBaseType
.
在特定情况下 T
是SpecificDerivedType
,我希望我的类有一个额外的方法。
类似于:
class Foo<T> where T: SomeBaseType
{
//.... lots of regular methods taking T, outputting T, etc.
//in pseudo code
void SpecialMethod() visible if T: SpecificDerivedType
{
//...code...
}
}
我怎样才能做到这一点?
最佳答案
制作一个extension method对于 Foo<T>
:
public static void SpecialMethod<T>(this Foo<T> foo)
where T : SpecificDerivedType
{
}
关于C# - 泛型类中的方法,对 T 有额外的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50393569/
我是一名优秀的程序员,十分优秀!