Can a generic type constraint be specified so that it executes a generic type?

Here is what I would like to do:

public interface IRepository<TSet<TElement>> where TSet<TElement> : IEnumerable<TElement> { TSet<TEntity> GetSet<TEntity>(); } 

Is such a construction possible in .NET?

Change The question was not clear enough. Here is what I want to do, expanded:

 public class DbRepository : IRepository<DbSet<TElement>> { DbSet<TEntity> GetSet<TEntity>(); } public class ObjectRepository : IRepository<ObjectSet<TElement>> { ObjectSet<TEntity> GetSet<TEntity>(); } 

Value, I want the associated type: - accept one common parameter - implement the given one common parameter interface.

Is it possible? In fact, I will be glad only the first.

 public interface IRepository<TGeneric<TElement>> { TGeneric<TArgument> GetThing<TArgument>(); } 
+6
generics c # type-constraints
source share
2 answers

To achieve this, you need to use two general types, for example:

 public interface IRepository<TCollection, TItem> where TCollection : IEnumerable<TItem> { TCollection GetSet<TItem>(); } 

(I guess TEntity should be TElement in the original ...)

Having said that, it's probably best to write something like:

 public interface IRepository<T> { IEnumerable<T> GetSet<T>(); } 

This would be a more common means of achieving the above.

+5
source share

In the end, this is not possible.

I finally worked on this by specifying my own interface:

public interface IRepository {IMySet GetSet (); }

And I wrapped both DbSet and ObjectSet in objects that implement these interfaces.

0
source share

All Articles