简体   繁体   中英

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?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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