5

So, I have a little problem here.

Suppose I have:

public class Repository<TEntity>
    where TEntity : class
{
    public abstract void Add(TEntity entity);

    // ...and so on...
}

And now I want to define a contract class, like so:

public class RepositoryContracts<TEntity> : Repository<TEntity>
    where TEntity : class
{
    public void Add(TEntity entity)
    {
        Contract.Requires(entity != null);
    }

    // ...etc...
}

Now, I'd have to mark these classes with ContractClassAttribute and ContractClassForAttribute. Problem being, this won't work:

[ContractClassFor(typeof(Repository<TEntity>))] // what is TEntity?! error!

So, the question boils down to: How do I link these two classes together using those attributes, when they're generic?

1 Answers1

5

Turns out this is a duplicate of this question, kinda.

The typeof(Repository<>) syntax didn't seem to work for me, but turns out, typeof(Repository<,>) does the trick, since there are two type parameters.

Closing the question, and adding a comment to the previous one.

Community
  • 1
  • 1
  • I am having the same issue, removing the generic `TEntity` solved it but when i `implement` this `interface` on some other `class`, it complaints that `class` or `interface` does not implement IRepository methods. Any ideas ? – Ali Umair May 09 '17 at 13:49