gpt4 book ai didi

c# - 如何约束多个泛型类型?

转载 作者:IT王子 更新时间:2023-10-29 04:37:04 24 4
gpt4 key购买 nike

这是一个简单的语法问题(我希望),我知道如何使用 where 子句约束一个泛型类型,但如何约束两个泛型类型?

也许最简单的方法是写下我对语法的最佳猜测。

public class GenericDaoGetByIdTests<TDao, TComponent> : BaseDaoTests 
where TDao : IDao<TComponent>, TComponent : EDC2ORMComponent {
public void GetByIdTest(int id) { }
}

这给了我一个错误。有人知道正确的语法是什么吗?

最佳答案

使用两个“where”关键字,例如我有这样的声明:

public interface IParentNodeT<TChild, TSelf>
where TChild : IChildNodeT<TSelf, TChild>, INodeT<TChild>
where TSelf : IParentNodeT<TChild, TSelf>
{
TChild childRoot { get; set; }
}

关于c# - 如何约束多个泛型类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/401174/

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