简体   繁体   English

如何约束多个泛型类型?

[英]How to constrain multiple generic types?

Here's a simple syntax question (I hope), I know how to constrain one generic type using the where clause, but how to constrain two generic types? 这是一个简单的语法问题(我希望),我知道如何使用where子句约束一个泛型类型,但是如何约束两个泛型类型?

Maybe the easiest way is to write down what my best guess as to the syntax was. 也许最简单的方法是记下我对语法的最佳猜测。

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

This gives me an error. 这给了我一个错误。 Anyone know what the proper syntax is? 任何人都知道正确的语法是什么?

Use two 'where' keywords, for example I have a declaration like this: 使用两个'where'关键字,例如我有这样的声明:

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

This should work: 这应该工作:

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

you just repeat the where. 你只需重复一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM