How to declare a generic constraint that is a generic type

Posted by HackedByChinese on Stack Overflow See other posts from Stack Overflow or by HackedByChinese
Published on 2010-04-20T18:19:27Z Indexed on 2010/04/20 18:23 UTC
Read the original article Hit count: 295

Filed under:
|
|

I have a two generic abstract types: Entity and Association.

Let's say Entity looks like this:

public class Entity<TId>
{ 
//...
}

and Association looks like this:

public class Association<TEntity, TEntity2>
{
//...
}

How do I constrain Association so they can be of any Entity?

I can accomplish it by the following:

public class Association<TEntity, TId, TEntity2, TId2>
     where TEntity : Entity<TId>
     where TEntity2: Entity<TId2>
{
//...
}

This gets very tedious as more types derive from Association, because I have to keep passing down TId and TId2. Is there a simpler way to do this, besides just removing the constraint?

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics