views:

35

answers:

1

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?

A: 

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.

Zor