gpt4 book ai didi

C# - 泛型类中的方法,对 T 有额外的约束

转载 作者:太空狗 更新时间:2023-10-29 23:59:39 24 4
gpt4 key购买 nike

我有一个通用类 Foo<T> where T: SomeBaseType .

在特定情况下 TSpecificDerivedType ,我希望我的类有一个额外的方法。

类似于:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com